@@ -50,8 +50,8 @@ the extracted data in a :class:`argparse.Namespace` object::
5050 print(args.filename, args.count, args.verbose)
5151
5252.. note ::
53- If you're looking a guide about how to upgrade optparse code
54- to argparse, see :ref: `Upgrading Optparse Code <upgrading-optparse-code >`.
53+ If you're looking for a guide about how to upgrade :mod: ` optparse ` code
54+ to :mod: ` ! argparse` , see :ref: `Upgrading Optparse Code <upgrading-optparse-code >`.
5555
5656ArgumentParser objects
5757----------------------
@@ -100,7 +100,7 @@ ArgumentParser objects
100100 * allow_abbrev _ - Allows long options to be abbreviated if the
101101 abbreviation is unambiguous. (default: ``True ``)
102102
103- * exit_on_error _ - Determines whether or not ArgumentParser exits with
103+ * exit_on_error _ - Determines whether or not :class: ` ! ArgumentParser` exits with
104104 error info when an error occurs. (default: ``True ``)
105105
106106 .. versionchanged :: 3.5
@@ -372,7 +372,7 @@ Most command-line options will use ``-`` as the prefix, e.g. ``-f/--foo``.
372372Parsers that need to support different or additional prefix
373373characters, e.g. for options
374374like ``+f `` or ``/foo ``, may specify them using the ``prefix_chars= `` argument
375- to the ArgumentParser constructor::
375+ to the :class: ` ArgumentParser ` constructor::
376376
377377 >>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')
378378 >>> parser.add_argument('+f')
@@ -503,9 +503,9 @@ string was overridden.
503503add_help
504504^^^^^^^^
505505
506- By default, ArgumentParser objects add an option which simply displays
506+ By default, :class: ` ArgumentParser ` objects add an option which simply displays
507507the parser's help message. If ``-h `` or ``--help `` is supplied at the command
508- line, the ArgumentParser help will be printed.
508+ line, the :class: ` ! ArgumentParser` help will be printed.
509509
510510Occasionally, it may be useful to disable the addition of this help option.
511511This can be achieved by passing ``False `` as the ``add_help= `` argument to
@@ -559,15 +559,15 @@ If the user would like to catch errors manually, the feature can be enabled by s
559559The add_argument() method
560560-------------------------
561561
562- .. method :: ArgumentParser.add_argument(name or flags..., [action], [nargs], \
562+ .. method :: ArgumentParser.add_argument(name or flags..., *, [action], [nargs], \
563563 [const], [default], [type], [choices], [required], \
564564 [help], [metavar], [dest])
565565
566566 Define how a single command-line argument should be parsed. Each parameter
567567 has its own more detailed description below, but in short they are:
568568
569- * `name or flags `_ - Either a name or a list of option strings, e.g. ``foo ``
570- or ``-f, --foo ``.
569+ * `name or flags `_ - Either a name or a list of option strings, e.g. ``' foo' ``
570+ or ``'-f', ' --foo' ``.
571571
572572 * action _ - The basic type of action to be taken when this argument is
573573 encountered at the command line.
@@ -733,22 +733,24 @@ how the command-line arguments should be handled. The supplied actions are:
733733Only actions that consume command-line arguments (e.g. ``'store' ``,
734734``'append' `` or ``'extend' ``) can be used with positional arguments.
735735
736- You may also specify an arbitrary action by passing an Action subclass or
737- other object that implements the same interface. The ``BooleanOptionalAction ``
738- is available in ``argparse `` and adds support for boolean actions such as
739- ``--foo `` and ``--no-foo ``::
736+ .. class :: BooleanOptionalAction
740737
741- >>> import argparse
742- >>> parser = argparse.ArgumentParser()
743- >>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
744- >>> parser.parse_args(['--no-foo'])
745- Namespace(foo=False)
738+ You may also specify an arbitrary action by passing an :class: `Action ` subclass or
739+ other object that implements the same interface. The :class: `!BooleanOptionalAction `
740+ is available in :mod: `!argparse ` and adds support for boolean actions such as
741+ ``--foo `` and ``--no-foo ``::
746742
747- .. versionadded :: 3.9
743+ >>> import argparse
744+ >>> parser = argparse.ArgumentParser()
745+ >>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
746+ >>> parser.parse_args(['--no-foo'])
747+ Namespace(foo=False)
748+
749+ .. versionadded :: 3.9
748750
749751The recommended way to create a custom action is to extend :class: `Action `,
750- overriding the `` __call__ `` method and optionally the `` __init__ ` ` and
751- `` format_usage ` ` methods.
752+ overriding the :meth: ` ! __call__ ` method and optionally the :meth: ` ! __init__ ` and
753+ :meth: ` ! format_usage ` methods.
752754
753755An example of a custom action::
754756
@@ -778,7 +780,7 @@ For more details, see :class:`Action`.
778780nargs
779781^^^^^
780782
781- ArgumentParser objects usually associate a single command-line argument with a
783+ :class: ` ArgumentParser ` objects usually associate a single command-line argument with a
782784single action to be taken. The ``nargs `` keyword argument associates a
783785different number of command-line arguments with a single action.
784786See also :ref: `specifying-ambiguous-arguments `. The supported values are:
@@ -1067,7 +1069,7 @@ many choices), just specify an explicit metavar_.
10671069required
10681070^^^^^^^^
10691071
1070- In general, the :mod: `argparse ` module assumes that flags like ``-f `` and ``--bar ``
1072+ In general, the :mod: `! argparse ` module assumes that flags like ``-f `` and ``--bar ``
10711073indicate *optional * arguments, which can always be omitted at the command line.
10721074To make an option *required *, ``True `` can be specified for the ``required= ``
10731075keyword argument to :meth: `~ArgumentParser.add_argument `::
@@ -1120,7 +1122,7 @@ specifiers include the program name, ``%(prog)s`` and most keyword arguments to
11201122As the help string supports %-formatting, if you want a literal ``% `` to appear
11211123in the help string, you must escape it as ``%% ``.
11221124
1123- :mod: `argparse ` supports silencing the help entry for certain options, by
1125+ :mod: `! argparse ` supports silencing the help entry for certain options, by
11241126setting the ``help `` value to ``argparse.SUPPRESS ``::
11251127
11261128 >>> parser = argparse.ArgumentParser(prog='frobble')
@@ -1138,7 +1140,7 @@ metavar
11381140^^^^^^^
11391141
11401142When :class: `ArgumentParser ` generates help messages, it needs some way to refer
1141- to each expected argument. By default, ArgumentParser objects use the dest _
1143+ to each expected argument. By default, :class: ` ! ArgumentParser` objects use the dest _
11421144value as the "name" of each object. By default, for positional argument
11431145actions, the dest _ value is used directly, and for optional argument actions,
11441146the dest _ value is uppercased. So, a single positional argument with
@@ -1242,7 +1244,7 @@ behavior::
12421244Action classes
12431245^^^^^^^^^^^^^^
12441246
1245- Action classes implement the Action API, a callable which returns a callable
1247+ :class: ` ! Action` classes implement the Action API, a callable which returns a callable
12461248which processes arguments from the command-line. Any object which follows
12471249this API may be passed as the ``action `` parameter to
12481250:meth: `~ArgumentParser.add_argument `.
@@ -1251,40 +1253,45 @@ this API may be passed as the ``action`` parameter to
12511253 type=None, choices=None, required=False, help=None, \
12521254 metavar=None)
12531255
1254- Action objects are used by an ArgumentParser to represent the information
1256+ :class: ` ! Action` objects are used by an :class: ` ArgumentParser ` to represent the information
12551257 needed to parse a single argument from one or more strings from the
1256- command line. The Action class must accept the two positional arguments
1258+ command line. The :class: ` ! Action` class must accept the two positional arguments
12571259 plus any keyword arguments passed to :meth: `ArgumentParser.add_argument `
12581260 except for the ``action `` itself.
12591261
1260- Instances of Action (or return value of any callable to the ``action ``
1261- parameter) should have attributes "dest", "option_strings", "default", "type",
1262- "required", "help", etc. defined. The easiest way to ensure these attributes
1263- are defined is to call ``Action.__init__ ``.
1262+ Instances of :class: `!Action ` (or return value of any callable to the
1263+ ``action `` parameter) should have attributes :attr: `!dest `,
1264+ :attr: `!option_strings `, :attr: `!default `, :attr: `!type `, :attr: `!required `,
1265+ :attr: `!help `, etc. defined. The easiest way to ensure these attributes
1266+ are defined is to call :meth: `!Action.__init__ `.
1267+
1268+ .. method :: __call__(parser, namespace, values, option_string=None)
1269+
1270+ :class: `!Action ` instances should be callable, so subclasses must override the
1271+ :meth: `!__call__ ` method, which should accept four parameters:
12641272
1265- Action instances should be callable, so subclasses must override the
1266- ``__call__ `` method, which should accept four parameters:
1273+ * *parser * - The :class: `ArgumentParser ` object which contains this action.
12671274
1268- * *parser * - The ArgumentParser object which contains this action.
1275+ * *namespace * - The :class: `Namespace ` object that will be returned by
1276+ :meth: `~ArgumentParser.parse_args `. Most actions add an attribute to this
1277+ object using :func: `setattr `.
12691278
1270- * *namespace * - The :class: ` Namespace ` object that will be returned by
1271- :meth: ` ~ArgumentParser.parse_args `. Most actions add an attribute to this
1272- object using :func: ` setattr `.
1279+ * *values * - The associated command-line arguments, with any type conversions
1280+ applied. Type conversions are specified with the type _ keyword argument to
1281+ :meth: ` ~ArgumentParser.add_argument `.
12731282
1274- * *values * - The associated command-line arguments, with any type conversions
1275- applied. Type conversions are specified with the type _ keyword argument to
1276- :meth: ` ~ArgumentParser.add_argument ` .
1283+ * *option_string * - The option string that was used to invoke this action.
1284+ The `` option_string `` argument is optional, and will be absent if the action
1285+ is associated with a positional argument .
12771286
1278- * *option_string * - The option string that was used to invoke this action.
1279- The ``option_string `` argument is optional, and will be absent if the action
1280- is associated with a positional argument.
1287+ The :meth: `!__call__ ` method may perform arbitrary actions, but will typically set
1288+ attributes on the ``namespace `` based on ``dest `` and ``values ``.
12811289
1282- The ``__call__ `` method may perform arbitrary actions, but will typically set
1283- attributes on the ``namespace `` based on ``dest `` and ``values ``.
1290+ .. method :: format_usage()
12841291
1285- Action subclasses can define a `` format_usage ` ` method that takes no argument
1286- and return a string which will be used when printing the usage of the program.
1287- If such method is not provided, a sensible default will be used.
1292+ :class: ` ! Action` subclasses can define a :meth: ` ! format_usage ` method that takes no argument
1293+ and return a string which will be used when printing the usage of the program.
1294+ If such method is not provided, a sensible default will be used.
12881295
12891296
12901297The parse_args() method
@@ -1297,7 +1304,7 @@ The parse_args() method
12971304
12981305 Previous calls to :meth: `add_argument ` determine exactly what objects are
12991306 created and how they are assigned. See the documentation for
1300- :meth: `add_argument ` for details.
1307+ :meth: `! add_argument ` for details.
13011308
13021309 * args _ - List of strings to parse. The default is taken from
13031310 :data: `sys.argv `.
@@ -1453,7 +1460,7 @@ This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
14531460Beyond ``sys.argv ``
14541461^^^^^^^^^^^^^^^^^^^
14551462
1456- Sometimes it may be useful to have an ArgumentParser parse arguments other than those
1463+ Sometimes it may be useful to have an :class: ` ArgumentParser ` parse arguments other than those
14571464of :data: `sys.argv `. This can be accomplished by passing a list of strings to
14581465:meth: `~ArgumentParser.parse_args `. This is useful for testing at the
14591466interactive prompt::
@@ -1511,9 +1518,9 @@ Other utilities
15111518Sub-commands
15121519^^^^^^^^^^^^
15131520
1514- .. method :: ArgumentParser.add_subparsers([title], [description], [prog], \
1521+ .. method :: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
15151522 [parser_class], [action], \
1516- [option_strings], [ dest], [required], \
1523+ [dest], [required], \
15171524 [help], [metavar])
15181525
15191526 Many programs split up their functionality into a number of subcommands,
@@ -1522,11 +1529,11 @@ Sub-commands
15221529 this way can be a particularly good idea when a program performs several
15231530 different functions which require different kinds of command-line arguments.
15241531 :class: `ArgumentParser ` supports the creation of such subcommands with the
1525- :meth: `add_subparsers ` method. The :meth: `add_subparsers ` method is normally
1532+ :meth: `! add_subparsers ` method. The :meth: `! add_subparsers ` method is normally
15261533 called with no arguments and returns a special action object. This object
15271534 has a single method, :meth: `~_SubParsersAction.add_parser `, which takes a
1528- command name and any :class: `ArgumentParser ` constructor arguments, and
1529- returns an :class: `ArgumentParser ` object that can be modified as usual.
1535+ command name and any :class: `! ArgumentParser ` constructor arguments, and
1536+ returns an :class: `! ArgumentParser ` object that can be modified as usual.
15301537
15311538 Description of parameters:
15321539
@@ -1542,7 +1549,7 @@ Sub-commands
15421549 subparser argument
15431550
15441551 * *parser_class * - class which will be used to create sub-parser instances, by
1545- default the class of the current parser (e.g. ArgumentParser)
1552+ default the class of the current parser (e.g. :class: ` ArgumentParser ` )
15461553
15471554 * action _ - the basic type of action to be taken when this argument is
15481555 encountered at the command line
@@ -1708,7 +1715,7 @@ Sub-commands
17081715 Namespace(subparser_name='2', y='frobble')
17091716
17101717 .. versionchanged :: 3.7
1711- New *required * keyword argument .
1718+ New *required * keyword-only parameter .
17121719
17131720
17141721FileType objects
@@ -1751,7 +1758,7 @@ Argument groups
17511758 "positional arguments" and "options" when displaying help
17521759 messages. When there is a better conceptual grouping of arguments than this
17531760 default one, appropriate groups can be created using the
1754- :meth: `add_argument_group ` method::
1761+ :meth: `! add_argument_group ` method::
17551762
17561763 >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
17571764 >>> group = parser.add_argument_group('group')
@@ -1768,7 +1775,7 @@ Argument groups
17681775 has an :meth: `~ArgumentParser.add_argument ` method just like a regular
17691776 :class: `ArgumentParser `. When an argument is added to the group, the parser
17701777 treats it just like a normal argument, but displays the argument in a
1771- separate group for help messages. The :meth: `add_argument_group ` method
1778+ separate group for help messages. The :meth: `! add_argument_group ` method
17721779 accepts *title * and *description * arguments which can be used to
17731780 customize this display::
17741781
@@ -1810,7 +1817,7 @@ Mutual exclusion
18101817
18111818.. method :: ArgumentParser.add_mutually_exclusive_group(required=False)
18121819
1813- Create a mutually exclusive group. :mod: `argparse ` will make sure that only
1820+ Create a mutually exclusive group. :mod: `! argparse ` will make sure that only
18141821 one of the arguments in the mutually exclusive group was present on the
18151822 command line::
18161823
@@ -2023,7 +2030,7 @@ Intermixed parsing
20232030 and :meth: `~ArgumentParser.parse_known_intermixed_args ` methods
20242031 support this parsing style.
20252032
2026- These parsers do not support all the argparse features, and will raise
2033+ These parsers do not support all the :mod: ` ! argparse` features, and will raise
20272034 exceptions if unsupported features are used. In particular, subparsers,
20282035 and mutually exclusive groups that include both
20292036 optionals and positionals are not supported.
0 commit comments