diff options
Diffstat (limited to 'Lib/distutils/fancy_getopt.py')
-rw-r--r-- | Lib/distutils/fancy_getopt.py | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/Lib/distutils/fancy_getopt.py b/Lib/distutils/fancy_getopt.py index e65302f..fe9b0d4 100644 --- a/Lib/distutils/fancy_getopt.py +++ b/Lib/distutils/fancy_getopt.py @@ -157,13 +157,18 @@ class FancyGetopt: self.long_opts = [] self.short_opts = [] self.short2long.clear() + self.repeat = {} for option in self.option_table: - try: - (long, short, help) = option - except ValueError: - raise DistutilsGetoptError, \ - "invalid option tuple " + str(option) + if len(option) == 3: + long, short, help = option + repeat = 0 + elif len(option) == 4: + long, short, help, repeat = option + else: + # the option table is part of the code, so simply + # assert that it is correct + assert "invalid option tuple: %s" % `option` # Type- and value-check the option names if type(long) is not StringType or len(long) < 2: @@ -177,6 +182,7 @@ class FancyGetopt: ("invalid short option '%s': " "must a single character or None") % short + self.repeat[long] = 1 self.long_opts.append(long) if long[-1] == '=': # option takes an argument? @@ -232,14 +238,15 @@ class FancyGetopt: def getopt (self, args=None, object=None): - """Parse the command-line options in 'args' and store the results - as attributes of 'object'. If 'args' is None or not supplied, uses - 'sys.argv[1:]'. If 'object' is None or not supplied, creates a new - OptionDummy object, stores option values there, and returns a tuple - (args, object). If 'object' is supplied, it is modified in place - and 'getopt()' just returns 'args'; in both cases, the returned - 'args' is a modified copy of the passed-in 'args' list, which is - left untouched. + """Parse command-line options in args. Store as attributes on object. + + If 'args' is None or not supplied, uses 'sys.argv[1:]'. If + 'object' is None or not supplied, creates a new OptionDummy + object, stores option values there, and returns a tuple (args, + object). If 'object' is supplied, it is modified in place and + 'getopt()' just returns 'args'; in both cases, the returned + 'args' is a modified copy of the passed-in 'args' list, which + is left untouched. """ if args is None: args = sys.argv[1:] @@ -253,30 +260,23 @@ class FancyGetopt: short_opts = string.join(self.short_opts) try: - (opts, args) = getopt.getopt(args, short_opts, self.long_opts) + opts, args = getopt.getopt(args, short_opts, self.long_opts) except getopt.error, msg: raise DistutilsArgError, msg - for (opt, val) in opts: + for opt, val in opts: if len(opt) == 2 and opt[0] == '-': # it's a short option opt = self.short2long[opt[1]] - - elif len(opt) > 2 and opt[0:2] == '--': - opt = opt[2:] - else: - raise DistutilsInternalError, \ - "this can't happen: bad option string '%s'" % opt + assert len(opt) > 2 and opt[:2] == '--' + opt = opt[2:] alias = self.alias.get(opt) if alias: opt = alias if not self.takes_arg[opt]: # boolean option? - if val != '': # shouldn't have a value! - raise DistutilsInternalError, \ - "this can't happen: bad option value '%s'" % val - + assert val == '', "boolean option can't have value" alias = self.negative_alias.get(opt) if alias: opt = alias @@ -285,13 +285,16 @@ class FancyGetopt: val = 1 attr = self.attr_name[opt] + # The only repeating option at the moment is 'verbose'. + # It has a negative option -q quiet, which should set verbose = 0. + if val and self.repeat.get(attr) is not None: + val = getattr(object, attr, 0) + 1 setattr(object, attr, val) self.option_order.append((opt, val)) # for opts - if created_object: - return (args, object) + return args, object else: return args |