option.process(opt, value, values, self)
def _process_short_opts(self, rargs, values):
option = self._short_opt.get(opt)
i += 1 # we have consumed a character
raise BadOptionError(opt)
# Any characters left in arg? Pretend they're the
# next arg, and stop consuming characters of arg.
"%(option)s option requires %(number)d argument",
"%(option)s option requires %(number)d arguments",
nargs) % {"option": opt, "number": nargs})
value = tuple(rargs[0:nargs])
else: # option doesn't take a value
option.process(opt, value, values, self)
# -- Feedback methods ----------------------------------------------
return os.path.basename(sys.argv[0])
def expand_prog_name(self, s):
return s.replace("%prog", self.get_prog_name())
def get_description(self):
return self.expand_prog_name(self.description)
def exit(self, status=0, msg=None):
Print a usage message incorporating 'msg' to stderr and exit.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
self.print_usage(sys.stderr)
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
return self.formatter.format_usage(
self.expand_prog_name(self.usage))
def print_usage(self, file=None):
"""print_usage(file : file = stdout)
Print the usage message for the current program (self.usage) to
'file' (default stdout). Any occurrence of the string "%prog" in
self.usage is replaced with the name of the current program
(basename of sys.argv[0]). Does nothing if self.usage is empty
print(self.get_usage(), file=file)
return self.expand_prog_name(self.version)
def print_version(self, file=None):
"""print_version(file : file = stdout)
Print the version message for this program (self.version) to
'file' (default stdout). As with print_usage(), any occurrence
of "%prog" in self.version is replaced by the current program's
name. Does nothing if self.version is empty or undefined.
print(self.get_version(), file=file)
def format_option_help(self, formatter=None):
formatter = self.formatter
formatter.store_option_strings(self)
result.append(formatter.format_heading(_("Options")))
result.append(OptionContainer.format_option_help(self, formatter))
for group in self.option_groups:
result.append(group.format_help(formatter))
# Drop the last "\n", or the header if no options or option groups:
return "".join(result[:-1])
def format_epilog(self, formatter):
return formatter.format_epilog(self.epilog)
def format_help(self, formatter=None):
formatter = self.formatter
result.append(self.get_usage() + "\n")
result.append(self.format_description(formatter) + "\n")
result.append(self.format_option_help(formatter))
result.append(self.format_epilog(formatter))
def print_help(self, file=None):
"""print_help(file : file = stdout)
Print an extended help message, listing all options and any
help text provided with them, to 'file' (default stdout).
file.write(self.format_help())
def _match_abbrev(s, wordmap):
"""_match_abbrev(s : string, wordmap : {string : Option}) -> string
Return the string key in 'wordmap' for which 's' is an unambiguous
abbreviation. If 's' is found to be ambiguous or doesn't match any of
'words', raise BadOptionError.
# Is there an exact match?
# Isolate all words with s as a prefix.
possibilities = [word for word in wordmap.keys()
# No exact match, so there had better be just one possibility.
if len(possibilities) == 1:
# More than one possible completion: ambiguous prefix.
raise AmbiguousOptionError(s, possibilities)
# Some day, there might be many Option classes. As of Optik 1.3, the
# preferred way to instantiate Options is indirectly, via make_option(),
# which will become a factory function when there are many Option