Fix nonsensical name.
The code used “long” to refer to a long option (e.g. --quiet), which was probably changed by 2to3 and not caught by the human operator, and then changed to “integer” by me to avoid shadowing without seeing the real obvious fix.
This commit is contained in:
parent
b4fefc8fa2
commit
a94bdee2e4
|
@ -142,20 +142,20 @@ class FancyGetopt:
|
||||||
|
|
||||||
for option in self.option_table:
|
for option in self.option_table:
|
||||||
if len(option) == 3:
|
if len(option) == 3:
|
||||||
integer, short, help = option
|
longopt, short, help = option
|
||||||
repeat = 0
|
repeat = 0
|
||||||
elif len(option) == 4:
|
elif len(option) == 4:
|
||||||
integer, short, help, repeat = option
|
longopt, short, help, repeat = option
|
||||||
else:
|
else:
|
||||||
# the option table is part of the code, so simply
|
# the option table is part of the code, so simply
|
||||||
# assert that it is correct
|
# assert that it is correct
|
||||||
raise ValueError("invalid option tuple: %r" % option)
|
raise ValueError("invalid option tuple: %r" % option)
|
||||||
|
|
||||||
# Type- and value-check the option names
|
# Type- and value-check the option names
|
||||||
if not isinstance(integer, str) or len(integer) < 2:
|
if not isinstance(longopt, str) or len(longopt) < 2:
|
||||||
raise PackagingGetoptError(
|
raise PackagingGetoptError(
|
||||||
("invalid long option '%s': "
|
("invalid long option '%s': "
|
||||||
"must be a string of length >= 2") % integer)
|
"must be a string of length >= 2") % longopt)
|
||||||
|
|
||||||
if (not ((short is None) or
|
if (not ((short is None) or
|
||||||
(isinstance(short, str) and len(short) == 1))):
|
(isinstance(short, str) and len(short) == 1))):
|
||||||
|
@ -163,55 +163,55 @@ class FancyGetopt:
|
||||||
("invalid short option '%s': "
|
("invalid short option '%s': "
|
||||||
"must be a single character or None") % short)
|
"must be a single character or None") % short)
|
||||||
|
|
||||||
self.repeat[integer] = repeat
|
self.repeat[longopt] = repeat
|
||||||
self.long_opts.append(integer)
|
self.long_opts.append(longopt)
|
||||||
|
|
||||||
if integer[-1] == '=': # option takes an argument?
|
if longopt[-1] == '=': # option takes an argument?
|
||||||
if short:
|
if short:
|
||||||
short = short + ':'
|
short = short + ':'
|
||||||
integer = integer[0:-1]
|
longopt = longopt[0:-1]
|
||||||
self.takes_arg[integer] = 1
|
self.takes_arg[longopt] = 1
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# Is option is a "negative alias" for some other option (eg.
|
# Is option is a "negative alias" for some other option (eg.
|
||||||
# "quiet" == "!verbose")?
|
# "quiet" == "!verbose")?
|
||||||
alias_to = self.negative_alias.get(integer)
|
alias_to = self.negative_alias.get(longopt)
|
||||||
if alias_to is not None:
|
if alias_to is not None:
|
||||||
if self.takes_arg[alias_to]:
|
if self.takes_arg[alias_to]:
|
||||||
raise PackagingGetoptError(
|
raise PackagingGetoptError(
|
||||||
("invalid negative alias '%s': "
|
("invalid negative alias '%s': "
|
||||||
"aliased option '%s' takes a value") % \
|
"aliased option '%s' takes a value") % \
|
||||||
(integer, alias_to))
|
(longopt, alias_to))
|
||||||
|
|
||||||
self.long_opts[-1] = integer # XXX redundant?!
|
self.long_opts[-1] = longopt # XXX redundant?!
|
||||||
self.takes_arg[integer] = 0
|
self.takes_arg[longopt] = 0
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.takes_arg[integer] = 0
|
self.takes_arg[longopt] = 0
|
||||||
|
|
||||||
# If this is an alias option, make sure its "takes arg" flag is
|
# If this is an alias option, make sure its "takes arg" flag is
|
||||||
# the same as the option it's aliased to.
|
# the same as the option it's aliased to.
|
||||||
alias_to = self.alias.get(integer)
|
alias_to = self.alias.get(longopt)
|
||||||
if alias_to is not None:
|
if alias_to is not None:
|
||||||
if self.takes_arg[integer] != self.takes_arg[alias_to]:
|
if self.takes_arg[longopt] != self.takes_arg[alias_to]:
|
||||||
raise PackagingGetoptError(
|
raise PackagingGetoptError(
|
||||||
("invalid alias '%s': inconsistent with "
|
("invalid alias '%s': inconsistent with "
|
||||||
"aliased option '%s' (one of them takes a value, "
|
"aliased option '%s' (one of them takes a value, "
|
||||||
"the other doesn't") % (integer, alias_to))
|
"the other doesn't") % (longopt, alias_to))
|
||||||
|
|
||||||
# Now enforce some bondage on the long option name, so we can
|
# Now enforce some bondage on the long option name, so we can
|
||||||
# later translate it to an attribute name on some object. Have
|
# later translate it to an attribute name on some object. Have
|
||||||
# to do this a bit late to make sure we've removed any trailing
|
# to do this a bit late to make sure we've removed any trailing
|
||||||
# '='.
|
# '='.
|
||||||
if not longopt_re.match(integer):
|
if not longopt_re.match(longopt):
|
||||||
raise PackagingGetoptError(
|
raise PackagingGetoptError(
|
||||||
("invalid long option name '%s' " +
|
("invalid long option name '%s' " +
|
||||||
"(must be letters, numbers, hyphens only") % integer)
|
"(must be letters, numbers, hyphens only") % longopt)
|
||||||
|
|
||||||
self.attr_name[integer] = integer.replace('-', '_')
|
self.attr_name[longopt] = longopt.replace('-', '_')
|
||||||
if short:
|
if short:
|
||||||
self.short_opts.append(short)
|
self.short_opts.append(short)
|
||||||
self.short2long[short[0]] = integer
|
self.short2long[short[0]] = longopt
|
||||||
|
|
||||||
def getopt(self, args=None, object=None):
|
def getopt(self, args=None, object=None):
|
||||||
"""Parse command-line options in args. Store as attributes on object.
|
"""Parse command-line options in args. Store as attributes on object.
|
||||||
|
@ -297,10 +297,10 @@ class FancyGetopt:
|
||||||
# First pass: determine maximum length of long option names
|
# First pass: determine maximum length of long option names
|
||||||
max_opt = 0
|
max_opt = 0
|
||||||
for option in self.option_table:
|
for option in self.option_table:
|
||||||
integer = option[0]
|
longopt = option[0]
|
||||||
short = option[1]
|
short = option[1]
|
||||||
l = len(integer)
|
l = len(longopt)
|
||||||
if integer[-1] == '=':
|
if longopt[-1] == '=':
|
||||||
l = l - 1
|
l = l - 1
|
||||||
if short is not None:
|
if short is not None:
|
||||||
l = l + 5 # " (-x)" where short == 'x'
|
l = l + 5 # " (-x)" where short == 'x'
|
||||||
|
@ -340,20 +340,20 @@ class FancyGetopt:
|
||||||
lines = ['Option summary:']
|
lines = ['Option summary:']
|
||||||
|
|
||||||
for option in self.option_table:
|
for option in self.option_table:
|
||||||
integer, short, help = option[:3]
|
longopt, short, help = option[:3]
|
||||||
text = textwrap.wrap(help, text_width)
|
text = textwrap.wrap(help, text_width)
|
||||||
|
|
||||||
# Case 1: no short option at all (makes life easy)
|
# Case 1: no short option at all (makes life easy)
|
||||||
if short is None:
|
if short is None:
|
||||||
if text:
|
if text:
|
||||||
lines.append(" --%-*s %s" % (max_opt, integer, text[0]))
|
lines.append(" --%-*s %s" % (max_opt, longopt, text[0]))
|
||||||
else:
|
else:
|
||||||
lines.append(" --%-*s " % (max_opt, integer))
|
lines.append(" --%-*s " % (max_opt, longopt))
|
||||||
|
|
||||||
# Case 2: we have a short option, so we have to include it
|
# Case 2: we have a short option, so we have to include it
|
||||||
# just after the long option
|
# just after the long option
|
||||||
else:
|
else:
|
||||||
opt_names = "%s (-%s)" % (integer, short)
|
opt_names = "%s (-%s)" % (longopt, short)
|
||||||
if text:
|
if text:
|
||||||
lines.append(" --%-*s %s" %
|
lines.append(" --%-*s %s" %
|
||||||
(max_opt, opt_names, text[0]))
|
(max_opt, opt_names, text[0]))
|
||||||
|
|
Loading…
Reference in New Issue