Continuing the refactoring: deleted the old 'fancy_getopt()' function,
leaving in its place a tiny wrapper around the FancyGetopt class for backwards compatibility.
This commit is contained in:
parent
ffc10d9a2e
commit
ead5c291bb
|
@ -266,127 +266,6 @@ class FancyGetopt:
|
|||
# class FancyGetopt
|
||||
|
||||
|
||||
def fancy_getopt (options, negative_opt, object, args):
|
||||
|
||||
# The 'options' table is a list of 3-tuples:
|
||||
# (long_option, short_option, help_string)
|
||||
# if an option takes an argument, its long_option should have '='
|
||||
# appended; short_option should just be a single character, no ':' in
|
||||
# any case. If a long_option doesn't have a corresponding
|
||||
# short_option, short_option should be None. All option tuples must
|
||||
# have long options.
|
||||
|
||||
# Build the short_opts string and long_opts list, remembering how
|
||||
# the two are tied together
|
||||
|
||||
short_opts = [] # we'll join 'em when done
|
||||
long_opts = []
|
||||
short2long = {}
|
||||
attr_name = {}
|
||||
takes_arg = {}
|
||||
|
||||
for option in options:
|
||||
try:
|
||||
(long, short, help) = option
|
||||
except ValueError:
|
||||
raise DistutilsGetoptError, \
|
||||
"invalid option tuple " + str (option)
|
||||
|
||||
# Type-check the option names
|
||||
if type (long) is not StringType or len (long) < 2:
|
||||
raise DistutilsGetoptError, \
|
||||
"long option '%s' must be a string of length >= 2" % \
|
||||
long
|
||||
|
||||
if (not ((short is None) or
|
||||
(type (short) is StringType and len (short) == 1))):
|
||||
raise DistutilsGetoptError, \
|
||||
"short option '%s' must be None or string of length 1" % \
|
||||
short
|
||||
|
||||
long_opts.append (long)
|
||||
|
||||
if long[-1] == '=': # option takes an argument?
|
||||
if short: short = short + ':'
|
||||
long = long[0:-1]
|
||||
takes_arg[long] = 1
|
||||
else:
|
||||
|
||||
# Is option is a "negative alias" for some other option (eg.
|
||||
# "quiet" == "!verbose")?
|
||||
alias_to = negative_opt.get(long)
|
||||
if alias_to is not None:
|
||||
if not takes_arg.has_key(alias_to) or takes_arg[alias_to]:
|
||||
raise DistutilsGetoptError, \
|
||||
("option '%s' is a negative alias for '%s', " +
|
||||
"which either hasn't been defined yet " +
|
||||
"or takes an argument") % (long, alias_to)
|
||||
|
||||
long_opts[-1] = long
|
||||
takes_arg[long] = 0
|
||||
|
||||
else:
|
||||
takes_arg[long] = 0
|
||||
|
||||
|
||||
# Now enforce some bondage on the long option name, so we can later
|
||||
# translate it to an attribute name in 'object'. Have to do this a
|
||||
# bit late to make sure we've removed any trailing '='.
|
||||
if not longopt_re.match (long):
|
||||
raise DistutilsGetoptError, \
|
||||
("invalid long option name '%s' " +
|
||||
"(must be letters, numbers, hyphens only") % long
|
||||
|
||||
attr_name[long] = string.translate (long, longopt_xlate)
|
||||
if short:
|
||||
short_opts.append (short)
|
||||
short2long[short[0]] = long
|
||||
|
||||
# end loop over 'options'
|
||||
|
||||
short_opts = string.join (short_opts)
|
||||
try:
|
||||
(opts, args) = getopt.getopt (args, short_opts, long_opts)
|
||||
except getopt.error, msg:
|
||||
raise DistutilsArgError, msg
|
||||
|
||||
global _option_order # blechh! should OO-ify this module
|
||||
_option_order = []
|
||||
|
||||
for (opt, val) in opts:
|
||||
if len (opt) == 2 and opt[0] == '-': # it's a short option
|
||||
opt = 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
|
||||
|
||||
if not takes_arg[opt]: # boolean option?
|
||||
if val != '': # shouldn't have a value!
|
||||
raise DistutilsInternalError, \
|
||||
"this can't happen: bad option value '%s'" % value
|
||||
|
||||
alias = negative_opt.get (opt)
|
||||
if alias:
|
||||
opt = alias
|
||||
val = 0
|
||||
else:
|
||||
val = 1
|
||||
|
||||
attr = attr_name[opt]
|
||||
setattr (object, attr, val)
|
||||
_option_order.append ((opt, val))
|
||||
|
||||
# for opts
|
||||
|
||||
return args
|
||||
|
||||
# fancy_getopt()
|
||||
|
||||
|
||||
def fancy_getopt (options, negative_opt, object, args):
|
||||
parser = FancyGetopt (options)
|
||||
parser.set_negative_aliases (negative_opt)
|
||||
|
|
Loading…
Reference in New Issue