PyDocs

Nav

Command-line interface libraries

ArgumentParser

  • ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True, *, suggest_on_error=True, color=True)
Object for parsing command line strings into Python objects.

Keyword Arguments:
    - prog -- The name of the program (default:
        ``os.path.basename(sys.argv[0])``)
    - usage -- A usage message (default: auto-generated from arguments)
    - description -- A description of what the program does
    - epilog -- Text following the argument descriptions
    - parents -- Parsers whose arguments should be copied into this one
    - formatter_class -- HelpFormatter class for printing help messages
    - prefix_chars -- Characters that prefix optional arguments
    - fromfile_prefix_chars -- Characters that prefix files containing
        additional arguments
    - argument_default -- The default value for all arguments
    - conflict_handler -- String indicating how to handle conflicts
    - add_help -- Add a -h/-help option
    - allow_abbrev -- Allow long options to be abbreviated unambiguously
    - exit_on_error -- Determines whether or not ArgumentParser exits with
        error info when an error occurs
    - suggest_on_error - Enables suggestions for mistyped argument choices
        and subparser names (default: ``True``)
    - color - Allow color output in help messages (default: ``False``)
Changed in 3.5, 3.8, 3.9, 3.14, 3.15
  • 3.5: allow_abbrev parameter was added.
  • 3.8: In previous versions, allow_abbrev also disabled grouping of short flags such as -vv to mean -v -v.
  • 3.9: exit_on_error parameter was added.
  • 3.14: suggest_on_error and color parameters were added.
  • 3.15: suggest_on_error default changed to True.
Members of ArgumentParser

a

add_argument
add_argument(option_string, option_string, ..., name=value, ...)
add_argument_group
By default, ArgumentParser groups command-line arguments into “positional arguments” and “options” when displaying help messages.
add_mutually_exclusive_group
Create a mutually exclusive group.
add_subparsers
Many programs split up their functionality into a number of subcommands, for example, the svn program can invoke subcommands like svn checkout, svn update, and svn commit.

c

convert_arg_line_to_args
Arguments that are read from a file (see the fromfile_prefix_chars keyword argument to the ArgumentParser constructor) are read one argument per line.

e

error
Prints a usage message incorporating the message to stderr and exits.
exit
This method terminates the program, exiting with the specified status and, if given, it prints a message to sys.stderr before that.

f

format_help
Return a string containing a help message, including the program usage and information about the arguments registered with the ArgumentParser.
format_usage
Return a string containing a brief description of how the ArgumentParser should be invoked on the command line.

g

get_default
Get the default value for a namespace attribute, as set by either add_argument() or by set_defaults():

p

parse_args
Convert argument strings to objects and assign them as attributes of the namespace.
parse_intermixed_args
A number of Unix commands allow the user to intermix optional arguments with positional arguments.
parse_known_args
Sometimes a script only needs to handle a specific set of command-line arguments, leaving any unrecognized arguments for another script or program.
parse_known_intermixed_args
A number of Unix commands allow the user to intermix optional arguments with positional arguments.
print_help
Print a help message, including the program usage and information about the arguments registered with the ArgumentParser.
print_usage
Print a brief description of how the ArgumentParser should be invoked on the command line.

r

register
Sometimes it’s desirable to use a custom string in error messages to provide more user-friendly output.

s

set_defaults
Most of the time, the attributes of the object returned by parse_args() will be fully determined by inspecting the command-line arguments and the argument actions.
1 Example
>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
from the Python docs
See Also
No see-alsos for argparse/ArgumentParser
0 Notes
No notes for ArgumentParser