tyro._backends._argparse

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.)

Module Contents

tyro._backends._argparse.SUPPRESS = '==SUPPRESS=='
tyro._backends._argparse.OPTIONAL = '?'
tyro._backends._argparse.ZERO_OR_MORE = '*'
tyro._backends._argparse.ONE_OR_MORE = '+'
tyro._backends._argparse.PARSER = 'A...'
tyro._backends._argparse.REMAINDER = '...'
class tyro._backends._argparse.HelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: object

Formatter for generating usage messages and argument help strings.

Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail.

start_section(heading)[source]
end_section()[source]
add_text(text)[source]
add_usage(usage, actions, groups, prefix=None)[source]
add_argument(action)[source]
add_arguments(actions)[source]
format_help()[source]
class tyro._backends._argparse.RawDescriptionHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: HelpFormatter

Help message formatter which retains any formatting in descriptions.

Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail.

class tyro._backends._argparse.RawTextHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: 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 provided by the class are considered an implementation detail.

class tyro._backends._argparse.ArgumentDefaultsHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: 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 provided by the class are considered an implementation detail.

class tyro._backends._argparse.MetavarTypeHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: HelpFormatter

Help message formatter which uses the argument ‘type’ as the default metavar value (instead of the argument ‘dest’)

Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail.

class tyro._backends._argparse.Action(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]

Bases: _AttributeHolder

Information about how to convert command line strings to Python objects.

Action objects are used by an ArgumentParser to represent the information needed to parse a single argument from one or more strings from the command line. The keyword arguments to the Action constructor are also all attributes of Action instances.

Keyword Arguments:
  • which (- option_strings -- A list of command-line option strings) – should be associated with this action.

  • object (- dest -- The name of the attribute to hold the created)

  • be (- nargs -- The number of command-line arguments that should) –

    consumed. By default, one argument will be consumed and a single value will be produced. Other values include:

    • N (an integer) consumes N arguments (and produces a list)

    • ’?’ consumes zero or one arguments

    • ’*’ consumes zero or more arguments (and produces a list)

    • ’+’ consumes one or more arguments (and produces a list)

    Note that the difference between the default and nargs=1 is that with the default, a single value will be produced, while with nargs=1, a list containing a single value will be produced.

  • the (- metavar -- The name to be used for the option's argument with) – option uses an action that takes no values.

  • specified. (- default -- The value to be produced if the option is not)

  • and (- type -- A callable that accepts a single string argument,) – returns the converted value. The standard Python types str, int, float, and complex are useful examples of such callables. If None, str is used.

  • None, (- choices -- A container of values that should be allowed. If not) – after a command-line argument has been converted to the appropriate type, an exception will be raised if it is not a member of this collection.

  • the – command line. This is only meaningful for optional command-line arguments.

  • argument. (- help -- The help string describing the)

  • the – help string. If None, the ‘dest’ value will be used as the name.

format_usage()[source]
abstract __call__(parser, namespace, values, option_string=None)[source]
class tyro._backends._argparse.BooleanOptionalAction(option_strings, dest, default=None, type=_deprecated_default, choices=_deprecated_default, required=False, help=None, metavar=_deprecated_default)[source]

Bases: Action

Information about how to convert command line strings to Python objects.

Action objects are used by an ArgumentParser to represent the information needed to parse a single argument from one or more strings from the command line. The keyword arguments to the Action constructor are also all attributes of Action instances.

Keyword Arguments:
  • which (- option_strings -- A list of command-line option strings) – should be associated with this action.

  • object (- dest -- The name of the attribute to hold the created)

  • be (- nargs -- The number of command-line arguments that should) –

    consumed. By default, one argument will be consumed and a single value will be produced. Other values include:

    • N (an integer) consumes N arguments (and produces a list)

    • ’?’ consumes zero or one arguments

    • ’*’ consumes zero or more arguments (and produces a list)

    • ’+’ consumes one or more arguments (and produces a list)

    Note that the difference between the default and nargs=1 is that with the default, a single value will be produced, while with nargs=1, a list containing a single value will be produced.

  • the (- metavar -- The name to be used for the option's argument with) – option uses an action that takes no values.

  • specified. (- default -- The value to be produced if the option is not)

  • and (- type -- A callable that accepts a single string argument,) – returns the converted value. The standard Python types str, int, float, and complex are useful examples of such callables. If None, str is used.

  • None, (- choices -- A container of values that should be allowed. If not) – after a command-line argument has been converted to the appropriate type, an exception will be raised if it is not a member of this collection.

  • the – command line. This is only meaningful for optional command-line arguments.

  • argument. (- help -- The help string describing the)

  • the – help string. If None, the ‘dest’ value will be used as the name.

__call__(parser, namespace, values, option_string=None)[source]
format_usage()[source]
class tyro._backends._argparse.FileType(mode='r', bufsize=-1, encoding=None, errors=None)[source]

Bases: object

Factory for creating file object types

Instances of FileType are typically passed as type= arguments to the ArgumentParser add_argument() method.

Keyword Arguments:
  • the (- encoding -- The file's encoding. Accepts the same values as) – same values as the builtin open() function.

  • as (- bufsize -- The file's desired buffer size. Accepts the same values) – the builtin open() function.

  • the – builtin open() function.

  • to (- errors -- A string indicating how encoding and decoding errors are) – be handled. Accepts the same value as the builtin open() function.

__call__(string)[source]
__repr__()[source]

Return repr(self).

class tyro._backends._argparse.Namespace(**kwargs)[source]

Bases: _AttributeHolder

Simple object for storing attributes.

Implements equality by attribute names and values, and provides a simple string representation.

__eq__(other)[source]

Return self==value.

__contains__(key)[source]
class tyro._backends._argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)[source]

Bases: _AttributeHolder, _ActionsContainer

Object for parsing command line strings into Python objects.

Keyword Arguments:
  • (default (- usage -- A usage message) – os.path.basename(sys.argv[0]))

  • (default – auto-generated from arguments)

  • does (- description -- A description of what the program)

  • descriptions (- epilog -- Text following the argument)

  • one (- parents -- Parsers whose arguments should be copied into this)

  • messages (- formatter_class -- HelpFormatter class for printing help)

  • arguments (- argument_default -- The default value for all)

  • containing (- fromfile_prefix_chars -- Characters that prefix files) – additional arguments

  • arguments

  • conflicts (- conflict_handler -- String indicating how to handle)

  • option (- add_help -- Add a -h/-help)

  • unambiguously (- allow_abbrev -- Allow long options to be abbreviated)

  • with (- exit_on_error -- Determines whether or not ArgumentParser exits) – error info when an error occurs

add_subparsers(**kwargs)[source]
parse_args(args=None, namespace=None)[source]
parse_known_args(args=None, namespace=None)[source]
convert_arg_line_to_args(arg_line)[source]
parse_intermixed_args(args=None, namespace=None)[source]
parse_known_intermixed_args(args=None, namespace=None)[source]
format_usage()[source]
format_help()[source]
print_usage(file=None)[source]
print_help(file=None)[source]
exit(status=0, message=None)[source]
error(message)[source]

error(message: string)

Prints a usage message incorporating the message to stderr and exits.

If you override this in a subclass, it should not return – it should either exit or raise an exception.