module documentation

Command-line parsing library

This module is an optparse-inspired command-line parsing library that:

  • handles both optional and positional arguments
  • produces highly informative usage messages
  • supports parsers that dispatch to sub-parsers

The following is a simple usage example that sums integers from the command-line and writes the result to a file:

parser = argparse.ArgumentParser(
    description='sum the integers at the command line')
parser.add_argument(
    'integers', metavar='int', nargs='+', type=int,
    help='an integer to be summed')
parser.add_argument(
    '--log', default=sys.stdout, type=argparse.FileType('w'),
    help='the file where the sum should be written')
args = parser.parse_args()
args.log.write('%s' % sum(args.integers))
args.log.close()

The module contains the following public classes:

  • ArgumentParser -- The main entry point for command-line parsing. As the
    example above shows, the add_argument() method is used to populate the parser with actions for optional and positional arguments. Then the parse_args() method is invoked to convert the args at the command-line into an object with attributes.
  • ArgumentError -- The exception raised by ArgumentParser objects when
    there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages.
  • FileType -- A factory for defining types of files to be created. As the
    example above shows, instances of FileType are typically passed as the type= argument of add_argument() calls.
  • Action -- The base class for parser actions. Typically actions are
    selected by passing strings like 'store_true' or 'append_const' to the action= argument of add_argument(). However, for greater customization of ArgumentParser actions, subclasses of Action may be defined and passed as the action= argument.
  • HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
    ArgumentDefaultsHelpFormatter -- Formatter classes which may be passed as the formatter_class= argument to the ArgumentParser constructor. HelpFormatter is the default, RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser not to change the formatting for help text, and ArgumentDefaultsHelpFormatter adds information about argument defaults to the help.

All other classes in this module are considered implementation details. (Also note that HelpFormatter and RawDescriptionHelpFormatter are only considered public as object names -- the API of the formatter objects is still considered an implementation detail.)

Class Action Information about how to convert command line strings to Python objects.
Class ArgumentDefaultsHelpFormatter Help message formatter which adds default values to argument help.
Class ArgumentParser Object for parsing command line strings into Python objects.
Class BooleanOptionalAction Undocumented
Class FileType Factory for creating file object types
Class HelpFormatter Formatter for generating usage messages and argument help strings.
Class MetavarTypeHelpFormatter Help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest')
Class Namespace Simple object for storing attributes.
Class RawDescriptionHelpFormatter Help message formatter which retains any formatting in descriptions.
Class RawTextHelpFormatter Help message formatter which retains formatting of all help text.
Exception ArgumentError An error from creating or using an argument (optional or positional).
Exception ArgumentTypeError An error from trying to convert a command line string to a type.
Constant ONE_OR_MORE Undocumented
Constant OPTIONAL Undocumented
Constant PARSER Undocumented
Constant REMAINDER Undocumented
Constant SUPPRESS Undocumented
Constant ZERO_OR_MORE Undocumented
Variable __version__ Undocumented
Class _ActionsContainer No class docstring; 0/12 instance variable, 1/18 method documented
Class _AppendAction Undocumented
Class _AppendConstAction Undocumented
Class _ArgumentGroup Undocumented
Class _AttributeHolder Abstract base class that provides __repr__.
Class _CountAction Undocumented
Class _ExtendAction Undocumented
Class _HelpAction Undocumented
Class _MutuallyExclusiveGroup Undocumented
Class _StoreAction Undocumented
Class _StoreConstAction Undocumented
Class _StoreFalseAction Undocumented
Class _StoreTrueAction Undocumented
Class _SubParsersAction Undocumented
Class _VersionAction Undocumented
Function _copy_items Undocumented
Function _get_action_name Undocumented
Constant _UNRECOGNIZED_ARGS_ATTR Undocumented
ONE_OR_MORE: str = (source)

Undocumented

Value
'+'
OPTIONAL: str = (source)

Undocumented

Value
'?'

Undocumented

Value
'A...'
REMAINDER: str = (source)

Undocumented

Value
'...'
SUPPRESS: str = (source)

Undocumented

Value
'==SUPPRESS=='
ZERO_OR_MORE: str = (source)

Undocumented

Value
'*'
__version__: str = (source)

Undocumented

def _copy_items(items): (source)

Undocumented

def _get_action_name(argument): (source)

Undocumented

_UNRECOGNIZED_ARGS_ATTR: str = (source)

Undocumented

Value
'_unrecognized_args'