emaint: portage.util._argparse
authorZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 21:48:14 +0000 (14:48 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 21:48:14 +0000 (14:48 -0700)
pym/portage/emaint/defaults.py
pym/portage/emaint/main.py
pym/portage/emaint/modules/logs/__init__.py

index ee0dc11183fd97d37e30e3eac3cabfe531cad455..30f36af50abca266c9ce155f45effc8f5ff7a6fa 100644 (file)
@@ -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}
index 34763971f3b0b49b589e062d9e33470e40583764..9f987fa41f4a7f5084459c29fe92079681ededb8 100644 (file)
@@ -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:
index c5204b761f07c6e549d70bb14f55ec662912d132..0407efe2bef19f2f815c53790e1ea790366b9967 100644 (file)
@@ -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'
                                        },