| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | :mod:`!argparse` --- Parser for command-line options, arguments and subcommands
 | 
					
						
							| 
									
										
										
										
											2024-05-08 15:34:40 -04:00
										 |  |  | ================================================================================
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: argparse
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  |    :synopsis: Command-line option and argument parsing library.
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com>
 | 
					
						
							|  |  |  | .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-27 01:20:32 +00:00
										 |  |  | .. versionadded:: 3.2
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-19 00:49:18 +02:00
										 |  |  | **Source code:** :source:`Lib/argparse.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 14:17:19 +10:00
										 |  |  | .. note::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    While :mod:`argparse` is the default recommended standard library module
 | 
					
						
							|  |  |  |    for implementing basic command line applications, authors with more
 | 
					
						
							|  |  |  |    exacting requirements for exactly how their command line applications
 | 
					
						
							|  |  |  |    behave may find it doesn't provide the necessary level of control.
 | 
					
						
							|  |  |  |    Refer to :ref:`choosing-an-argument-parser` for alternatives to
 | 
					
						
							|  |  |  |    consider when ``argparse`` doesn't support behaviors that the application
 | 
					
						
							|  |  |  |    requires (such as entirely disabling support for interspersed options and
 | 
					
						
							|  |  |  |    positional arguments, or accepting option parameter values that start
 | 
					
						
							|  |  |  |    with ``-`` even when they correspond to another defined option).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-27 01:20:32 +00:00
										 |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-06 16:15:35 +03:00
										 |  |  | .. sidebar:: Tutorial
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This page contains the API reference information. For a more gentle
 | 
					
						
							|  |  |  |    introduction to Python command-line parsing, have a look at the
 | 
					
						
							|  |  |  |    :ref:`argparse tutorial <argparse-tutorial>`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | The :mod:`!argparse` module makes it easy to write user-friendly command-line
 | 
					
						
							|  |  |  | interfaces. The program defines what arguments it requires, and :mod:`!argparse`
 | 
					
						
							|  |  |  | will figure out how to parse those out of :data:`sys.argv`.  The :mod:`!argparse`
 | 
					
						
							| 
									
										
										
										
											2022-05-01 02:20:10 +04:00
										 |  |  | module also automatically generates help and usage messages.  The module
 | 
					
						
							|  |  |  | will also issue errors when users give the program invalid arguments.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | The :mod:`!argparse` module's support for command-line interfaces is built
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  | around an instance of :class:`argparse.ArgumentParser`.  It is a container for
 | 
					
						
							| 
									
										
										
										
											2023-02-05 01:45:07 -08:00
										 |  |  | argument specifications and has options that apply to the parser as whole::
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  |    parser = argparse.ArgumentParser(
 | 
					
						
							| 
									
										
										
										
											2023-03-08 08:25:28 +01:00
										 |  |  |                        prog='ProgramName',
 | 
					
						
							|  |  |  |                        description='What the program does',
 | 
					
						
							|  |  |  |                        epilog='Text at the bottom of help')
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 16:02:47 -05:00
										 |  |  | The :meth:`ArgumentParser.add_argument` method attaches individual argument
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  | specifications to the parser.  It supports positional arguments, options that
 | 
					
						
							|  |  |  | accept values, and on/off flags::
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  |    parser.add_argument('filename')           # positional argument
 | 
					
						
							| 
									
										
										
										
											2022-04-20 16:02:47 -05:00
										 |  |  |    parser.add_argument('-c', '--count')      # option that takes a value
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  |    parser.add_argument('-v', '--verbose',
 | 
					
						
							|  |  |  |                        action='store_true')  # on/off flag
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 16:02:47 -05:00
										 |  |  | The :meth:`ArgumentParser.parse_args` method runs the parser and places
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  | the extracted data in a :class:`argparse.Namespace` object::
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 01:21:54 -05:00
										 |  |  |    args = parser.parse_args()
 | 
					
						
							|  |  |  |    print(args.filename, args.count, args.verbose)
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | .. note::
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    If you're looking for a guide about how to upgrade :mod:`optparse` code
 | 
					
						
							|  |  |  |    to :mod:`!argparse`, see :ref:`Upgrading Optparse Code <upgrading-optparse-code>`.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ArgumentParser objects
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-14 06:32:35 +03:00
										 |  |  | .. class:: 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', \
 | 
					
						
							| 
									
										
										
										
											2024-10-17 00:07:37 -07:00
										 |  |  |                           add_help=True, allow_abbrev=True, exit_on_error=True, \
 | 
					
						
							|  |  |  |                           suggest_on_error=False)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    Create a new :class:`ArgumentParser` object. All parameters should be passed
 | 
					
						
							|  |  |  |    as keyword arguments. Each parameter has its own more detailed description
 | 
					
						
							|  |  |  |    below, but in short they are:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 22:51:40 +03:00
										 |  |  |    * prog_ - The name of the program (default: generated from the ``__main__``
 | 
					
						
							|  |  |  |      module attributes and ``sys.argv[0]``)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    * usage_ - The string describing the program usage (default: generated from
 | 
					
						
							|  |  |  |      arguments added to parser)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-28 07:15:39 -05:00
										 |  |  |    * description_ - Text to display before the argument help
 | 
					
						
							|  |  |  |      (by default, no text)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-28 07:15:39 -05:00
										 |  |  |    * epilog_ - Text to display after the argument help (by default, no text)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    * parents_ - A list of :class:`ArgumentParser` objects whose arguments should
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |      also be included
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * formatter_class_ - A class for customizing the help output
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    * prefix_chars_ - The set of characters that prefix optional arguments
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      (default: '-')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * fromfile_prefix_chars_ - The set of characters that prefix files from
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |      which additional arguments should be read (default: ``None``)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    * argument_default_ - The global default value for arguments
 | 
					
						
							|  |  |  |      (default: ``None``)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    * conflict_handler_ - The strategy for resolving conflicting optionals
 | 
					
						
							|  |  |  |      (usually unnecessary)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 08:23:08 +00:00
										 |  |  |    * add_help_ - Add a ``-h/--help`` option to the parser (default: ``True``)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  |    * allow_abbrev_ - Allows long options to be abbreviated if the
 | 
					
						
							|  |  |  |      abbreviation is unambiguous. (default: ``True``)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    * exit_on_error_ - Determines whether or not :class:`!ArgumentParser` exits with
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  |      error info when an error occurs. (default: ``True``)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 00:07:37 -07:00
										 |  |  |    * suggest_on_error_ - Enables suggestions for mistyped argument choices
 | 
					
						
							|  |  |  |      and subparser names (default: ``False``)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  |    .. versionchanged:: 3.5
 | 
					
						
							|  |  |  |       *allow_abbrev* parameter was added.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-14 00:35:58 -05:00
										 |  |  |    .. versionchanged:: 3.8
 | 
					
						
							|  |  |  |       In previous versions, *allow_abbrev* also disabled grouping of short
 | 
					
						
							|  |  |  |       flags such as ``-vv`` to mean ``-v -v``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  |    .. versionchanged:: 3.9
 | 
					
						
							|  |  |  |       *exit_on_error* parameter was added.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | The following sections describe how each of these are used.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _prog:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | prog
 | 
					
						
							|  |  |  | ^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 22:51:40 +03:00
										 |  |  | By default, :class:`ArgumentParser` calculates the name of the program
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | to display in help messages depending on the way the Python interpreter was run:
 | 
					
						
							| 
									
										
										
										
											2024-10-01 22:51:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | * The :func:`base name <os.path.basename>` of ``sys.argv[0]`` if a file was
 | 
					
						
							|  |  |  |   passed as argument.
 | 
					
						
							|  |  |  | * The Python interpreter name followed by ``sys.argv[0]`` if a directory or
 | 
					
						
							|  |  |  |   a zipfile was passed as argument.
 | 
					
						
							|  |  |  | * The Python interpreter name followed by ``-m`` followed by the
 | 
					
						
							|  |  |  |   module or package name if the :option:`-m` option was used.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | This default is almost always desirable because it will make the help messages
 | 
					
						
							|  |  |  | match the string that was used to invoke the program on the command line.
 | 
					
						
							|  |  |  | However, to change this default behavior, another value can be supplied using
 | 
					
						
							|  |  |  | the ``prog=`` argument to :class:`ArgumentParser`::
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='myprogram')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: myprogram [-h]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 22:51:40 +03:00
										 |  |  | Note that the program name, whether determined from ``sys.argv[0]``,
 | 
					
						
							|  |  |  | from the ``__main__`` module attributes or from the
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | ``prog=`` argument, is available to help messages using the ``%(prog)s`` format
 | 
					
						
							|  |  |  | specifier.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='myprogram')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', help='foo of the %(prog)s program')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: myprogram [-h] [--foo FOO]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     --foo FOO   foo of the myprogram program
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 22:51:40 +03:00
										 |  |  | .. versionchanged:: 3.14
 | 
					
						
							|  |  |  |    The default ``prog`` value now reflects how ``__main__`` was actually executed,
 | 
					
						
							|  |  |  |    rather than always being ``os.path.basename(sys.argv[0])``.
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | usage
 | 
					
						
							|  |  |  | ^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | By default, :class:`ArgumentParser` calculates the usage message from the
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | arguments it contains. The default message can be overridden with the
 | 
					
						
							|  |  |  | ``usage=`` keyword argument::
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', nargs='?', help='foo help')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', nargs='+', help='bar help')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [options]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |     bar          bar help
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |     -h, --help   show this help message and exit
 | 
					
						
							|  |  |  |     --foo [FOO]  foo help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The ``%(prog)s`` format specifier is available to fill in the program name in
 | 
					
						
							|  |  |  | your usage messages.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 17:29:33 +02:00
										 |  |  | When a custom usage message is specified for the main parser, you may also want to
 | 
					
						
							|  |  |  | consider passing  the ``prog`` argument to :meth:`~ArgumentParser.add_subparsers`
 | 
					
						
							|  |  |  | or the ``prog`` and the ``usage`` arguments to
 | 
					
						
							| 
									
										
										
										
											2024-11-22 18:34:05 +02:00
										 |  |  | :meth:`~_SubParsersAction.add_parser`, to ensure consistent command prefixes and
 | 
					
						
							| 
									
										
										
										
											2024-11-22 17:29:33 +02:00
										 |  |  | usage information across subparsers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _description:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | description
 | 
					
						
							|  |  |  | ^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Most calls to the :class:`ArgumentParser` constructor will use the
 | 
					
						
							|  |  |  | ``description=`` keyword argument.  This argument gives a brief description of
 | 
					
						
							|  |  |  | what the program does and how it works.  In help messages, the description is
 | 
					
						
							|  |  |  | displayed between the command-line usage string and the help messages for the
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | various arguments.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | By default, the description will be line-wrapped so that it fits within the
 | 
					
						
							|  |  |  | given space.  To change this behavior, see the formatter_class_ argument.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | epilog
 | 
					
						
							|  |  |  | ^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Some programs like to display additional description of the program after the
 | 
					
						
							|  |  |  | description of the arguments.  Such text can be specified using the ``epilog=``
 | 
					
						
							|  |  |  | argument to :class:`ArgumentParser`::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(
 | 
					
						
							|  |  |  |    ...     description='A foo that bars',
 | 
					
						
							|  |  |  |    ...     epilog="And that's how you'd foo a bar")
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: argparse.py [-h]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A foo that bars
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    And that's how you'd foo a bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | As with the description_ argument, the ``epilog=`` text is by default
 | 
					
						
							|  |  |  | line-wrapped, but this behavior can be adjusted with the formatter_class_
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | argument to :class:`ArgumentParser`.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | parents
 | 
					
						
							|  |  |  | ^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sometimes, several parsers share a common set of arguments. Rather than
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | repeating the definitions of these arguments, a single parser with all the
 | 
					
						
							|  |  |  | shared arguments and passed to ``parents=`` argument to :class:`ArgumentParser`
 | 
					
						
							|  |  |  | can be used.  The ``parents=`` argument takes a list of :class:`ArgumentParser`
 | 
					
						
							|  |  |  | objects, collects all the positional and optional actions from them, and adds
 | 
					
						
							|  |  |  | these actions to the :class:`ArgumentParser` object being constructed::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parent_parser = argparse.ArgumentParser(add_help=False)
 | 
					
						
							|  |  |  |    >>> parent_parser.add_argument('--parent', type=int)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> foo_parser = argparse.ArgumentParser(parents=[parent_parser])
 | 
					
						
							|  |  |  |    >>> foo_parser.add_argument('foo')
 | 
					
						
							|  |  |  |    >>> foo_parser.parse_args(['--parent', '2', 'XXX'])
 | 
					
						
							|  |  |  |    Namespace(foo='XXX', parent=2)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> bar_parser = argparse.ArgumentParser(parents=[parent_parser])
 | 
					
						
							|  |  |  |    >>> bar_parser.add_argument('--bar')
 | 
					
						
							|  |  |  |    >>> bar_parser.parse_args(['--bar', 'YYY'])
 | 
					
						
							|  |  |  |    Namespace(bar='YYY', parent=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that most parent parsers will specify ``add_help=False``.  Otherwise, the
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | :class:`ArgumentParser` will see two ``-h/--help`` options (one in the parent
 | 
					
						
							|  |  |  | and one in the child) and raise an error.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 21:49:00 +01:00
										 |  |  | .. note::
 | 
					
						
							|  |  |  |    You must fully initialize the parsers before passing them via ``parents=``.
 | 
					
						
							|  |  |  |    If you change the parent parsers after the child parser, those changes will
 | 
					
						
							|  |  |  |    not be reflected in the child.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _formatter_class:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | formatter_class
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | :class:`ArgumentParser` objects allow the help formatting to be customized by
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:57:47 +03:00
										 |  |  | specifying an alternate formatting class.  Currently, there are four such
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | classes:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: RawDescriptionHelpFormatter
 | 
					
						
							|  |  |  |            RawTextHelpFormatter
 | 
					
						
							|  |  |  |            ArgumentDefaultsHelpFormatter
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:57:47 +03:00
										 |  |  |            MetavarTypeHelpFormatter
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | :class:`RawDescriptionHelpFormatter` and :class:`RawTextHelpFormatter` give
 | 
					
						
							|  |  |  | more control over how textual descriptions are displayed.
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | By default, :class:`ArgumentParser` objects line-wrap the description_ and
 | 
					
						
							|  |  |  | epilog_ texts in command-line help messages::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(
 | 
					
						
							|  |  |  |    ...     prog='PROG',
 | 
					
						
							|  |  |  |    ...     description='''this description
 | 
					
						
							|  |  |  |    ...         was indented weird
 | 
					
						
							|  |  |  |    ...             but that is okay''',
 | 
					
						
							|  |  |  |    ...     epilog='''
 | 
					
						
							|  |  |  |    ...             likewise for this epilog whose whitespace will
 | 
					
						
							|  |  |  |    ...         be cleaned up and whose words will be wrapped
 | 
					
						
							|  |  |  |    ...         across a couple lines''')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [-h]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    this description was indented weird but that is okay
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    likewise for this epilog whose whitespace will be cleaned up and whose words
 | 
					
						
							|  |  |  |    will be wrapped across a couple lines
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | Passing :class:`RawDescriptionHelpFormatter` as ``formatter_class=``
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | indicates that description_ and epilog_ are already correctly formatted and
 | 
					
						
							|  |  |  | should not be line-wrapped::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(
 | 
					
						
							|  |  |  |    ...     prog='PROG',
 | 
					
						
							|  |  |  |    ...     formatter_class=argparse.RawDescriptionHelpFormatter,
 | 
					
						
							|  |  |  |    ...     description=textwrap.dedent('''\
 | 
					
						
							|  |  |  |    ...         Please do not mess up this text!
 | 
					
						
							|  |  |  |    ...         --------------------------------
 | 
					
						
							|  |  |  |    ...             I have indented it
 | 
					
						
							|  |  |  |    ...             exactly the way
 | 
					
						
							|  |  |  |    ...             I want it
 | 
					
						
							|  |  |  |    ...         '''))
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [-h]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Please do not mess up this text!
 | 
					
						
							|  |  |  |    --------------------------------
 | 
					
						
							|  |  |  |       I have indented it
 | 
					
						
							|  |  |  |       exactly the way
 | 
					
						
							|  |  |  |       I want it
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | :class:`RawTextHelpFormatter` maintains whitespace for all sorts of help text,
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | including argument descriptions. However, multiple newlines are replaced with
 | 
					
						
							| 
									
										
										
										
											2017-09-07 23:06:45 +03:00
										 |  |  | one. If you wish to preserve multiple blank lines, add spaces between the
 | 
					
						
							|  |  |  | newlines.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | :class:`ArgumentDefaultsHelpFormatter` automatically adds information about
 | 
					
						
							|  |  |  | default values to each of the argument help messages::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(
 | 
					
						
							|  |  |  |    ...     prog='PROG',
 | 
					
						
							|  |  |  |    ...     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', type=int, default=42, help='FOO!')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							| 
									
										
										
										
											2019-11-11 12:47:48 -08:00
										 |  |  |    usage: PROG [-h] [--foo FOO] [bar ...]
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |     bar         BAR! (default: [1, 2, 3])
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     --foo FOO   FOO! (default: 42)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | :class:`MetavarTypeHelpFormatter` uses the name of the type_ argument for each
 | 
					
						
							| 
									
										
										
										
											2011-10-19 11:06:26 +03:00
										 |  |  | argument as the display name for its values (rather than using the dest_
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  | as the regular formatter does)::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(
 | 
					
						
							|  |  |  |    ...     prog='PROG',
 | 
					
						
							|  |  |  |    ...     formatter_class=argparse.MetavarTypeHelpFormatter)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', type=int)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', type=float)
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [-h] [--foo int] float
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |      float
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2011-03-26 14:48:04 +01:00
										 |  |  |      -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |      --foo int
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | prefix_chars
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Most command-line options will use ``-`` as the prefix, e.g. ``-f/--foo``.
 | 
					
						
							|  |  |  | Parsers that need to support different or additional prefix
 | 
					
						
							|  |  |  | characters, e.g. for options
 | 
					
						
							|  |  |  | like ``+f`` or ``/foo``, may specify them using the ``prefix_chars=`` argument
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | to the :class:`ArgumentParser` constructor::
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('+f')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('++bar')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('+f X ++bar Y'.split())
 | 
					
						
							|  |  |  |    Namespace(bar='Y', f='X')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The ``prefix_chars=`` argument defaults to ``'-'``. Supplying a set of
 | 
					
						
							|  |  |  | characters that does not include ``-`` will cause ``-f/--foo`` options to be
 | 
					
						
							|  |  |  | disallowed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fromfile_prefix_chars
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-12 05:25:09 +02:00
										 |  |  | Sometimes, when dealing with a particularly long argument list, it
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | may make sense to keep the list of arguments in a file rather than typing it out
 | 
					
						
							|  |  |  | at the command line.  If the ``fromfile_prefix_chars=`` argument is given to the
 | 
					
						
							|  |  |  | :class:`ArgumentParser` constructor, then arguments that start with any of the
 | 
					
						
							|  |  |  | specified characters will be treated as files, and will be replaced by the
 | 
					
						
							|  |  |  | arguments they contain.  For example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:09:57 +09:00
										 |  |  |    >>> with open('args.txt', 'w', encoding=sys.getfilesystemencoding()) as fp:
 | 
					
						
							| 
									
										
										
										
											2016-05-10 12:01:23 +03:00
										 |  |  |    ...     fp.write('-f\nbar')
 | 
					
						
							| 
									
										
										
										
											2022-12-08 19:31:19 -08:00
										 |  |  |    ...
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    >>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-f', 'foo', '@args.txt'])
 | 
					
						
							|  |  |  |    Namespace(f='bar')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Arguments read from a file must by default be one per line (but see also
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.convert_arg_line_to_args`) and are treated as if they
 | 
					
						
							|  |  |  | were in the same place as the original file referencing argument on the command
 | 
					
						
							|  |  |  | line.  So in the example above, the expression ``['-f', 'foo', '@args.txt']``
 | 
					
						
							|  |  |  | is considered equivalent to the expression ``['-f', 'foo', '-f', 'bar']``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:09:57 +09:00
										 |  |  | :class:`ArgumentParser` uses :term:`filesystem encoding and error handler`
 | 
					
						
							|  |  |  | to read the file containing arguments.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that
 | 
					
						
							|  |  |  | arguments will never be treated as file references.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:09:57 +09:00
										 |  |  | .. versionchanged:: 3.12
 | 
					
						
							|  |  |  |    :class:`ArgumentParser` changed encoding and errors to read arguments files
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    from default (e.g. :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`
 | 
					
						
							|  |  |  |    and ``"strict"``) to the :term:`filesystem encoding and error handler`.
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:09:57 +09:00
										 |  |  |    Arguments file should be encoded in UTF-8 instead of ANSI Codepage on Windows.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | argument_default
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generally, argument defaults are specified either by passing a default to
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument` or by calling the
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.set_defaults` methods with a specific set of name-value
 | 
					
						
							|  |  |  | pairs.  Sometimes however, it may be useful to specify a single parser-wide
 | 
					
						
							|  |  |  | default for arguments.  This can be accomplished by passing the
 | 
					
						
							|  |  |  | ``argument_default=`` keyword argument to :class:`ArgumentParser`.  For example,
 | 
					
						
							|  |  |  | to globally suppress attribute creation on :meth:`~ArgumentParser.parse_args`
 | 
					
						
							|  |  |  | calls, we supply ``argument_default=SUPPRESS``::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', nargs='?')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo', '1', 'BAR'])
 | 
					
						
							|  |  |  |    Namespace(bar='BAR', foo='1')
 | 
					
						
							|  |  |  |    >>> parser.parse_args([])
 | 
					
						
							|  |  |  |    Namespace()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  | .. _allow_abbrev:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | allow_abbrev
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Normally, when you pass an argument list to the
 | 
					
						
							| 
									
										
										
										
											2015-11-02 04:20:33 +00:00
										 |  |  | :meth:`~ArgumentParser.parse_args` method of an :class:`ArgumentParser`,
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  | it :ref:`recognizes abbreviations <prefix-matching>` of long options.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This feature can be disabled by setting ``allow_abbrev`` to ``False``::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foobar', action='store_true')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foonley', action='store_false')
 | 
					
						
							| 
									
										
										
										
											2015-03-12 20:47:41 +02:00
										 |  |  |    >>> parser.parse_args(['--foon'])
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  |    usage: PROG [-h] [--foobar] [--foonley]
 | 
					
						
							|  |  |  |    PROG: error: unrecognized arguments: --foon
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 3.5
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | conflict_handler
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | :class:`ArgumentParser` objects do not allow two actions with the same option
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:04:41 +00:00
										 |  |  | string.  By default, :class:`ArgumentParser` objects raise an exception if an
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | attempt is made to create an argument with an option string that is already in
 | 
					
						
							|  |  |  | use::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f', '--foo', help='old foo help')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', help='new foo help')
 | 
					
						
							|  |  |  |    Traceback (most recent call last):
 | 
					
						
							|  |  |  |     ..
 | 
					
						
							|  |  |  |    ArgumentError: argument --foo: conflicting option string(s): --foo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sometimes (e.g. when using parents_) it may be useful to simply override any
 | 
					
						
							|  |  |  | older arguments with the same option string.  To get this behavior, the value
 | 
					
						
							|  |  |  | ``'resolve'`` can be supplied to the ``conflict_handler=`` argument of
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | :class:`ArgumentParser`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', conflict_handler='resolve')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f', '--foo', help='old foo help')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', help='new foo help')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-f FOO] [--foo FOO]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     -f FOO      old foo help
 | 
					
						
							|  |  |  |     --foo FOO   new foo help
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Note that :class:`ArgumentParser` objects only remove an action if all of its
 | 
					
						
							|  |  |  | option strings are overridden.  So, in the example above, the old ``-f/--foo``
 | 
					
						
							|  |  |  | action is retained as the ``-f`` action, because only the ``--foo`` option
 | 
					
						
							|  |  |  | string was overridden.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | add_help
 | 
					
						
							|  |  |  | ^^^^^^^^
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | By default, :class:`ArgumentParser` objects add an option which simply displays
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | the parser's help message. If ``-h`` or ``--help`` is supplied at the command
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | line, the :class:`!ArgumentParser` help will be printed.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | Occasionally, it may be useful to disable the addition of this help option.
 | 
					
						
							|  |  |  | This can be achieved by passing ``False`` as the ``add_help=`` argument to
 | 
					
						
							|  |  |  | :class:`ArgumentParser`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', help='foo help')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    >>> parser.print_help()
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    usage: PROG [--foo FOO]
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |     --foo FOO  foo help
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  | The help option is typically ``-h/--help``. The exception to this is
 | 
					
						
							|  |  |  | if the ``prefix_chars=`` is specified and does not include ``-``, in
 | 
					
						
							|  |  |  | which case ``-h`` and ``--help`` are not valid options.  In
 | 
					
						
							|  |  |  | this case, the first character in ``prefix_chars`` is used to prefix
 | 
					
						
							|  |  |  | the help options::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 14:43:17 +03:00
										 |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='+/')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    >>> parser.print_help()
 | 
					
						
							| 
									
										
										
										
											2013-10-06 09:50:36 +02:00
										 |  |  |    usage: PROG [+h]
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2013-10-06 09:50:36 +02:00
										 |  |  |      +h, ++help  show this help message and exit
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  | exit_on_error
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Normally, when you pass an invalid argument list to the :meth:`~ArgumentParser.parse_args`
 | 
					
						
							| 
									
										
										
										
											2024-10-10 13:56:05 -04:00
										 |  |  | method of an :class:`ArgumentParser`, it will print a *message* to :data:`sys.stderr` and exit with a status
 | 
					
						
							|  |  |  | code of 2.
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-26 06:04:26 +02:00
										 |  |  | If the user would like to catch errors manually, the feature can be enabled by setting
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  | ``exit_on_error`` to ``False``::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(exit_on_error=False)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--integers', type=int)
 | 
					
						
							|  |  |  |    _StoreAction(option_strings=['--integers'], dest='integers', nargs=None, const=None, default=None, type=<class 'int'>, choices=None, help=None, metavar=None)
 | 
					
						
							|  |  |  |    >>> try:
 | 
					
						
							|  |  |  |    ...     parser.parse_args('--integers a'.split())
 | 
					
						
							|  |  |  |    ... except argparse.ArgumentError:
 | 
					
						
							|  |  |  |    ...     print('Catching an argumentError')
 | 
					
						
							|  |  |  |    ...
 | 
					
						
							|  |  |  |    Catching an argumentError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 3.9
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 00:07:37 -07:00
										 |  |  | suggest_on_error
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | By default, when a user passes an invalid argument choice or subparser name,
 | 
					
						
							|  |  |  | :class:`ArgumentParser` will exit with error info and list the permissible
 | 
					
						
							|  |  |  | argument choices (if specified) or subparser names as part of the error message.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the user would like to enable suggestions for mistyped argument choices and
 | 
					
						
							|  |  |  | subparser names, the feature can be enabled by setting ``suggest_on_error`` to
 | 
					
						
							|  |  |  | ``True``. Note that this only applies for arguments when the choices specified
 | 
					
						
							|  |  |  | are strings::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(description='Process some integers.', suggest_on_error=True)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--action', choices=['sum', 'max'])
 | 
					
						
							|  |  |  |    >>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
 | 
					
						
							|  |  |  |    ...                     help='an integer for the accumulator')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--action', 'sumn', 1, 2, 3])
 | 
					
						
							|  |  |  |    tester.py: error: argument --action: invalid choice: 'sumn', maybe you meant 'sum'? (choose from 'sum', 'max')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 19:18:18 -08:00
										 |  |  | If you're writing code that needs to be compatible with older Python versions
 | 
					
						
							|  |  |  | and want to opportunistically use ``suggest_on_error`` when it's available, you
 | 
					
						
							|  |  |  | can set it as an attribute after initializing the parser instead of using the
 | 
					
						
							|  |  |  | keyword argument::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(description='Process some integers.')
 | 
					
						
							|  |  |  |    >>> parser.suggest_on_error = True
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 00:07:37 -07:00
										 |  |  | .. versionadded:: 3.14
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-12 05:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | The add_argument() method
 | 
					
						
							|  |  |  | -------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | .. method:: ArgumentParser.add_argument(name or flags..., *, [action], [nargs], \
 | 
					
						
							| 
									
										
										
										
											2011-01-09 09:04:08 +00:00
										 |  |  |                            [const], [default], [type], [choices], [required], \
 | 
					
						
							| 
									
										
										
										
											2024-02-19 09:29:32 +01:00
										 |  |  |                            [help], [metavar], [dest], [deprecated])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  |    Define how a single command-line argument should be parsed.  Each parameter
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    has its own more detailed description below, but in short they are:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    * `name or flags`_ - Either a name or a list of option strings, e.g. ``'foo'``
 | 
					
						
							|  |  |  |      or ``'-f', '--foo'``.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    * action_ - The basic type of action to be taken when this argument is
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  |      encountered at the command line.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    * nargs_ - The number of command-line arguments that should be consumed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * const_ - A constant value required by some action_ and nargs_ selections.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * default_ - The value produced if the argument is absent from the
 | 
					
						
							| 
									
										
										
										
											2020-12-06 18:29:08 -08:00
										 |  |  |      command line and if it is absent from the namespace object.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-16 23:13:50 +03:00
										 |  |  |    * type_ - The type to which the command-line argument should be converted.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 08:22:53 +02:00
										 |  |  |    * choices_ - A sequence of the allowable values for the argument.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    * required_ - Whether or not the command-line option may be omitted
 | 
					
						
							|  |  |  |      (optionals only).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * help_ - A brief description of what the argument does.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * metavar_ - A name for the argument in usage messages.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * dest_ - The name of the attribute to be added to the object returned by
 | 
					
						
							|  |  |  |      :meth:`parse_args`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  |    * deprecated_ - Whether or not use of the argument is deprecated.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | The following sections describe how each of these are used.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-10 23:30:27 -07:00
										 |  |  | .. _`name or flags`:
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | name or flags
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | The :meth:`~ArgumentParser.add_argument` method must know whether an optional
 | 
					
						
							|  |  |  | argument, like ``-f`` or ``--foo``, or a positional argument, like a list of
 | 
					
						
							|  |  |  | filenames, is expected.  The first arguments passed to
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument` must therefore be either a series of
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  | flags, or a simple argument name.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example, an optional argument could be created like::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f', '--foo')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | while a positional argument could be created like::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | When :meth:`~ArgumentParser.parse_args` is called, optional arguments will be
 | 
					
						
							|  |  |  | identified by the ``-`` prefix, and the remaining arguments will be assumed to
 | 
					
						
							|  |  |  | be positional::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f', '--foo')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['BAR'])
 | 
					
						
							|  |  |  |    Namespace(bar='BAR', foo=None)
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['BAR', '--foo', 'FOO'])
 | 
					
						
							|  |  |  |    Namespace(bar='BAR', foo='FOO')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo', 'FOO'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-f FOO] bar
 | 
					
						
							| 
									
										
										
										
											2018-04-11 20:45:04 +02:00
										 |  |  |    PROG: error: the following arguments are required: bar
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | By default, :mod:`!argparse` automatically handles the internal naming and
 | 
					
						
							| 
									
										
										
										
											2024-10-12 14:46:28 +03:00
										 |  |  | display names of arguments, simplifying the process without requiring
 | 
					
						
							|  |  |  | additional configuration.
 | 
					
						
							|  |  |  | As such, you do not need to specify the dest_ and metavar_ parameters.
 | 
					
						
							|  |  |  | The dest_ parameter defaults to the argument name with underscores ``_``
 | 
					
						
							|  |  |  | replacing hyphens ``-`` . The metavar_ parameter defaults to the
 | 
					
						
							|  |  |  | upper-cased name. For example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo-bar')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo-bar', 'FOO-BAR']
 | 
					
						
							|  |  |  |    Namespace(foo_bar='FOO-BAR')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage:  [-h] [--foo-bar FOO-BAR]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    optional arguments:
 | 
					
						
							|  |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     --foo-bar FOO-BAR
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _action:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | action
 | 
					
						
							|  |  |  | ^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  | :class:`ArgumentParser` objects associate command-line arguments with actions.  These
 | 
					
						
							|  |  |  | actions can do just about anything with the command-line arguments associated with
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | them, though most actions simply add an attribute to the object returned by
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | :meth:`~ArgumentParser.parse_args`.  The ``action`` keyword argument specifies
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | how the command-line arguments should be handled. The supplied actions are:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | * ``'store'`` - This just stores the argument's value.  This is the default
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |   action.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | * ``'store_const'`` - This stores the value specified by the const_ keyword
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  |   argument; note that the const_ keyword argument defaults to ``None``.  The
 | 
					
						
							|  |  |  |   ``'store_const'`` action is most commonly used with optional arguments that
 | 
					
						
							|  |  |  |   specify some sort of flag.  For example::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--foo', action='store_const', const=42)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |     >>> parser.parse_args(['--foo'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     Namespace(foo=42)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-20 11:05:23 -08:00
										 |  |  | * ``'store_true'`` and ``'store_false'`` - These are special cases of
 | 
					
						
							|  |  |  |   ``'store_const'`` used for storing the values ``True`` and ``False``
 | 
					
						
							|  |  |  |   respectively.  In addition, they create default values of ``False`` and
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |   ``True`` respectively::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--foo', action='store_true')
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--bar', action='store_false')
 | 
					
						
							| 
									
										
										
										
											2011-11-20 11:05:23 -08:00
										 |  |  |     >>> parser.add_argument('--baz', action='store_false')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     >>> parser.parse_args('--foo --bar'.split())
 | 
					
						
							| 
									
										
										
										
											2011-11-20 11:05:23 -08:00
										 |  |  |     Namespace(foo=True, bar=False, baz=True)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | * ``'append'`` - This stores a list, and appends each argument value to the
 | 
					
						
							| 
									
										
										
										
											2022-05-06 11:32:35 +08:00
										 |  |  |   list. It is useful to allow an option to be specified multiple times.
 | 
					
						
							|  |  |  |   If the default value is non-empty, the default elements will be present
 | 
					
						
							|  |  |  |   in the parsed value for the option, with any values from the
 | 
					
						
							|  |  |  |   command line appended after those default values. Example usage::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--foo', action='append')
 | 
					
						
							|  |  |  |     >>> parser.parse_args('--foo 1 --foo 2'.split())
 | 
					
						
							|  |  |  |     Namespace(foo=['1', '2'])
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * ``'append_const'`` - This stores a list, and appends the value specified by
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  |   the const_ keyword argument to the list; note that the const_ keyword
 | 
					
						
							|  |  |  |   argument defaults to ``None``. The ``'append_const'`` action is typically
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |   useful when multiple arguments need to store constants to the same list. For
 | 
					
						
							|  |  |  |   example::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--str', dest='types', action='append_const', const=str)
 | 
					
						
							|  |  |  |     >>> parser.add_argument('--int', dest='types', action='append_const', const=int)
 | 
					
						
							|  |  |  |     >>> parser.parse_args('--str --int'.split())
 | 
					
						
							| 
									
										
										
										
											2011-10-28 11:21:19 +02:00
										 |  |  |     Namespace(types=[<class 'str'>, <class 'int'>])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 11:41:38 +03:00
										 |  |  | * ``'extend'`` - This stores a list and appends each item from the multi-value
 | 
					
						
							|  |  |  |   argument list to it.
 | 
					
						
							|  |  |  |   The ``'extend'`` action is typically used with the nargs_ keyword argument
 | 
					
						
							|  |  |  |   value ``'+'`` or ``'*'``.
 | 
					
						
							|  |  |  |   Note that when nargs_ is ``None`` (the default) or ``'?'``, each
 | 
					
						
							|  |  |  |   character of the argument string will be appended to the list.
 | 
					
						
							|  |  |  |   Example usage::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |     >>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
 | 
					
						
							|  |  |  |     >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
 | 
					
						
							|  |  |  |     Namespace(foo=['f1', 'f2', 'f3', 'f4'])
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   .. versionadded:: 3.8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-04 23:25:04 +01:00
										 |  |  | * ``'count'`` - This counts the number of times a keyword argument occurs. For
 | 
					
						
							|  |  |  |   example, this is useful for increasing verbosity levels::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser()
 | 
					
						
							| 
									
										
										
										
											2019-11-17 22:06:19 -08:00
										 |  |  |     >>> parser.add_argument('--verbose', '-v', action='count', default=0)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |     >>> parser.parse_args(['-vvv'])
 | 
					
						
							| 
									
										
										
										
											2012-01-04 23:25:04 +01:00
										 |  |  |     Namespace(verbose=3)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 22:06:19 -08:00
										 |  |  |   Note, the *default* will be ``None`` unless explicitly set to *0*.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-04 23:25:04 +01:00
										 |  |  | * ``'help'`` - This prints a complete help message for all the options in the
 | 
					
						
							|  |  |  |   current parser and then exits. By default a help action is automatically
 | 
					
						
							|  |  |  |   added to the parser. See :class:`ArgumentParser` for details of how the
 | 
					
						
							|  |  |  |   output is created.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | * ``'version'`` - This expects a ``version=`` keyword argument in the
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  |   :meth:`~ArgumentParser.add_argument` call, and prints version information
 | 
					
						
							| 
									
										
										
										
											2012-02-20 01:44:55 +01:00
										 |  |  |   and exits when invoked::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> import argparse
 | 
					
						
							|  |  |  |     >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							| 
									
										
										
										
											2010-05-24 03:21:08 +00:00
										 |  |  |     >>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')
 | 
					
						
							|  |  |  |     >>> parser.parse_args(['--version'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     PROG 2.0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-12 14:53:00 +03:00
										 |  |  | Only actions that consume command-line arguments (e.g. ``'store'``,
 | 
					
						
							|  |  |  | ``'append'`` or ``'extend'``) can be used with positional arguments.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | .. class:: BooleanOptionalAction
 | 
					
						
							| 
									
										
										
										
											2019-09-13 12:17:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    You may also specify an arbitrary action by passing an :class:`Action` subclass or
 | 
					
						
							|  |  |  |    other object that implements the same interface. The :class:`!BooleanOptionalAction`
 | 
					
						
							|  |  |  |    is available in :mod:`!argparse` and adds support for boolean actions such as
 | 
					
						
							|  |  |  |    ``--foo`` and ``--no-foo``::
 | 
					
						
							| 
									
										
										
										
											2019-09-13 12:17:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |        >>> import argparse
 | 
					
						
							|  |  |  |        >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |        >>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
 | 
					
						
							|  |  |  |        >>> parser.parse_args(['--no-foo'])
 | 
					
						
							|  |  |  |        Namespace(foo=False)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.9
 | 
					
						
							| 
									
										
										
										
											2021-07-13 01:19:54 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 12:17:43 +02:00
										 |  |  | The recommended way to create a custom action is to extend :class:`Action`,
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` and
 | 
					
						
							| 
									
										
										
										
											2024-11-21 16:36:11 -08:00
										 |  |  | :meth:`!format_usage` methods. You can also register custom actions using the
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.register` method and reference them by their registered name.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | An example of a custom action::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> class FooAction(argparse.Action):
 | 
					
						
							| 
									
										
										
										
											2014-07-20 10:52:46 -04:00
										 |  |  |    ...     def __init__(self, option_strings, dest, nargs=None, **kwargs):
 | 
					
						
							|  |  |  |    ...         if nargs is not None:
 | 
					
						
							|  |  |  |    ...             raise ValueError("nargs not allowed")
 | 
					
						
							| 
									
										
										
										
											2021-04-26 19:13:54 -03:00
										 |  |  |    ...         super().__init__(option_strings, dest, **kwargs)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    ...     def __call__(self, parser, namespace, values, option_string=None):
 | 
					
						
							| 
									
										
										
										
											2010-07-26 17:00:20 +00:00
										 |  |  |    ...         print('%r %r %r' % (namespace, values, option_string))
 | 
					
						
							|  |  |  |    ...         setattr(namespace, self.dest, values)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    ...
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', action=FooAction)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', action=FooAction)
 | 
					
						
							|  |  |  |    >>> args = parser.parse_args('1 --foo 2'.split())
 | 
					
						
							|  |  |  |    Namespace(bar=None, foo=None) '1' None
 | 
					
						
							|  |  |  |    Namespace(bar='1', foo=None) '2' '--foo'
 | 
					
						
							|  |  |  |    >>> args
 | 
					
						
							|  |  |  |    Namespace(bar='1', foo='2')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-03 14:54:11 -04:00
										 |  |  | For more details, see :class:`Action`.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. _nargs:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | nargs
 | 
					
						
							|  |  |  | ^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | :class:`ArgumentParser` objects usually associate a single command-line argument with a
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | single action to be taken.  The ``nargs`` keyword argument associates a
 | 
					
						
							| 
									
										
										
										
											2022-05-13 07:17:07 -07:00
										 |  |  | different number of command-line arguments with a single action.
 | 
					
						
							|  |  |  | See also :ref:`specifying-ambiguous-arguments`. The supported values are:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 01:44:55 +01:00
										 |  |  | * ``N`` (an integer).  ``N`` arguments from the command line will be gathered
 | 
					
						
							|  |  |  |   together into a list.  For example::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 10:26:05 +00:00
										 |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', nargs=2)
 | 
					
						
							|  |  |  |      >>> parser.add_argument('bar', nargs=1)
 | 
					
						
							|  |  |  |      >>> parser.parse_args('c --foo a b'.split())
 | 
					
						
							|  |  |  |      Namespace(bar=['c'], foo=['a', 'b'])
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Note that ``nargs=1`` produces a list of one item.  This is different from
 | 
					
						
							|  |  |  |   the default, in which the item is produced by itself.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 13:41:26 +02:00
										 |  |  | .. index:: single: ? (question mark); in argparse module
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  | * ``'?'``. One argument will be consumed from the command line if possible, and
 | 
					
						
							|  |  |  |   produced as a single item.  If no command-line argument is present, the value from
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |   default_ will be produced.  Note that for optional arguments, there is an
 | 
					
						
							|  |  |  |   additional case - the option string is present but not followed by a
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  |   command-line argument.  In this case the value from const_ will be produced.  Some
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |   examples to illustrate this::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', nargs='?', const='c', default='d')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('bar', nargs='?', default='d')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |      >>> parser.parse_args(['XX', '--foo', 'YY'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      Namespace(bar='XX', foo='YY')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |      >>> parser.parse_args(['XX', '--foo'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      Namespace(bar='XX', foo='c')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |      >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      Namespace(bar='d', foo='d')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   One of the more common uses of ``nargs='?'`` is to allow optional input and
 | 
					
						
							|  |  |  |   output files::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:50:29 +03:00
										 |  |  |      >>> parser.add_argument('infile', nargs='?')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('outfile', nargs='?')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>> parser.parse_args(['input.txt', 'output.txt'])
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:50:29 +03:00
										 |  |  |      Namespace(infile='input.txt', outfile='output.txt')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['input.txt'])
 | 
					
						
							|  |  |  |      Namespace(infile='input.txt', outfile=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:50:29 +03:00
										 |  |  |      Namespace(infile=None, outfile=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 13:41:26 +02:00
										 |  |  | .. index:: single: * (asterisk); in argparse module
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  | * ``'*'``.  All command-line arguments present are gathered into a list.  Note that
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |   it generally doesn't make much sense to have more than one positional argument
 | 
					
						
							|  |  |  |   with ``nargs='*'``, but multiple optional arguments with ``nargs='*'`` is
 | 
					
						
							|  |  |  |   possible.  For example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', nargs='*')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--bar', nargs='*')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('baz', nargs='*')
 | 
					
						
							|  |  |  |      >>> parser.parse_args('a b --foo x y --bar 1 2'.split())
 | 
					
						
							|  |  |  |      Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 13:41:26 +02:00
										 |  |  | .. index:: single: + (plus); in argparse module
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | * ``'+'``. Just like ``'*'``, all command-line args present are gathered into a
 | 
					
						
							|  |  |  |   list.  Additionally, an error message will be generated if there wasn't at
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  |   least one command-line argument present.  For example::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('foo', nargs='+')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |      >>> parser.parse_args(['a', 'b'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      Namespace(foo=['a', 'b'])
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |      >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      usage: PROG [-h] foo [foo ...]
 | 
					
						
							| 
									
										
										
										
											2018-04-11 20:45:04 +02:00
										 |  |  |      PROG: error: the following arguments are required: foo
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  | If the ``nargs`` keyword argument is not provided, the number of arguments consumed
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  | is determined by the action_.  Generally this means a single command-line argument
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | will be consumed and a single item (not a list) will be produced.
 | 
					
						
							| 
									
										
										
										
											2024-10-12 14:53:00 +03:00
										 |  |  | Actions that do not consume command-line arguments (e.g.
 | 
					
						
							|  |  |  | ``'store_const'``) set ``nargs=0``.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _const:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | const
 | 
					
						
							|  |  |  | ^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | The ``const`` argument of :meth:`~ArgumentParser.add_argument` is used to hold
 | 
					
						
							|  |  |  | constant values that are not read from the command line but are required for
 | 
					
						
							|  |  |  | the various :class:`ArgumentParser` actions.  The two most common uses of it are:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | * When :meth:`~ArgumentParser.add_argument` is called with
 | 
					
						
							|  |  |  |   ``action='store_const'`` or ``action='append_const'``.  These actions add the
 | 
					
						
							| 
									
										
										
										
											2012-02-20 01:44:55 +01:00
										 |  |  |   ``const`` value to one of the attributes of the object returned by
 | 
					
						
							|  |  |  |   :meth:`~ArgumentParser.parse_args`. See the action_ description for examples.
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  |   If ``const`` is not provided to :meth:`~ArgumentParser.add_argument`, it will
 | 
					
						
							|  |  |  |   receive a default value of ``None``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | * When :meth:`~ArgumentParser.add_argument` is called with option strings
 | 
					
						
							|  |  |  |   (like ``-f`` or ``--foo``) and ``nargs='?'``.  This creates an optional
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  |   argument that can be followed by zero or one command-line arguments.
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  |   When parsing the command line, if the option string is encountered with no
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  |   command-line argument following it, the value of ``const`` will be assumed to
 | 
					
						
							|  |  |  |   be ``None`` instead.  See the nargs_ description for examples.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-31 12:27:55 -04:00
										 |  |  | .. versionchanged:: 3.11
 | 
					
						
							|  |  |  |    ``const=None`` by default, including when ``action='append_const'`` or
 | 
					
						
							|  |  |  |    ``action='store_const'``.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _default:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | default
 | 
					
						
							|  |  |  | ^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | All optional arguments and some positional arguments may be omitted at the
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | command line.  The ``default`` keyword argument of
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument`, whose value defaults to ``None``,
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  | specifies what value should be used if the command-line argument is not present.
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | For optional arguments, the ``default`` value is used when the option string
 | 
					
						
							|  |  |  | was not present at the command line::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', default=42)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['--foo', '2'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo='2')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo=42)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-06 18:29:08 -08:00
										 |  |  | If the target namespace already has an attribute set, the action *default*
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | will not overwrite it::
 | 
					
						
							| 
									
										
										
										
											2020-12-06 18:29:08 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', default=42)
 | 
					
						
							|  |  |  |    >>> parser.parse_args([], namespace=argparse.Namespace(foo=101))
 | 
					
						
							|  |  |  |    Namespace(foo=101)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-25 10:37:58 -04:00
										 |  |  | If the ``default`` value is a string, the parser parses the value as if it
 | 
					
						
							|  |  |  | were a command-line argument.  In particular, the parser applies any type_
 | 
					
						
							|  |  |  | conversion argument, if provided, before setting the attribute on the
 | 
					
						
							|  |  |  | :class:`Namespace` return value.  Otherwise, the parser uses the value as is::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--length', default='10', type=int)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--width', default=10.5, type=int)
 | 
					
						
							|  |  |  |    >>> parser.parse_args()
 | 
					
						
							|  |  |  |    Namespace(length=10, width=10.5)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | For positional arguments with nargs_ equal to ``?`` or ``*``, the ``default`` value
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  | is used when no command-line argument was present::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('foo', nargs='?', default=42)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['a'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo='a')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo=42)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 09:30:01 -07:00
										 |  |  | For required_ arguments, the ``default`` value is ignored. For example, this
 | 
					
						
							|  |  |  | applies to positional arguments with nargs_ values other than ``?`` or ``*``,
 | 
					
						
							|  |  |  | or optional arguments marked as ``required=True``.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Providing ``default=argparse.SUPPRESS`` causes no attribute to be added if the
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:14:15 +02:00
										 |  |  | command-line argument was not present::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', default=argparse.SUPPRESS)
 | 
					
						
							|  |  |  |    >>> parser.parse_args([])
 | 
					
						
							|  |  |  |    Namespace()
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo', '1'])
 | 
					
						
							|  |  |  |    Namespace(foo='1')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 06:50:34 -07:00
										 |  |  | .. _argparse-type:
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | type
 | 
					
						
							|  |  |  | ^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | By default, the parser reads command-line arguments in as simple
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | strings. However, quite often the command-line string should instead be
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | interpreted as another type, such as a :class:`float` or :class:`int`.  The
 | 
					
						
							|  |  |  | ``type`` keyword for :meth:`~ArgumentParser.add_argument` allows any
 | 
					
						
							|  |  |  | necessary type-checking and type conversions to be performed.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | If the type_ keyword is used with the default_ keyword, the type converter
 | 
					
						
							|  |  |  | is only applied if the default is a string.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-21 16:36:11 -08:00
										 |  |  | The argument to ``type`` can be a callable that accepts a single string or
 | 
					
						
							|  |  |  | the name of a registered type (see :meth:`~ArgumentParser.register`)
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | If the function raises :exc:`ArgumentTypeError`, :exc:`TypeError`, or
 | 
					
						
							|  |  |  | :exc:`ValueError`, the exception is caught and a nicely formatted error
 | 
					
						
							| 
									
										
										
										
											2024-11-21 16:36:11 -08:00
										 |  |  | message is displayed. Other exception types are not handled.
 | 
					
						
							| 
									
										
										
										
											2012-09-25 10:37:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | Common built-in types and functions can be used as type converters:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | .. testcode::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  |    import argparse
 | 
					
						
							|  |  |  |    import pathlib
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  |    parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    parser.add_argument('count', type=int)
 | 
					
						
							|  |  |  |    parser.add_argument('distance', type=float)
 | 
					
						
							|  |  |  |    parser.add_argument('street', type=ascii)
 | 
					
						
							|  |  |  |    parser.add_argument('code_point', type=ord)
 | 
					
						
							|  |  |  |    parser.add_argument('datapath', type=pathlib.Path)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | User defined functions can be used as well:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. doctest::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> def hyphenated(string):
 | 
					
						
							|  |  |  |    ...     return '-'.join([word[:4] for word in string.casefold().split()])
 | 
					
						
							|  |  |  |    ...
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> _ = parser.add_argument('short_title', type=hyphenated)
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['"The Tale of Two Cities"'])
 | 
					
						
							|  |  |  |    Namespace(short_title='"the-tale-of-two-citi')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The :func:`bool` function is not recommended as a type converter.  All it does
 | 
					
						
							|  |  |  | is convert empty strings to ``False`` and non-empty strings to ``True``.
 | 
					
						
							|  |  |  | This is usually not what is desired.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In general, the ``type`` keyword is a convenience that should only be used for
 | 
					
						
							|  |  |  | simple conversions that can only raise one of the three supported exceptions.
 | 
					
						
							|  |  |  | Anything with more interesting error-handling or resource management should be
 | 
					
						
							|  |  |  | done downstream after the arguments are parsed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example, JSON or YAML conversions have complex error cases that require
 | 
					
						
							| 
									
										
										
										
											2021-09-28 13:00:51 +02:00
										 |  |  | better reporting than can be given by the ``type`` keyword.  A
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | :exc:`~json.JSONDecodeError` would not be well formatted and a
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  | :exc:`FileNotFoundError` exception would not be handled at all.
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Even :class:`~argparse.FileType` has its limitations for use with the ``type``
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | keyword.  If one argument uses :class:`~argparse.FileType` and then a
 | 
					
						
							|  |  |  | subsequent argument fails, an error is reported but the file is not
 | 
					
						
							|  |  |  | automatically closed.  In this case, it would be better to wait until after
 | 
					
						
							|  |  |  | the parser has run and then use the :keyword:`with`-statement to manage the
 | 
					
						
							|  |  |  | files.
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:14:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | For type checkers that simply check against a fixed set of values, consider
 | 
					
						
							|  |  |  | using the choices_ keyword instead.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _choices:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | choices
 | 
					
						
							|  |  |  | ^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  | Some command-line arguments should be selected from a restricted set of values.
 | 
					
						
							| 
									
										
										
										
											2022-12-26 08:22:53 +02:00
										 |  |  | These can be handled by passing a sequence object as the *choices* keyword
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | argument to :meth:`~ArgumentParser.add_argument`.  When the command line is
 | 
					
						
							| 
									
										
										
										
											2013-01-11 19:26:44 -08:00
										 |  |  | parsed, argument values will be checked, and an error message will be displayed
 | 
					
						
							|  |  |  | if the argument was not one of the acceptable values::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='game.py')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['rock'])
 | 
					
						
							|  |  |  |    Namespace(move='rock')
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['fire'])
 | 
					
						
							|  |  |  |    usage: game.py [-h] {rock,paper,scissors}
 | 
					
						
							|  |  |  |    game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
 | 
					
						
							|  |  |  |    'paper', 'scissors')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 08:22:53 +02:00
										 |  |  | Note that inclusion in the *choices* sequence is checked after any type_
 | 
					
						
							| 
									
										
										
										
											2013-01-11 19:26:44 -08:00
										 |  |  | conversions have been performed, so the type of the objects in the *choices*
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | sequence should match the type_ specified.
 | 
					
						
							| 
									
										
										
										
											2013-01-11 19:26:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 08:22:53 +02:00
										 |  |  | Any sequence can be passed as the *choices* value, so :class:`list` objects,
 | 
					
						
							|  |  |  | :class:`tuple` objects, and custom sequences are all supported.
 | 
					
						
							| 
									
										
										
										
											2020-11-30 09:55:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Use of :class:`enum.Enum` is not recommended because it is difficult to
 | 
					
						
							|  |  |  | control its appearance in usage, help, and error messages.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-01 02:20:10 +04:00
										 |  |  | Formatted choices override the default *metavar* which is normally derived
 | 
					
						
							| 
									
										
										
										
											2020-12-22 09:24:26 -08:00
										 |  |  | from *dest*.  This is usually what you want because the user never sees the
 | 
					
						
							|  |  |  | *dest* parameter.  If this display isn't desirable (perhaps because there are
 | 
					
						
							|  |  |  | many choices), just specify an explicit metavar_.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _required:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | required
 | 
					
						
							|  |  |  | ^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | In general, the :mod:`!argparse` module assumes that flags like ``-f`` and ``--bar``
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  | indicate *optional* arguments, which can always be omitted at the command line.
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | To make an option *required*, ``True`` can be specified for the ``required=``
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | keyword argument to :meth:`~ArgumentParser.add_argument`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', required=True)
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo', 'BAR'])
 | 
					
						
							|  |  |  |    Namespace(foo='BAR')
 | 
					
						
							|  |  |  |    >>> parser.parse_args([])
 | 
					
						
							| 
									
										
										
										
											2020-08-28 11:47:58 -07:00
										 |  |  |    usage: [-h] --foo FOO
 | 
					
						
							|  |  |  |    : error: the following arguments are required: --foo
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | As the example shows, if an option is marked as ``required``,
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.parse_args` will report an error if that option is not
 | 
					
						
							|  |  |  | present at the command line.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | .. note::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Required options are generally considered bad form because users expect
 | 
					
						
							|  |  |  |     *options* to be *optional*, and thus they should be avoided when possible.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _help:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | help
 | 
					
						
							|  |  |  | ^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | The ``help`` value is a string containing a brief description of the argument.
 | 
					
						
							|  |  |  | When a user requests help (usually by using ``-h`` or ``--help`` at the
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  | command line), these ``help`` descriptions will be displayed with each
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | argument.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The ``help`` strings can include various format specifiers to avoid repetition
 | 
					
						
							|  |  |  | of things like the program name or the argument default_.  The available
 | 
					
						
							|  |  |  | specifiers include the program name, ``%(prog)s`` and most keyword arguments to
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | :meth:`~ArgumentParser.add_argument`, e.g. ``%(default)s``, ``%(type)s``, etc.::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='frobble')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', nargs='?', type=int, default=42,
 | 
					
						
							| 
									
										
										
										
											2016-05-10 12:01:23 +03:00
										 |  |  |    ...                     help='the bar to %(prog)s (default: %(default)s)')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: frobble [-h] [bar]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |     bar     the bar to frobble (default: 42)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							| 
									
										
										
										
											2012-01-03 18:37:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-26 14:17:19 +08:00
										 |  |  | As the help string supports %-formatting, if you want a literal ``%`` to appear
 | 
					
						
							|  |  |  | in the help string, you must escape it as ``%%``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | :mod:`!argparse` supports silencing the help entry for certain options, by
 | 
					
						
							| 
									
										
										
										
											2012-01-03 18:37:03 +01:00
										 |  |  | setting the ``help`` value to ``argparse.SUPPRESS``::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='frobble')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', help=argparse.SUPPRESS)
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: frobble [-h]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2012-01-03 18:37:03 +01:00
										 |  |  |      -h, --help  show this help message and exit
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _metavar:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | metavar
 | 
					
						
							|  |  |  | ^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-11 10:49:00 +01:00
										 |  |  | When :class:`ArgumentParser` generates help messages, it needs some way to refer
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | to each expected argument.  By default, :class:`!ArgumentParser` objects use the dest_
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | value as the "name" of each object.  By default, for positional argument
 | 
					
						
							|  |  |  | actions, the dest_ value is used directly, and for optional argument actions,
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | the dest_ value is uppercased.  So, a single positional argument with
 | 
					
						
							| 
									
										
										
										
											2011-11-11 10:57:01 +02:00
										 |  |  | ``dest='bar'`` will be referred to as ``bar``. A single
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  | optional argument ``--foo`` that should be followed by a single command-line argument
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | will be referred to as ``FOO``.  An example::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('X --foo Y'.split())
 | 
					
						
							|  |  |  |    Namespace(bar='X', foo='Y')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage:  [-h] [--foo FOO] bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |     bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     --foo FOO
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | An alternative name can be specified with ``metavar``::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', metavar='YYY')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', metavar='XXX')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('X --foo Y'.split())
 | 
					
						
							|  |  |  |    Namespace(bar='X', foo='Y')
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage:  [-h] [--foo YYY] XXX
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    positional arguments:
 | 
					
						
							|  |  |  |     XXX
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |     --foo YYY
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that ``metavar`` only changes the *displayed* name - the name of the
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | attribute on the :meth:`~ArgumentParser.parse_args` object is still determined
 | 
					
						
							|  |  |  | by the dest_ value.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Different values of ``nargs`` may cause the metavar to be used multiple times.
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Providing a tuple to ``metavar`` specifies a different display for each of the
 | 
					
						
							|  |  |  | arguments::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-x', nargs=2)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz'))
 | 
					
						
							|  |  |  |    >>> parser.print_help()
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-x X X] [--foo bar baz]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |    options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |     -h, --help     show this help message and exit
 | 
					
						
							|  |  |  |     -x X X
 | 
					
						
							|  |  |  |     --foo bar baz
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 21:46:18 -07:00
										 |  |  | .. _dest:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | dest
 | 
					
						
							|  |  |  | ^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Most :class:`ArgumentParser` actions add some value as an attribute of the
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | object returned by :meth:`~ArgumentParser.parse_args`.  The name of this
 | 
					
						
							|  |  |  | attribute is determined by the ``dest`` keyword argument of
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument`.  For positional argument actions,
 | 
					
						
							|  |  |  | ``dest`` is normally supplied as the first argument to
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['XXX'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(bar='XXX')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For optional argument actions, the value of ``dest`` is normally inferred from
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | the option strings.  :class:`ArgumentParser` generates the value of ``dest`` by
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | taking the first long option string and stripping away the initial ``--``
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | string.  If no long option strings were supplied, ``dest`` will be derived from
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | the first short option string by stripping the initial ``-`` character.  Any
 | 
					
						
							|  |  |  | internal ``-`` characters will be converted to ``_`` characters to make sure
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | the string is a valid attribute name.  The examples below illustrate this
 | 
					
						
							|  |  |  | behavior::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-f', '--foo-bar', '--foo')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-x', '-y')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('-f 1 -x 2'.split())
 | 
					
						
							|  |  |  |    Namespace(foo_bar='1', x='2')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('--foo 1 -y 2'.split())
 | 
					
						
							|  |  |  |    Namespace(foo_bar='1', x='2')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | ``dest`` allows a custom attribute name to be provided::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', dest='bar')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('--foo XXX'.split())
 | 
					
						
							|  |  |  |    Namespace(bar='XXX')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. _deprecated:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | deprecated
 | 
					
						
							|  |  |  | ^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | During a project's lifetime, some arguments may need to be removed from the
 | 
					
						
							|  |  |  | command line. Before removing them, you should inform
 | 
					
						
							|  |  |  | your users that the arguments are deprecated and will be removed.
 | 
					
						
							|  |  |  | The ``deprecated`` keyword argument of
 | 
					
						
							|  |  |  | :meth:`~ArgumentParser.add_argument`, which defaults to ``False``,
 | 
					
						
							|  |  |  | specifies if the argument is deprecated and will be removed
 | 
					
						
							|  |  |  | in the future.
 | 
					
						
							|  |  |  | For arguments, if ``deprecated`` is ``True``, then a warning will be
 | 
					
						
							| 
									
										
										
										
											2024-09-23 02:08:03 -07:00
										 |  |  | printed to :data:`sys.stderr` when the argument is used::
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> import argparse
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='snake.py')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--legs', default=0, type=int, deprecated=True)
 | 
					
						
							|  |  |  |    >>> parser.parse_args([])
 | 
					
						
							|  |  |  |    Namespace(legs=0)
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--legs', '4'])  # doctest: +SKIP
 | 
					
						
							|  |  |  |    snake.py: warning: option '--legs' is deprecated
 | 
					
						
							|  |  |  |    Namespace(legs=4)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-16 16:26:17 -05:00
										 |  |  | .. versionadded:: 3.13
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | Action classes
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | :class:`!Action` classes implement the Action API, a callable which returns a callable
 | 
					
						
							| 
									
										
										
										
											2014-07-20 10:52:46 -04:00
										 |  |  | which processes arguments from the command-line. Any object which follows
 | 
					
						
							|  |  |  | this API may be passed as the ``action`` parameter to
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  | :meth:`~ArgumentParser.add_argument`.
 | 
					
						
							| 
									
										
										
										
											2014-07-20 10:52:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-23 22:21:47 -04:00
										 |  |  | .. class:: Action(option_strings, dest, nargs=None, const=None, default=None, \
 | 
					
						
							|  |  |  |                   type=None, choices=None, required=False, help=None, \
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  |                   metavar=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    :class:`!Action` objects are used by an :class:`ArgumentParser` to represent the information
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    needed to parse a single argument from one or more strings from the
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    command line. The :class:`!Action` class must accept the two positional arguments
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    plus any keyword arguments passed to :meth:`ArgumentParser.add_argument`
 | 
					
						
							|  |  |  |    except for the ``action`` itself.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    Instances of :class:`!Action` (or return value of any callable to the
 | 
					
						
							|  |  |  |    ``action`` parameter) should have attributes :attr:`!dest`,
 | 
					
						
							|  |  |  |    :attr:`!option_strings`, :attr:`!default`, :attr:`!type`, :attr:`!required`,
 | 
					
						
							|  |  |  |    :attr:`!help`, etc. defined. The easiest way to ensure these attributes
 | 
					
						
							|  |  |  |    are defined is to call :meth:`!Action.__init__`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: __call__(parser, namespace, values, option_string=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       :class:`!Action` instances should be callable, so subclasses must override the
 | 
					
						
							|  |  |  |       :meth:`!__call__` method, which should accept four parameters:
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       * *parser* - The :class:`ArgumentParser` object which contains this action.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       * *namespace* - The :class:`Namespace` object that will be returned by
 | 
					
						
							|  |  |  |         :meth:`~ArgumentParser.parse_args`.  Most actions add an attribute to this
 | 
					
						
							|  |  |  |         object using :func:`setattr`.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       * *values* - The associated command-line arguments, with any type conversions
 | 
					
						
							|  |  |  |         applied.  Type conversions are specified with the type_ keyword argument to
 | 
					
						
							|  |  |  |         :meth:`~ArgumentParser.add_argument`.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       * *option_string* - The option string that was used to invoke this action.
 | 
					
						
							|  |  |  |         The ``option_string`` argument is optional, and will be absent if the action
 | 
					
						
							|  |  |  |         is associated with a positional argument.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       The :meth:`!__call__` method may perform arbitrary actions, but will typically set
 | 
					
						
							|  |  |  |       attributes on the ``namespace`` based on ``dest`` and ``values``.
 | 
					
						
							| 
									
										
										
										
											2011-12-13 23:36:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    .. method:: format_usage()
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       :class:`!Action` subclasses can define a :meth:`!format_usage` method that takes no argument
 | 
					
						
							|  |  |  |       and return a string which will be used when printing the usage of the program.
 | 
					
						
							|  |  |  |       If such method is not provided, a sensible default will be used.
 | 
					
						
							| 
									
										
										
										
											2014-07-20 10:52:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The parse_args() method
 | 
					
						
							|  |  |  | -----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.parse_args(args=None, namespace=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    Convert argument strings to objects and assign them as attributes of the
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    namespace.  Return the populated namespace.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Previous calls to :meth:`add_argument` determine exactly what objects are
 | 
					
						
							|  |  |  |    created and how they are assigned. See the documentation for
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    :meth:`!add_argument` for details.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 16:17:26 -04:00
										 |  |  |    * args_ - List of strings to parse.  The default is taken from
 | 
					
						
							|  |  |  |      :data:`sys.argv`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * namespace_ - An object to take the attributes.  The default is a new empty
 | 
					
						
							|  |  |  |      :class:`Namespace` object.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | Option value syntax
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | The :meth:`~ArgumentParser.parse_args` method supports several ways of
 | 
					
						
							|  |  |  | specifying the value of an option (if it takes one).  In the simplest case, the
 | 
					
						
							|  |  |  | option and its value are passed as two separate arguments::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-x')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['-x', 'X'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo=None, x='X')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['--foo', 'FOO'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo='FOO', x=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | For long options (options with names longer than a single character), the option
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  | and value can also be passed as a single command-line argument, using ``=`` to
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | separate them::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['--foo=FOO'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo='FOO', x=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | For short options (options only one character long), the option and its value
 | 
					
						
							|  |  |  | can be concatenated::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['-xX'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(foo=None, x='X')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | Several short options can be joined together, using only a single ``-`` prefix,
 | 
					
						
							|  |  |  | as long as only the last option (or none of them) requires a value::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-x', action='store_true')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-y', action='store_true')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-z')
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['-xyzZ'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(x=True, y=True, z='Z')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Invalid arguments
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | While parsing the command line, :meth:`~ArgumentParser.parse_args` checks for a
 | 
					
						
							|  |  |  | variety of errors, including ambiguous options, invalid types, invalid options,
 | 
					
						
							|  |  |  | wrong number of positional arguments, etc.  When it encounters such an error,
 | 
					
						
							|  |  |  | it exits and prints the error along with a usage message::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('--foo', type=int)
 | 
					
						
							|  |  |  |    >>> parser.add_argument('bar', nargs='?')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # invalid type
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--foo', 'spam'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [--foo FOO] [bar]
 | 
					
						
							|  |  |  |    PROG: error: argument --foo: invalid int value: 'spam'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # invalid option
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--bar'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [--foo FOO] [bar]
 | 
					
						
							|  |  |  |    PROG: error: no such option: --bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # wrong number of arguments
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['spam', 'badger'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [--foo FOO] [bar]
 | 
					
						
							|  |  |  |    PROG: error: extra arguments found: badger
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | Arguments containing ``-``
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | The :meth:`~ArgumentParser.parse_args` method attempts to give errors whenever
 | 
					
						
							|  |  |  | the user has clearly made a mistake, but some situations are inherently
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | ambiguous.  For example, the command-line argument ``-1`` could either be an
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | attempt to specify an option or an attempt to provide a positional argument.
 | 
					
						
							|  |  |  | The :meth:`~ArgumentParser.parse_args` method is cautious here: positional
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | arguments may only begin with ``-`` if they look like negative numbers and
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | there are no options in the parser that look like negative numbers::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-x')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('foo', nargs='?')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # no negative number options, so -1 is a positional argument
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-x', '-1'])
 | 
					
						
							|  |  |  |    Namespace(foo=None, x='-1')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # no negative number options, so -1 and -5 are positional arguments
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-x', '-1', '-5'])
 | 
					
						
							|  |  |  |    Namespace(foo='-5', x='-1')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-1', dest='one')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('foo', nargs='?')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # negative number options present, so -1 is an option
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-1', 'X'])
 | 
					
						
							|  |  |  |    Namespace(foo=None, one='X')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # negative number options present, so -2 is an option
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-2'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-1 ONE] [foo]
 | 
					
						
							|  |  |  |    PROG: error: no such option: -2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    >>> # negative number options present, so both -1s are options
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['-1', '-1'])
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-1 ONE] [foo]
 | 
					
						
							|  |  |  |    PROG: error: argument -1: expected one argument
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  | If you have positional arguments that must begin with ``-`` and don't look
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | like negative numbers, you can insert the pseudo-argument ``'--'`` which tells
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | :meth:`~ArgumentParser.parse_args` that everything after that is a positional
 | 
					
						
							|  |  |  | argument::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser.parse_args(['--', '-f'])
 | 
					
						
							|  |  |  |    Namespace(foo='-f', one=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-13 07:17:07 -07:00
										 |  |  | See also :ref:`the argparse howto on ambiguous arguments <specifying-ambiguous-arguments>`
 | 
					
						
							|  |  |  | for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 05:49:54 -08:00
										 |  |  | .. _prefix-matching:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 05:49:54 -08:00
										 |  |  | Argument abbreviations (prefix matching)
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  | The :meth:`~ArgumentParser.parse_args` method :ref:`by default <allow_abbrev>`
 | 
					
						
							|  |  |  | allows long options to be abbreviated to a prefix, if the abbreviation is
 | 
					
						
							|  |  |  | unambiguous (the prefix matches a unique option)::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-bacon')
 | 
					
						
							|  |  |  |    >>> parser.add_argument('-badger')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('-bac MMM'.split())
 | 
					
						
							|  |  |  |    Namespace(bacon='MMM', badger=None)
 | 
					
						
							|  |  |  |    >>> parser.parse_args('-bad WOOD'.split())
 | 
					
						
							|  |  |  |    Namespace(bacon=None, badger='WOOD')
 | 
					
						
							|  |  |  |    >>> parser.parse_args('-ba BA'.split())
 | 
					
						
							|  |  |  |    usage: PROG [-h] [-bacon BACON] [-badger BADGER]
 | 
					
						
							|  |  |  |    PROG: error: ambiguous option: -ba could match -badger, -bacon
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | An error is produced for arguments that could produce more than one options.
 | 
					
						
							| 
									
										
										
										
											2015-02-14 01:39:17 +02:00
										 |  |  | This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 16:17:26 -04:00
										 |  |  | .. _args:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Beyond ``sys.argv``
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | Sometimes it may be useful to have an :class:`ArgumentParser` parse arguments other than those
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | of :data:`sys.argv`.  This can be accomplished by passing a list of strings to
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | :meth:`~ArgumentParser.parse_args`.  This is useful for testing at the
 | 
					
						
							|  |  |  | interactive prompt::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |    >>> parser.add_argument(
 | 
					
						
							| 
									
										
										
										
											2011-03-03 05:27:17 +00:00
										 |  |  |    ...     'integers', metavar='int', type=int, choices=range(10),
 | 
					
						
							| 
									
										
										
										
											2016-05-10 12:01:23 +03:00
										 |  |  |    ...     nargs='+', help='an integer in the range 0..9')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    >>> parser.add_argument(
 | 
					
						
							|  |  |  |    ...     '--sum', dest='accumulate', action='store_const', const=sum,
 | 
					
						
							| 
									
										
										
										
											2016-05-10 12:01:23 +03:00
										 |  |  |    ...     default=max, help='sum the integers (default: find the max)')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    >>> parser.parse_args(['1', '2', '3', '4'])
 | 
					
						
							|  |  |  |    Namespace(accumulate=<built-in function max>, integers=[1, 2, 3, 4])
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:41:25 +00:00
										 |  |  |    >>> parser.parse_args(['1', '2', '3', '4', '--sum'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 16:17:26 -04:00
										 |  |  | .. _namespace:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 19:50:06 +01:00
										 |  |  | The Namespace object
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-29 17:59:17 +02:00
										 |  |  | .. class:: Namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Simple class used by default by :meth:`~ArgumentParser.parse_args` to create
 | 
					
						
							|  |  |  |    an object holding attributes and return it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    This class is deliberately simple, just an :class:`object` subclass with a
 | 
					
						
							|  |  |  |    readable string representation. If you prefer to have dict-like view of the
 | 
					
						
							|  |  |  |    attributes, you can use the standard Python idiom, :func:`vars`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.add_argument('--foo')
 | 
					
						
							|  |  |  |       >>> args = parser.parse_args(['--foo', 'BAR'])
 | 
					
						
							|  |  |  |       >>> vars(args)
 | 
					
						
							|  |  |  |       {'foo': 'BAR'}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    It may also be useful to have an :class:`ArgumentParser` assign attributes to an
 | 
					
						
							|  |  |  |    already existing object, rather than a new :class:`Namespace` object.  This can
 | 
					
						
							|  |  |  |    be achieved by specifying the ``namespace=`` keyword argument::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       >>> class C:
 | 
					
						
							|  |  |  |       ...     pass
 | 
					
						
							|  |  |  |       ...
 | 
					
						
							|  |  |  |       >>> c = C()
 | 
					
						
							|  |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.add_argument('--foo')
 | 
					
						
							|  |  |  |       >>> parser.parse_args(args=['--foo', 'BAR'], namespace=c)
 | 
					
						
							|  |  |  |       >>> c.foo
 | 
					
						
							|  |  |  |       'BAR'
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Other utilities
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sub-commands
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  | .. method:: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  |                                           [parser_class], [action], \
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |                                           [dest], [required], \
 | 
					
						
							| 
									
										
										
										
											2017-09-20 14:35:27 -07:00
										 |  |  |                                           [help], [metavar])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    Many programs split up their functionality into a number of subcommands,
 | 
					
						
							|  |  |  |    for example, the ``svn`` program can invoke subcommands like ``svn
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    checkout``, ``svn update``, and ``svn commit``.  Splitting up functionality
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    this way can be a particularly good idea when a program performs several
 | 
					
						
							|  |  |  |    different functions which require different kinds of command-line arguments.
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    :class:`ArgumentParser` supports the creation of such subcommands with the
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    :meth:`!add_subparsers` method.  The :meth:`!add_subparsers` method is normally
 | 
					
						
							| 
									
										
										
										
											2012-12-28 01:59:24 +02:00
										 |  |  |    called with no arguments and returns a special action object.  This object
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  |    has a single method, :meth:`~_SubParsersAction.add_parser`, which takes a
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    command name and any :class:`!ArgumentParser` constructor arguments, and
 | 
					
						
							|  |  |  |    returns an :class:`!ArgumentParser` object that can be modified as usual.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  |    Description of parameters:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    * *title* - title for the sub-parser group in help output; by default
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  |      "subcommands" if description is provided, otherwise uses title for
 | 
					
						
							|  |  |  |      positional arguments
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    * *description* - description for the sub-parser group in help output, by
 | 
					
						
							| 
									
										
										
										
											2016-10-19 16:29:26 +03:00
										 |  |  |      default ``None``
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    * *prog* - usage information that will be displayed with sub-command help,
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  |      by default the name of the program and any positional arguments before the
 | 
					
						
							|  |  |  |      subparser argument
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    * *parser_class* - class which will be used to create sub-parser instances, by
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |      default the class of the current parser (e.g. :class:`ArgumentParser`)
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 06:45:53 +02:00
										 |  |  |    * action_ - the basic type of action to be taken when this argument is
 | 
					
						
							|  |  |  |      encountered at the command line
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * dest_ - name of the attribute under which sub-command name will be
 | 
					
						
							| 
									
										
										
										
											2016-10-19 16:29:26 +03:00
										 |  |  |      stored; by default ``None`` and no value is stored
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 14:35:27 -07:00
										 |  |  |    * required_ - Whether or not a subcommand must be provided, by default
 | 
					
						
							| 
									
										
										
										
											2019-10-06 21:08:48 -05:00
										 |  |  |      ``False`` (added in 3.7)
 | 
					
						
							| 
									
										
										
										
											2017-09-20 14:35:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 16:29:26 +03:00
										 |  |  |    * help_ - help for sub-parser group in help output, by default ``None``
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    * metavar_ - string presenting available subcommands in help; by default it
 | 
					
						
							|  |  |  |      is ``None`` and presents subcommands in form {cmd1, cmd2, ..}
 | 
					
						
							| 
									
										
										
										
											2013-10-06 18:51:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    Some example usage::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> # create the top-level parser
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', action='store_true', help='foo help')
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |      >>> subparsers = parser.add_subparsers(help='subcommand help')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # create the parser for the "a" command
 | 
					
						
							|  |  |  |      >>> parser_a = subparsers.add_parser('a', help='a help')
 | 
					
						
							|  |  |  |      >>> parser_a.add_argument('bar', type=int, help='bar help')
 | 
					
						
							|  |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # create the parser for the "b" command
 | 
					
						
							|  |  |  |      >>> parser_b = subparsers.add_parser('b', help='b help')
 | 
					
						
							| 
									
										
										
										
											2024-09-29 10:57:21 +03:00
										 |  |  |      >>> parser_b.add_argument('--baz', choices=('X', 'Y', 'Z'), help='baz help')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>>
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:30:26 +02:00
										 |  |  |      >>> # parse some argument lists
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>> parser.parse_args(['a', '12'])
 | 
					
						
							|  |  |  |      Namespace(bar=12, foo=False)
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])
 | 
					
						
							|  |  |  |      Namespace(baz='Z', foo=True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Note that the object returned by :meth:`parse_args` will only contain
 | 
					
						
							|  |  |  |    attributes for the main parser and the subparser that was selected by the
 | 
					
						
							|  |  |  |    command line (and not any other subparsers).  So in the example above, when
 | 
					
						
							| 
									
										
										
										
											2011-08-19 01:45:12 +02:00
										 |  |  |    the ``a`` command is specified, only the ``foo`` and ``bar`` attributes are
 | 
					
						
							|  |  |  |    present, and when the ``b`` command is specified, only the ``foo`` and
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    ``baz`` attributes are present.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Similarly, when a help message is requested from a subparser, only the help
 | 
					
						
							|  |  |  |    for that particular parser will be printed.  The help message will not
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    include parent parser or sibling parser messages.  (A help message for each
 | 
					
						
							|  |  |  |    subparser command, however, can be given by supplying the ``help=`` argument
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  |    to :meth:`~_SubParsersAction.add_parser` as above.)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    ::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['--help'])
 | 
					
						
							|  |  |  |      usage: PROG [-h] [--foo] {a,b} ...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      positional arguments:
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |        {a,b}   subcommand help
 | 
					
						
							| 
									
										
										
										
											2013-01-12 10:39:45 +02:00
										 |  |  |          a     a help
 | 
					
						
							|  |  |  |          b     b help
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |      options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |        -h, --help  show this help message and exit
 | 
					
						
							|  |  |  |        --foo   foo help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['a', '--help'])
 | 
					
						
							|  |  |  |      usage: PROG a [-h] bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      positional arguments:
 | 
					
						
							|  |  |  |        bar     bar help
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |      options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |        -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['b', '--help'])
 | 
					
						
							|  |  |  |      usage: PROG b [-h] [--baz {X,Y,Z}]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |      options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |        -h, --help     show this help message and exit
 | 
					
						
							|  |  |  |        --baz {X,Y,Z}  baz help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :meth:`add_subparsers` method also supports ``title`` and ``description``
 | 
					
						
							|  |  |  |    keyword arguments.  When either is present, the subparser's commands will
 | 
					
						
							|  |  |  |    appear in their own group in the help output.  For example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> subparsers = parser.add_subparsers(title='subcommands',
 | 
					
						
							|  |  |  |      ...                                    description='valid subcommands',
 | 
					
						
							|  |  |  |      ...                                    help='additional help')
 | 
					
						
							|  |  |  |      >>> subparsers.add_parser('foo')
 | 
					
						
							|  |  |  |      >>> subparsers.add_parser('bar')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['-h'])
 | 
					
						
							|  |  |  |      usage:  [-h] {foo,bar} ...
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 09:40:56 -08:00
										 |  |  |      options:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |        -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      subcommands:
 | 
					
						
							|  |  |  |        valid subcommands
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        {foo,bar}   additional help
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  |    Furthermore, :meth:`~_SubParsersAction.add_parser` supports an additional
 | 
					
						
							|  |  |  |    *aliases* argument,
 | 
					
						
							| 
									
										
										
										
											2010-12-18 11:19:23 +00:00
										 |  |  |    which allows multiple strings to refer to the same subparser. This example,
 | 
					
						
							|  |  |  |    like ``svn``, aliases ``co`` as a shorthand for ``checkout``::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> subparsers = parser.add_subparsers()
 | 
					
						
							|  |  |  |      >>> checkout = subparsers.add_parser('checkout', aliases=['co'])
 | 
					
						
							|  |  |  |      >>> checkout.add_argument('foo')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['co', 'bar'])
 | 
					
						
							|  |  |  |      Namespace(foo='bar')
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 00:41:34 +02:00
										 |  |  |    :meth:`~_SubParsersAction.add_parser` supports also an additional
 | 
					
						
							|  |  |  |    *deprecated* argument, which allows to deprecate the subparser.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       >>> import argparse
 | 
					
						
							|  |  |  |       >>> parser = argparse.ArgumentParser(prog='chicken.py')
 | 
					
						
							|  |  |  |       >>> subparsers = parser.add_subparsers()
 | 
					
						
							|  |  |  |       >>> run = subparsers.add_parser('run')
 | 
					
						
							|  |  |  |       >>> fly = subparsers.add_parser('fly', deprecated=True)
 | 
					
						
							|  |  |  |       >>> parser.parse_args(['fly'])  # doctest: +SKIP
 | 
					
						
							|  |  |  |       chicken.py: warning: command 'fly' is deprecated
 | 
					
						
							|  |  |  |       Namespace()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.13
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    One particularly effective way of handling subcommands is to combine the use
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so
 | 
					
						
							|  |  |  |    that each subparser knows which Python function it should execute.  For
 | 
					
						
							|  |  |  |    example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |      >>> # subcommand functions
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>> def foo(args):
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:09:18 +00:00
										 |  |  |      ...     print(args.x * args.y)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      ...
 | 
					
						
							|  |  |  |      >>> def bar(args):
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:09:18 +00:00
										 |  |  |      ...     print('((%s))' % args.z)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      ...
 | 
					
						
							|  |  |  |      >>> # create the top-level parser
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							| 
									
										
										
										
											2023-02-18 19:06:03 -06:00
										 |  |  |      >>> subparsers = parser.add_subparsers(required=True)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # create the parser for the "foo" command
 | 
					
						
							|  |  |  |      >>> parser_foo = subparsers.add_parser('foo')
 | 
					
						
							|  |  |  |      >>> parser_foo.add_argument('-x', type=int, default=1)
 | 
					
						
							|  |  |  |      >>> parser_foo.add_argument('y', type=float)
 | 
					
						
							|  |  |  |      >>> parser_foo.set_defaults(func=foo)
 | 
					
						
							|  |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # create the parser for the "bar" command
 | 
					
						
							|  |  |  |      >>> parser_bar = subparsers.add_parser('bar')
 | 
					
						
							|  |  |  |      >>> parser_bar.add_argument('z')
 | 
					
						
							|  |  |  |      >>> parser_bar.set_defaults(func=bar)
 | 
					
						
							|  |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # parse the args and call whatever function was selected
 | 
					
						
							|  |  |  |      >>> args = parser.parse_args('foo 1 -x 2'.split())
 | 
					
						
							|  |  |  |      >>> args.func(args)
 | 
					
						
							|  |  |  |      2.0
 | 
					
						
							|  |  |  |      >>>
 | 
					
						
							|  |  |  |      >>> # parse the args and call whatever function was selected
 | 
					
						
							|  |  |  |      >>> args = parser.parse_args('bar XYZYX'.split())
 | 
					
						
							|  |  |  |      >>> args.func(args)
 | 
					
						
							|  |  |  |      ((XYZYX))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-18 11:19:23 +00:00
										 |  |  |    This way, you can let :meth:`parse_args` do the job of calling the
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    appropriate function after argument parsing is complete.  Associating
 | 
					
						
							|  |  |  |    functions with actions like this is typically the easiest way to handle the
 | 
					
						
							|  |  |  |    different actions for each of your subparsers.  However, if it is necessary
 | 
					
						
							|  |  |  |    to check the name of the subparser that was invoked, the ``dest`` keyword
 | 
					
						
							|  |  |  |    argument to the :meth:`add_subparsers` call will work::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> subparsers = parser.add_subparsers(dest='subparser_name')
 | 
					
						
							|  |  |  |      >>> subparser1 = subparsers.add_parser('1')
 | 
					
						
							|  |  |  |      >>> subparser1.add_argument('-x')
 | 
					
						
							|  |  |  |      >>> subparser2 = subparsers.add_parser('2')
 | 
					
						
							|  |  |  |      >>> subparser2.add_argument('y')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['2', 'frobble'])
 | 
					
						
							|  |  |  |      Namespace(subparser_name='2', y='frobble')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 21:08:48 -05:00
										 |  |  |    .. versionchanged:: 3.7
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       New *required* keyword-only parameter.
 | 
					
						
							| 
									
										
										
										
											2019-10-06 21:08:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 17:29:33 +02:00
										 |  |  |    .. versionchanged:: 3.14
 | 
					
						
							|  |  |  |       Subparser's *prog* is no longer affected by a custom usage message in
 | 
					
						
							|  |  |  |       the main parser.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | FileType objects
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 22:39:32 +02:00
										 |  |  | .. class:: FileType(mode='r', bufsize=-1, encoding=None, errors=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The :class:`FileType` factory creates objects that can be passed to the type
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    argument of :meth:`ArgumentParser.add_argument`.  Arguments that have
 | 
					
						
							| 
									
										
										
										
											2012-12-15 22:39:32 +02:00
										 |  |  |    :class:`FileType` objects as their type will open command-line arguments as
 | 
					
						
							|  |  |  |    files with the requested modes, buffer sizes, encodings and error handling
 | 
					
						
							|  |  |  |    (see the :func:`open` function for more details)::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 01:44:55 +01:00
										 |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							| 
									
										
										
										
											2012-12-15 22:39:32 +02:00
										 |  |  |       >>> parser.add_argument('--raw', type=argparse.FileType('wb', 0))
 | 
					
						
							|  |  |  |       >>> parser.add_argument('out', type=argparse.FileType('w', encoding='UTF-8'))
 | 
					
						
							|  |  |  |       >>> parser.parse_args(['--raw', 'raw.dat', 'file.txt'])
 | 
					
						
							|  |  |  |       Namespace(out=<_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'>, raw=<_io.FileIO name='raw.dat' mode='wb'>)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    FileType objects understand the pseudo-argument ``'-'`` and automatically
 | 
					
						
							| 
									
										
										
										
											2022-10-28 07:15:39 -05:00
										 |  |  |    convert this into :data:`sys.stdin` for readable :class:`FileType` objects and
 | 
					
						
							|  |  |  |    :data:`sys.stdout` for writable :class:`FileType` objects::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 01:44:55 +01:00
										 |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.add_argument('infile', type=argparse.FileType('r'))
 | 
					
						
							|  |  |  |       >>> parser.parse_args(['-'])
 | 
					
						
							|  |  |  |       Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:50:29 +03:00
										 |  |  |    .. note::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       If one argument uses *FileType* and then a subsequent argument fails,
 | 
					
						
							|  |  |  |       an error is reported but the file is not automatically closed.
 | 
					
						
							|  |  |  |       This can also clobber the output files.
 | 
					
						
							|  |  |  |       In this case, it would be better to wait until after the parser has
 | 
					
						
							|  |  |  |       run and then use the :keyword:`with`-statement to manage the files.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 22:40:26 +01:00
										 |  |  |    .. versionchanged:: 3.4
 | 
					
						
							|  |  |  |       Added the *encodings* and *errors* parameters.
 | 
					
						
							| 
									
										
										
										
											2013-12-31 11:18:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:50:29 +03:00
										 |  |  |    .. deprecated:: 3.14
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Argument groups
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 08:48:28 -07:00
										 |  |  | .. method:: ArgumentParser.add_argument_group(title=None, description=None, *, \
 | 
					
						
							|  |  |  |                                               [argument_default], [conflict_handler])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    By default, :class:`ArgumentParser` groups command-line arguments into
 | 
					
						
							| 
									
										
										
										
											2022-11-23 02:46:06 +05:30
										 |  |  |    "positional arguments" and "options" when displaying help
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    messages. When there is a better conceptual grouping of arguments than this
 | 
					
						
							|  |  |  |    default one, appropriate groups can be created using the
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    :meth:`!add_argument_group` method::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
 | 
					
						
							|  |  |  |      >>> group = parser.add_argument_group('group')
 | 
					
						
							|  |  |  |      >>> group.add_argument('--foo', help='foo help')
 | 
					
						
							|  |  |  |      >>> group.add_argument('bar', help='bar help')
 | 
					
						
							|  |  |  |      >>> parser.print_help()
 | 
					
						
							|  |  |  |      usage: PROG [--foo FOO] bar
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      group:
 | 
					
						
							|  |  |  |        bar    bar help
 | 
					
						
							|  |  |  |        --foo FOO  foo help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :meth:`add_argument_group` method returns an argument group object which
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    has an :meth:`~ArgumentParser.add_argument` method just like a regular
 | 
					
						
							|  |  |  |    :class:`ArgumentParser`.  When an argument is added to the group, the parser
 | 
					
						
							|  |  |  |    treats it just like a normal argument, but displays the argument in a
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    separate group for help messages.  The :meth:`!add_argument_group` method
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  |    accepts *title* and *description* arguments which can be used to
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    customize this display::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
 | 
					
						
							|  |  |  |      >>> group1 = parser.add_argument_group('group1', 'group1 description')
 | 
					
						
							|  |  |  |      >>> group1.add_argument('foo', help='foo help')
 | 
					
						
							|  |  |  |      >>> group2 = parser.add_argument_group('group2', 'group2 description')
 | 
					
						
							|  |  |  |      >>> group2.add_argument('--bar', help='bar help')
 | 
					
						
							|  |  |  |      >>> parser.print_help()
 | 
					
						
							|  |  |  |      usage: PROG [--bar BAR] foo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      group1:
 | 
					
						
							|  |  |  |        group1 description
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        foo    foo help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      group2:
 | 
					
						
							|  |  |  |        group2 description
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        --bar BAR  bar help
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 08:48:28 -07:00
										 |  |  |    The optional, keyword-only parameters argument_default_ and conflict_handler_
 | 
					
						
							|  |  |  |    allow for finer-grained control of the behavior of the argument group. These
 | 
					
						
							|  |  |  |    parameters have the same meaning as in the :class:`ArgumentParser` constructor,
 | 
					
						
							|  |  |  |    but apply specifically to the argument group rather than the entire parser.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 19:36:23 +02:00
										 |  |  |    Note that any arguments not in your user-defined groups will end up back
 | 
					
						
							|  |  |  |    in the usual "positional arguments" and "optional arguments" sections.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 07:20:37 -08:00
										 |  |  |    .. deprecated-removed:: 3.11 3.14
 | 
					
						
							|  |  |  |       Calling :meth:`add_argument_group` on an argument group now raises an
 | 
					
						
							|  |  |  |       exception. This nesting was never supported, often failed to work
 | 
					
						
							|  |  |  |       correctly, and was unintentionally exposed through inheritance.
 | 
					
						
							| 
									
										
										
										
											2021-12-16 15:31:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 02:11:47 -07:00
										 |  |  |    .. deprecated:: 3.14
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |       Passing prefix_chars_ to :meth:`add_argument_group`
 | 
					
						
							|  |  |  |       is now deprecated.
 | 
					
						
							| 
									
										
										
										
											2024-10-17 02:11:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Mutual exclusion
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-06 13:09:59 +02:00
										 |  |  | .. method:: ArgumentParser.add_mutually_exclusive_group(required=False)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    Create a mutually exclusive group. :mod:`!argparse` will make sure that only
 | 
					
						
							| 
									
										
										
										
											2011-04-21 16:12:17 +03:00
										 |  |  |    one of the arguments in the mutually exclusive group was present on the
 | 
					
						
							|  |  |  |    command line::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |      >>> group = parser.add_mutually_exclusive_group()
 | 
					
						
							|  |  |  |      >>> group.add_argument('--foo', action='store_true')
 | 
					
						
							|  |  |  |      >>> group.add_argument('--bar', action='store_false')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['--foo'])
 | 
					
						
							|  |  |  |      Namespace(bar=True, foo=True)
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['--bar'])
 | 
					
						
							|  |  |  |      Namespace(bar=False, foo=False)
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['--foo', '--bar'])
 | 
					
						
							|  |  |  |      usage: PROG [-h] [--foo | --bar]
 | 
					
						
							|  |  |  |      PROG: error: argument --bar: not allowed with argument --foo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  |    The :meth:`add_mutually_exclusive_group` method also accepts a *required*
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    argument, to indicate that at least one of the mutually exclusive arguments
 | 
					
						
							|  |  |  |    is required::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |      >>> group = parser.add_mutually_exclusive_group(required=True)
 | 
					
						
							|  |  |  |      >>> group.add_argument('--foo', action='store_true')
 | 
					
						
							|  |  |  |      >>> group.add_argument('--bar', action='store_false')
 | 
					
						
							|  |  |  |      >>> parser.parse_args([])
 | 
					
						
							|  |  |  |      usage: PROG [-h] (--foo | --bar)
 | 
					
						
							|  |  |  |      PROG: error: one of the arguments --foo --bar is required
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Note that currently mutually exclusive argument groups do not support the
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  |    *title* and *description* arguments of
 | 
					
						
							| 
									
										
										
										
											2022-09-18 01:05:43 -07:00
										 |  |  |    :meth:`~ArgumentParser.add_argument_group`. However, a mutually exclusive
 | 
					
						
							|  |  |  |    group can be added to an argument group that has a title and description.
 | 
					
						
							|  |  |  |    For example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser(prog='PROG')
 | 
					
						
							|  |  |  |      >>> group = parser.add_argument_group('Group title', 'Group description')
 | 
					
						
							|  |  |  |      >>> exclusive_group = group.add_mutually_exclusive_group(required=True)
 | 
					
						
							|  |  |  |      >>> exclusive_group.add_argument('--foo', help='foo help')
 | 
					
						
							|  |  |  |      >>> exclusive_group.add_argument('--bar', help='bar help')
 | 
					
						
							|  |  |  |      >>> parser.print_help()
 | 
					
						
							|  |  |  |      usage: PROG [-h] (--foo FOO | --bar BAR)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      options:
 | 
					
						
							|  |  |  |        -h, --help  show this help message and exit
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      Group title:
 | 
					
						
							|  |  |  |        Group description
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        --foo FOO   foo help
 | 
					
						
							|  |  |  |        --bar BAR   bar help
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 07:20:37 -08:00
										 |  |  |    .. deprecated-removed:: 3.11 3.14
 | 
					
						
							|  |  |  |       Calling :meth:`add_argument_group` or :meth:`add_mutually_exclusive_group`
 | 
					
						
							|  |  |  |       on a mutually exclusive group now raises an exception. This nesting was
 | 
					
						
							|  |  |  |       never supported, often failed to work correctly, and was unintentionally
 | 
					
						
							|  |  |  |       exposed through inheritance.
 | 
					
						
							| 
									
										
										
										
											2021-12-16 15:31:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Parser defaults
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | .. method:: ArgumentParser.set_defaults(**kwargs)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Most of the time, the attributes of the object returned by :meth:`parse_args`
 | 
					
						
							| 
									
										
										
										
											2011-08-10 04:19:03 +02:00
										 |  |  |    will be fully determined by inspecting the command-line arguments and the argument
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  |    actions.  :meth:`set_defaults` allows some additional
 | 
					
						
							| 
									
										
										
										
											2011-04-16 16:44:54 +02:00
										 |  |  |    attributes that are determined without any inspection of the command line to
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    be added::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('foo', type=int)
 | 
					
						
							|  |  |  |      >>> parser.set_defaults(bar=42, baz='badger')
 | 
					
						
							|  |  |  |      >>> parser.parse_args(['736'])
 | 
					
						
							|  |  |  |      Namespace(bar=42, baz='badger', foo=736)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    Note that parser-level defaults always override argument-level defaults::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', default='bar')
 | 
					
						
							|  |  |  |      >>> parser.set_defaults(foo='spam')
 | 
					
						
							|  |  |  |      >>> parser.parse_args([])
 | 
					
						
							|  |  |  |      Namespace(foo='spam')
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    Parser-level defaults can be particularly useful when working with multiple
 | 
					
						
							|  |  |  |    parsers.  See the :meth:`~ArgumentParser.add_subparsers` method for an
 | 
					
						
							|  |  |  |    example of this type.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | .. method:: ArgumentParser.get_default(dest)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Get the default value for a namespace attribute, as set by either
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    :meth:`~ArgumentParser.add_argument` or by
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.set_defaults`::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |      >>> parser.add_argument('--foo', default='badger')
 | 
					
						
							|  |  |  |      >>> parser.get_default('foo')
 | 
					
						
							|  |  |  |      'badger'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Printing help
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-22 01:42:10 +03:00
										 |  |  | In most typical applications, :meth:`~ArgumentParser.parse_args` will take
 | 
					
						
							|  |  |  | care of formatting and printing any usage or error messages.  However, several
 | 
					
						
							|  |  |  | formatting methods are available:
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.print_usage(file=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Print a brief description of how the :class:`ArgumentParser` should be
 | 
					
						
							| 
									
										
										
										
											2010-12-18 16:39:06 +00:00
										 |  |  |    invoked on the command line.  If *file* is ``None``, :data:`sys.stdout` is
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    assumed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.print_help(file=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Print a help message, including the program usage and information about the
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  |    arguments registered with the :class:`ArgumentParser`.  If *file* is
 | 
					
						
							| 
									
										
										
										
											2010-12-18 16:39:06 +00:00
										 |  |  |    ``None``, :data:`sys.stdout` is assumed.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | There are also variants of these methods that simply return a string instead of
 | 
					
						
							|  |  |  | printing it:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.format_usage()
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Return a string containing a brief description of how the
 | 
					
						
							|  |  |  |    :class:`ArgumentParser` should be invoked on the command line.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.format_help()
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Return a string containing a help message, including the program usage and
 | 
					
						
							|  |  |  |    information about the arguments registered with the :class:`ArgumentParser`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Partial parsing
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  | .. method:: ArgumentParser.parse_known_args(args=None, namespace=None)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    Sometimes a script may only parse a few of the command-line arguments, passing
 | 
					
						
							|  |  |  |    the remaining arguments on to another script or program. In these cases, the
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.parse_known_args` method can be useful.  It works much like
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.parse_args` except that it does not produce an error when
 | 
					
						
							|  |  |  |    extra arguments are present.  Instead, it returns a two item tuple containing
 | 
					
						
							|  |  |  |    the populated namespace and the list of remaining argument strings.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    ::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.add_argument('--foo', action='store_true')
 | 
					
						
							|  |  |  |       >>> parser.add_argument('bar')
 | 
					
						
							|  |  |  |       >>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam'])
 | 
					
						
							|  |  |  |       (Namespace(bar='BAR', foo=True), ['--badger', 'spam'])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 05:49:54 -08:00
										 |  |  | .. warning::
 | 
					
						
							|  |  |  |    :ref:`Prefix matching <prefix-matching>` rules apply to
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  |    :meth:`~ArgumentParser.parse_known_args`. The parser may consume an option even if it's just
 | 
					
						
							| 
									
										
										
										
											2013-12-02 05:49:54 -08:00
										 |  |  |    a prefix of one of its known options, instead of leaving it in the remaining
 | 
					
						
							|  |  |  |    arguments list.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Customizing file parsing
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  | .. method:: ArgumentParser.convert_arg_line_to_args(arg_line)
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  |    Arguments that are read from a file (see the *fromfile_prefix_chars*
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    keyword argument to the :class:`ArgumentParser` constructor) are read one
 | 
					
						
							| 
									
										
										
										
											2014-05-20 12:58:38 -04:00
										 |  |  |    argument per line. :meth:`convert_arg_line_to_args` can be overridden for
 | 
					
						
							| 
									
										
										
										
											2010-03-03 02:07:08 +00:00
										 |  |  |    fancier reading.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:34:28 +00:00
										 |  |  |    This method takes a single argument *arg_line* which is a string read from
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  |    the argument file.  It returns a list of arguments parsed from this string.
 | 
					
						
							|  |  |  |    The method is called once per line read from the argument file, in order.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A useful override of this method is one that treats each space-separated word
 | 
					
						
							| 
									
										
										
										
											2016-10-17 06:14:17 +03:00
										 |  |  |    as an argument.  The following example demonstrates how to do this::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 06:14:17 +03:00
										 |  |  |     class MyArgumentParser(argparse.ArgumentParser):
 | 
					
						
							|  |  |  |         def convert_arg_line_to_args(self, arg_line):
 | 
					
						
							|  |  |  |             return arg_line.split()
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:28:04 +00:00
										 |  |  | Exiting methods
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: ArgumentParser.exit(status=0, message=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This method terminates the program, exiting with the specified *status*
 | 
					
						
							| 
									
										
										
										
											2024-09-23 02:08:03 -07:00
										 |  |  |    and, if given, it prints a *message* to :data:`sys.stderr` before that.
 | 
					
						
							|  |  |  |    The user can override this method to handle these steps differently::
 | 
					
						
							| 
									
										
										
										
											2019-09-12 10:34:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class ErrorCatchingArgumentParser(argparse.ArgumentParser):
 | 
					
						
							|  |  |  |         def exit(self, status=0, message=None):
 | 
					
						
							|  |  |  |             if status:
 | 
					
						
							|  |  |  |                 raise Exception(f'Exiting because of an error: {message}')
 | 
					
						
							|  |  |  |             exit(status)
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:28:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: ArgumentParser.error(message)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 02:08:03 -07:00
										 |  |  |    This method prints a usage message, including the *message*, to
 | 
					
						
							|  |  |  |    :data:`sys.stderr` and terminates the program with a status code of 2.
 | 
					
						
							| 
									
										
										
										
											2010-10-17 10:28:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 20:25:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Intermixed parsing
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: ArgumentParser.parse_intermixed_args(args=None, namespace=None)
 | 
					
						
							|  |  |  | .. method:: ArgumentParser.parse_known_intermixed_args(args=None, namespace=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    A number of Unix commands allow the user to intermix optional arguments with
 | 
					
						
							|  |  |  |    positional arguments.  The :meth:`~ArgumentParser.parse_intermixed_args`
 | 
					
						
							|  |  |  |    and :meth:`~ArgumentParser.parse_known_intermixed_args` methods
 | 
					
						
							|  |  |  |    support this parsing style.
 | 
					
						
							| 
									
										
										
										
											2014-09-26 15:34:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 10:50:12 +02:00
										 |  |  |    These parsers do not support all the :mod:`!argparse` features, and will raise
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    exceptions if unsupported features are used.  In particular, subparsers,
 | 
					
						
							|  |  |  |    and mutually exclusive groups that include both
 | 
					
						
							|  |  |  |    optionals and positionals are not supported.
 | 
					
						
							| 
									
										
										
										
											2014-09-26 15:34:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    The following example shows the difference between
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.parse_known_args` and
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.parse_intermixed_args`: the former returns ``['2',
 | 
					
						
							|  |  |  |    '3']`` as unparsed arguments, while the latter collects all the positionals
 | 
					
						
							|  |  |  |    into ``rest``.  ::
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.add_argument('--foo')
 | 
					
						
							|  |  |  |       >>> parser.add_argument('cmd')
 | 
					
						
							|  |  |  |       >>> parser.add_argument('rest', nargs='*', type=int)
 | 
					
						
							|  |  |  |       >>> parser.parse_known_args('doit 1 --foo bar 2 3'.split())
 | 
					
						
							|  |  |  |       (Namespace(cmd='doit', foo='bar', rest=[1]), ['2', '3'])
 | 
					
						
							|  |  |  |       >>> parser.parse_intermixed_args('doit 1 --foo bar 2 3'.split())
 | 
					
						
							|  |  |  |       Namespace(cmd='doit', foo='bar', rest=[1, 2, 3])
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    :meth:`~ArgumentParser.parse_known_intermixed_args` returns a two item tuple
 | 
					
						
							|  |  |  |    containing the populated namespace and the list of remaining argument strings.
 | 
					
						
							|  |  |  |    :meth:`~ArgumentParser.parse_intermixed_args` raises an error if there are any
 | 
					
						
							|  |  |  |    remaining unparsed argument strings.
 | 
					
						
							| 
									
										
										
										
											2010-03-02 22:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  |    .. versionadded:: 3.7
 | 
					
						
							| 
									
										
										
										
											2010-05-24 03:21:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-21 16:36:11 -08:00
										 |  |  | Registering custom types or actions
 | 
					
						
							|  |  |  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: ArgumentParser.register(registry_name, value, object)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Sometimes it's desirable to use a custom string in error messages to provide
 | 
					
						
							|  |  |  |    more user-friendly output. In these cases, :meth:`!register` can be used to
 | 
					
						
							|  |  |  |    register custom actions or types with a parser and allow you to reference the
 | 
					
						
							|  |  |  |    type by their registered name instead of their callable name.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :meth:`!register` method accepts three arguments - a *registry_name*,
 | 
					
						
							|  |  |  |    specifying the internal registry where the object will be stored (e.g.,
 | 
					
						
							|  |  |  |    ``action``, ``type``), *value*, which is the key under which the object will
 | 
					
						
							|  |  |  |    be registered, and object, the callable to be registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The following example shows how to register a custom type with a parser::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       >>> import argparse
 | 
					
						
							|  |  |  |       >>> parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |       >>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
 | 
					
						
							|  |  |  |       >>> parser.add_argument('--foo', type='hexadecimal integer')
 | 
					
						
							|  |  |  |       _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type='hexadecimal integer', choices=None, required=False, help=None, metavar=None, deprecated=False)
 | 
					
						
							|  |  |  |       >>> parser.parse_args(['--foo', '0xFA'])
 | 
					
						
							|  |  |  |       Namespace(foo=250)
 | 
					
						
							|  |  |  |       >>> parser.parse_args(['--foo', '1.2'])
 | 
					
						
							|  |  |  |       usage: PROG [-h] [--foo FOO]
 | 
					
						
							|  |  |  |       PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 15:36:32 -06:00
										 |  |  | Exceptions
 | 
					
						
							|  |  |  | ----------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. exception:: ArgumentError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    An error from creating or using an argument (optional or positional).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The string value of this exception is the message, augmented with
 | 
					
						
							|  |  |  |    information about the argument that caused it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. exception:: ArgumentTypeError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Raised when something goes wrong converting a command line string to a type.
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:07:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. rubric:: Guides and Tutorials
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. toctree::
 | 
					
						
							|  |  |  |    :maxdepth: 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    ../howto/argparse.rst
 | 
					
						
							|  |  |  |    ../howto/argparse-optparse.rst
 |