public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Zac Medico" <zmedico@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/emaint/modules/logs/, pym/portage/emaint/
Date: Fri,  2 Aug 2013 21:49:17 +0000 (UTC)	[thread overview]
Message-ID: <1375480094.c9a8f7c11183f368c1d046f125c542a986f13357.zmedico@gentoo> (raw)

commit:     c9a8f7c11183f368c1d046f125c542a986f13357
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Fri Aug  2 21:48:14 2013 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Fri Aug  2 21:48:14 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=c9a8f7c1

emaint: portage.util._argparse

---
 pym/portage/emaint/defaults.py              |  7 ++-
 pym/portage/emaint/main.py                  | 66 ++++++++++++++++++++---------
 pym/portage/emaint/modules/logs/__init__.py |  4 +-
 3 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/pym/portage/emaint/defaults.py b/pym/portage/emaint/defaults.py
index ee0dc11..30f36af 100644
--- a/pym/portage/emaint/defaults.py
+++ b/pym/portage/emaint/defaults.py
@@ -16,5 +16,10 @@ FIX = {"short": "-f", "long": "--fix",
 	'func': 'fix'
 	}
 
+VERSION = {"long": "--version",
+	"help": "show program's version number and exit",
+	'action': 'store_true',
+	}
+
 # parser options
-DEFAULT_OPTIONS = {'check': CHECK, 'fix': FIX}
+DEFAULT_OPTIONS = {'check': CHECK, 'fix': FIX, 'version': VERSION}

diff --git a/pym/portage/emaint/main.py b/pym/portage/emaint/main.py
index 3476397..9f987fa 100644
--- a/pym/portage/emaint/main.py
+++ b/pym/portage/emaint/main.py
@@ -6,34 +6,56 @@ from __future__ import print_function
 
 import sys
 import textwrap
-from optparse import OptionParser
-
 
 import portage
 from portage import os
 from portage.emaint.module import Modules
 from portage.emaint.progress import ProgressBar
 from portage.emaint.defaults import DEFAULT_OPTIONS
+from portage.util._argparse import ArgumentParser
 
 class OptionItem(object):
-	"""class to hold module OptionParser options data
+	"""class to hold module ArgumentParser options data
 	"""
 
-	def __init__(self, opt, parser):
+	def __init__(self, opt):
 		"""
 		@type opt: dictionary
 		@param opt: options parser options
 		"""
-		self.parser = parser
-		self.short = opt['short']
-		self.long = opt['long']
-		self.help = opt['help']
-		self.status = opt['status']
-		self.func = opt['func']
-		self.action = opt.get('action', 'store')
-		self.type = opt.get('type', None)
-		self.dest = opt.get('dest', None)
-
+		self.short = opt.get('short')
+		self.long = opt.get('long')
+		self.help = opt.get('help')
+		self.status = opt.get('status')
+		self.func = opt.get('func')
+		self.action = opt.get('action')
+		self.type = opt.get('type')
+		self.dest = opt.get('dest')
+
+	@property
+	def pargs(self):
+		pargs = []
+		if self.short is not None:
+			pargs.append(self.short)
+		if self.long is not None:
+			pargs.append(self.long)
+		return pargs
+
+	@property
+	def kwargs(self):
+		# Support for keyword arguments varies depending on the action,
+		# so only pass in the keywords that are needed, in order
+		# to avoid a TypeError.
+		kwargs = {}
+		if self.help is not None:
+			kwargs['help'] = self.help
+		if self.action is not None:
+			kwargs['action'] = self.action
+		if self.type is not None:
+			kwargs['type'] = self.type
+		if self.dest is not None:
+			kwargs['dest'] = self.dest
+		return kwargs
 
 def usage(module_controller):
 		_usage = "usage: emaint [options] COMMAND"
@@ -133,21 +155,25 @@ def emaint_main(myargv):
 	module_names.insert(0, "all")
 
 
-	parser = OptionParser(usage=usage(module_controller), version=portage.VERSION)
+	parser = ArgumentParser(usage=usage(module_controller))
 	# add default options
 	parser_options = []
 	for opt in DEFAULT_OPTIONS:
-		parser_options.append(OptionItem(DEFAULT_OPTIONS[opt], parser))
+		parser_options.append(OptionItem(DEFAULT_OPTIONS[opt]))
 	for mod in module_names[1:]:
 		desc = module_controller.get_func_descriptions(mod)
 		if desc:
 			for opt in desc:
-				parser_options.append(OptionItem(desc[opt], parser))
+				parser_options.append(OptionItem(desc[opt]))
 	for opt in parser_options:
-		parser.add_option(opt.short, opt.long, help=opt.help, action=opt.action,
-		type=opt.type, dest=opt.dest)
+		parser.add_argument(*opt.pargs, **opt.kwargs)
+
+	options, args = parser.parse_known_args(args=myargv)
+
+	if options.version:
+		print(portage.VERSION)
+		return os.EX_OK
 
-	(options, args) = parser.parse_args(args=myargv)
 	if len(args) != 1:
 		parser.error("Incorrect number of arguments")
 	if args[0] not in module_names:

diff --git a/pym/portage/emaint/modules/logs/__init__.py b/pym/portage/emaint/modules/logs/__init__.py
index c5204b7..0407efe 100644
--- a/pym/portage/emaint/modules/logs/__init__.py
+++ b/pym/portage/emaint/modules/logs/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2005-2012 Gentoo Foundation
+# Copyright 2005-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 """Check and clean old logs in the PORT_LOGDIR.
@@ -27,7 +27,7 @@ module_spec = {
 					"short": "-t", "long": "--time",
 					"help": "(cleanlogs only): -t, --time   Delete logs older than NUM of days",
 					'status': "",
-					'type': 'int',
+					'type': int,
 					'dest': 'NUM',
 					'func': 'clean'
 					},


             reply	other threads:[~2013-08-02 21:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-02 21:49 Zac Medico [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-08-02 20:20 [gentoo-commits] proj/portage:master commit in: pym/portage/emaint/modules/logs/, pym/portage/emaint/ Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1375480094.c9a8f7c11183f368c1d046f125c542a986f13357.zmedico@gentoo \
    --to=zmedico@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox