|
|
|
@ -107,8 +107,7 @@ try:
|
|
|
|
|
except NameError:
|
|
|
|
|
# for python < 2.4 compatibility:
|
|
|
|
|
def sorted(iterable, reverse=False):
|
|
|
|
|
result = list(iterable)
|
|
|
|
|
result.sort()
|
|
|
|
|
result = sorted(iterable)
|
|
|
|
|
if reverse:
|
|
|
|
|
result.reverse()
|
|
|
|
|
return result
|
|
|
|
@ -131,7 +130,9 @@ _UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
|
|
|
|
|
# Utility functions and classes
|
|
|
|
|
# =============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _AttributeHolder(object):
|
|
|
|
|
|
|
|
|
|
"""Abstract base class that provides __repr__.
|
|
|
|
|
|
|
|
|
|
The __repr__ method returns a string in the format::
|
|
|
|
@ -167,6 +168,7 @@ def _ensure_value(namespace, name, value):
|
|
|
|
|
# ===============
|
|
|
|
|
|
|
|
|
|
class HelpFormatter(object):
|
|
|
|
|
|
|
|
|
|
"""Formatter for generating usage messages and argument help strings.
|
|
|
|
|
|
|
|
|
|
Only the name of this class is considered a public API. All the methods
|
|
|
|
@ -647,6 +649,7 @@ class HelpFormatter(object):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RawDescriptionHelpFormatter(HelpFormatter):
|
|
|
|
|
|
|
|
|
|
"""Help message formatter which retains any formatting in descriptions.
|
|
|
|
|
|
|
|
|
|
Only the name of this class is considered a public API. All the methods
|
|
|
|
@ -658,6 +661,7 @@ class RawDescriptionHelpFormatter(HelpFormatter):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RawTextHelpFormatter(RawDescriptionHelpFormatter):
|
|
|
|
|
|
|
|
|
|
"""Help message formatter which retains formatting of all help text.
|
|
|
|
|
|
|
|
|
|
Only the name of this class is considered a public API. All the methods
|
|
|
|
@ -669,6 +673,7 @@ class RawTextHelpFormatter(RawDescriptionHelpFormatter):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgumentDefaultsHelpFormatter(HelpFormatter):
|
|
|
|
|
|
|
|
|
|
"""Help message formatter which adds default values to argument help.
|
|
|
|
|
|
|
|
|
|
Only the name of this class is considered a public API. All the methods
|
|
|
|
@ -703,6 +708,7 @@ def _get_action_name(argument):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgumentError(Exception):
|
|
|
|
|
|
|
|
|
|
"""An error from creating or using an argument (optional or positional).
|
|
|
|
|
|
|
|
|
|
The string value of this exception is the message, augmented with
|
|
|
|
@ -723,6 +729,7 @@ class ArgumentError(Exception):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgumentTypeError(Exception):
|
|
|
|
|
|
|
|
|
|
"""An error from trying to convert a command line string to a type."""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
@ -732,6 +739,7 @@ class ArgumentTypeError(Exception):
|
|
|
|
|
# ==============
|
|
|
|
|
|
|
|
|
|
class Action(_AttributeHolder):
|
|
|
|
|
|
|
|
|
|
"""Information about how to convert command line strings to Python objects.
|
|
|
|
|
|
|
|
|
|
Action objects are used by an ArgumentParser to represent the information
|
|
|
|
@ -1109,7 +1117,8 @@ class _SubParsersAction(Action):
|
|
|
|
|
# parse all the remaining options into the namespace
|
|
|
|
|
# store any unrecognized options on the object, so that the top
|
|
|
|
|
# level parser can decide what to do with them
|
|
|
|
|
namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
|
|
|
|
|
namespace, arg_strings = parser.parse_known_args(
|
|
|
|
|
arg_strings, namespace)
|
|
|
|
|
if arg_strings:
|
|
|
|
|
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
|
|
|
|
|
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
|
|
|
|
@ -1120,6 +1129,7 @@ class _SubParsersAction(Action):
|
|
|
|
|
# ==============
|
|
|
|
|
|
|
|
|
|
class FileType(object):
|
|
|
|
|
|
|
|
|
|
"""Factory for creating file object types
|
|
|
|
|
|
|
|
|
|
Instances of FileType are typically passed as type= arguments to the
|
|
|
|
@ -1162,7 +1172,9 @@ class FileType(object):
|
|
|
|
|
# Optional and Positional Parsing
|
|
|
|
|
# ===========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Namespace(_AttributeHolder):
|
|
|
|
|
|
|
|
|
|
"""Simple object for storing attributes.
|
|
|
|
|
|
|
|
|
|
Implements equality by attribute names and values, and provides a simple
|
|
|
|
@ -1264,7 +1276,6 @@ class _ActionsContainer(object):
|
|
|
|
|
return action.default
|
|
|
|
|
return self._defaults.get(dest, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =======================
|
|
|
|
|
# Adding argument actions
|
|
|
|
|
# =======================
|
|
|
|
@ -1536,6 +1547,7 @@ class _MutuallyExclusiveGroup(_ArgumentGroup):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|
|
|
|
|
|
|
|
|
"""Object for parsing command line strings into Python objects.
|
|
|
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
@ -1611,12 +1623,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|
|
|
|
default_prefix = prefix_chars[0]
|
|
|
|
|
if self.add_help:
|
|
|
|
|
self.add_argument(
|
|
|
|
|
default_prefix+'h', default_prefix*2+'help',
|
|
|
|
|
default_prefix + 'h', default_prefix * 2 + 'help',
|
|
|
|
|
action='help', default=SUPPRESS,
|
|
|
|
|
help=_('show this help message and exit'))
|
|
|
|
|
if self.version:
|
|
|
|
|
self.add_argument(
|
|
|
|
|
default_prefix+'v', default_prefix*2+'version',
|
|
|
|
|
default_prefix + 'v', default_prefix * 2 + 'version',
|
|
|
|
|
action='version', default=SUPPRESS,
|
|
|
|
|
version=self.version,
|
|
|
|
|
help=_("show program's version number and exit"))
|
|
|
|
@ -2075,8 +2087,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|
|
|
|
|
|
|
|
|
# if multiple actions match, the option string was ambiguous
|
|
|
|
|
if len(option_tuples) > 1:
|
|
|
|
|
options = ', '.join([option_string
|
|
|
|
|
for action, option_string, explicit_arg in option_tuples])
|
|
|
|
|
options = ', '.join(
|
|
|
|
|
[option_string for action, option_string, explicit_arg in option_tuples])
|
|
|
|
|
tup = arg_string, options
|
|
|
|
|
self.error(_('ambiguous option: %s could match %s') % tup)
|
|
|
|
|
|
|
|
|
|