Skip to content

Commit 6b0ef1f

Browse files
[3.13] gh-126071: Improve formatting of the argparse documentation (GH-126073) (GH-126173)
* Use appropriate roles for ArgumentParser, Action, etc. * Remove superfluous repeated links. * Explicitly document signatures and add index entries for some methods and classes. * Make it more clear that some parameters are keyword-only. * Fix some minor errors. (cherry picked from commit 2ab377a)
1 parent de9ff23 commit 6b0ef1f

File tree

1 file changed

+69
-62
lines changed

1 file changed

+69
-62
lines changed

Doc/library/argparse.rst

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5656
ArgumentParser 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``.
372372
Parsers that need to support different or additional prefix
373373
characters, e.g. for options
374374
like ``+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.
503503
add_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
507507
the 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

510510
Occasionally, it may be useful to disable the addition of this help option.
511511
This 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
559559
The 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], [deprecated])
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.
@@ -735,22 +735,24 @@ how the command-line arguments should be handled. The supplied actions are:
735735
Only actions that consume command-line arguments (e.g. ``'store'``,
736736
``'append'`` or ``'extend'``) can be used with positional arguments.
737737

738-
You may also specify an arbitrary action by passing an Action subclass or
739-
other object that implements the same interface. The ``BooleanOptionalAction``
740-
is available in ``argparse`` and adds support for boolean actions such as
741-
``--foo`` and ``--no-foo``::
738+
.. class:: BooleanOptionalAction
742739

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)
740+
You may also specify an arbitrary action by passing an :class:`Action` subclass or
741+
other object that implements the same interface. The :class:`!BooleanOptionalAction`
742+
is available in :mod:`!argparse` and adds support for boolean actions such as
743+
``--foo`` and ``--no-foo``::
748744

749-
.. versionadded:: 3.9
745+
>>> import argparse
746+
>>> parser = argparse.ArgumentParser()
747+
>>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
748+
>>> parser.parse_args(['--no-foo'])
749+
Namespace(foo=False)
750+
751+
.. versionadded:: 3.9
750752

751753
The recommended way to create a custom action is to extend :class:`Action`,
752-
overriding the ``__call__`` method and optionally the ``__init__`` and
753-
``format_usage`` methods.
754+
overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` and
755+
:meth:`!format_usage` methods.
754756

755757
An example of a custom action::
756758

@@ -780,7 +782,7 @@ For more details, see :class:`Action`.
780782
nargs
781783
^^^^^
782784

783-
ArgumentParser objects usually associate a single command-line argument with a
785+
:class:`ArgumentParser` objects usually associate a single command-line argument with a
784786
single action to be taken. The ``nargs`` keyword argument associates a
785787
different number of command-line arguments with a single action.
786788
See also :ref:`specifying-ambiguous-arguments`. The supported values are:
@@ -1069,7 +1071,7 @@ many choices), just specify an explicit metavar_.
10691071
required
10701072
^^^^^^^^
10711073

1072-
In general, the :mod:`argparse` module assumes that flags like ``-f`` and ``--bar``
1074+
In general, the :mod:`!argparse` module assumes that flags like ``-f`` and ``--bar``
10731075
indicate *optional* arguments, which can always be omitted at the command line.
10741076
To make an option *required*, ``True`` can be specified for the ``required=``
10751077
keyword argument to :meth:`~ArgumentParser.add_argument`::
@@ -1122,7 +1124,7 @@ specifiers include the program name, ``%(prog)s`` and most keyword arguments to
11221124
As the help string supports %-formatting, if you want a literal ``%`` to appear
11231125
in the help string, you must escape it as ``%%``.
11241126

1125-
:mod:`argparse` supports silencing the help entry for certain options, by
1127+
:mod:`!argparse` supports silencing the help entry for certain options, by
11261128
setting the ``help`` value to ``argparse.SUPPRESS``::
11271129

11281130
>>> parser = argparse.ArgumentParser(prog='frobble')
@@ -1140,7 +1142,7 @@ metavar
11401142
^^^^^^^
11411143

11421144
When :class:`ArgumentParser` generates help messages, it needs some way to refer
1143-
to each expected argument. By default, ArgumentParser objects use the dest_
1145+
to each expected argument. By default, :class:`!ArgumentParser` objects use the dest_
11441146
value as the "name" of each object. By default, for positional argument
11451147
actions, the dest_ value is used directly, and for optional argument actions,
11461148
the dest_ value is uppercased. So, a single positional argument with
@@ -1272,7 +1274,7 @@ printed to :data:`sys.stderr` when the argument is used::
12721274
Action classes
12731275
^^^^^^^^^^^^^^
12741276

1275-
Action classes implement the Action API, a callable which returns a callable
1277+
:class:`!Action` classes implement the Action API, a callable which returns a callable
12761278
which processes arguments from the command-line. Any object which follows
12771279
this API may be passed as the ``action`` parameter to
12781280
:meth:`~ArgumentParser.add_argument`.
@@ -1281,40 +1283,45 @@ this API may be passed as the ``action`` parameter to
12811283
type=None, choices=None, required=False, help=None, \
12821284
metavar=None)
12831285

1284-
Action objects are used by an ArgumentParser to represent the information
1286+
:class:`!Action` objects are used by an :class:`ArgumentParser` to represent the information
12851287
needed to parse a single argument from one or more strings from the
1286-
command line. The Action class must accept the two positional arguments
1288+
command line. The :class:`!Action` class must accept the two positional arguments
12871289
plus any keyword arguments passed to :meth:`ArgumentParser.add_argument`
12881290
except for the ``action`` itself.
12891291

1290-
Instances of Action (or return value of any callable to the ``action``
1291-
parameter) should have attributes "dest", "option_strings", "default", "type",
1292-
"required", "help", etc. defined. The easiest way to ensure these attributes
1293-
are defined is to call ``Action.__init__``.
1292+
Instances of :class:`!Action` (or return value of any callable to the
1293+
``action`` parameter) should have attributes :attr:`!dest`,
1294+
:attr:`!option_strings`, :attr:`!default`, :attr:`!type`, :attr:`!required`,
1295+
:attr:`!help`, etc. defined. The easiest way to ensure these attributes
1296+
are defined is to call :meth:`!Action.__init__`.
1297+
1298+
.. method:: __call__(parser, namespace, values, option_string=None)
1299+
1300+
:class:`!Action` instances should be callable, so subclasses must override the
1301+
:meth:`!__call__` method, which should accept four parameters:
12941302

1295-
Action instances should be callable, so subclasses must override the
1296-
``__call__`` method, which should accept four parameters:
1303+
* *parser* - The :class:`ArgumentParser` object which contains this action.
12971304

1298-
* *parser* - The ArgumentParser object which contains this action.
1305+
* *namespace* - The :class:`Namespace` object that will be returned by
1306+
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
1307+
object using :func:`setattr`.
12991308

1300-
* *namespace* - The :class:`Namespace` object that will be returned by
1301-
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
1302-
object using :func:`setattr`.
1309+
* *values* - The associated command-line arguments, with any type conversions
1310+
applied. Type conversions are specified with the type_ keyword argument to
1311+
:meth:`~ArgumentParser.add_argument`.
13031312

1304-
* *values* - The associated command-line arguments, with any type conversions
1305-
applied. Type conversions are specified with the type_ keyword argument to
1306-
:meth:`~ArgumentParser.add_argument`.
1313+
* *option_string* - The option string that was used to invoke this action.
1314+
The ``option_string`` argument is optional, and will be absent if the action
1315+
is associated with a positional argument.
13071316

1308-
* *option_string* - The option string that was used to invoke this action.
1309-
The ``option_string`` argument is optional, and will be absent if the action
1310-
is associated with a positional argument.
1317+
The :meth:`!__call__` method may perform arbitrary actions, but will typically set
1318+
attributes on the ``namespace`` based on ``dest`` and ``values``.
13111319

1312-
The ``__call__`` method may perform arbitrary actions, but will typically set
1313-
attributes on the ``namespace`` based on ``dest`` and ``values``.
1320+
.. method:: format_usage()
13141321

1315-
Action subclasses can define a ``format_usage`` method that takes no argument
1316-
and return a string which will be used when printing the usage of the program.
1317-
If such method is not provided, a sensible default will be used.
1322+
:class:`!Action` subclasses can define a :meth:`!format_usage` method that takes no argument
1323+
and return a string which will be used when printing the usage of the program.
1324+
If such method is not provided, a sensible default will be used.
13181325

13191326

13201327
The parse_args() method
@@ -1327,7 +1334,7 @@ The parse_args() method
13271334

13281335
Previous calls to :meth:`add_argument` determine exactly what objects are
13291336
created and how they are assigned. See the documentation for
1330-
:meth:`add_argument` for details.
1337+
:meth:`!add_argument` for details.
13311338

13321339
* args_ - List of strings to parse. The default is taken from
13331340
:data:`sys.argv`.
@@ -1483,7 +1490,7 @@ This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
14831490
Beyond ``sys.argv``
14841491
^^^^^^^^^^^^^^^^^^^
14851492

1486-
Sometimes it may be useful to have an ArgumentParser parse arguments other than those
1493+
Sometimes it may be useful to have an :class:`ArgumentParser` parse arguments other than those
14871494
of :data:`sys.argv`. This can be accomplished by passing a list of strings to
14881495
:meth:`~ArgumentParser.parse_args`. This is useful for testing at the
14891496
interactive prompt::
@@ -1541,9 +1548,9 @@ Other utilities
15411548
Sub-commands
15421549
^^^^^^^^^^^^
15431550

1544-
.. method:: ArgumentParser.add_subparsers([title], [description], [prog], \
1551+
.. method:: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
15451552
[parser_class], [action], \
1546-
[option_strings], [dest], [required], \
1553+
[dest], [required], \
15471554
[help], [metavar])
15481555

15491556
Many programs split up their functionality into a number of subcommands,
@@ -1552,11 +1559,11 @@ Sub-commands
15521559
this way can be a particularly good idea when a program performs several
15531560
different functions which require different kinds of command-line arguments.
15541561
:class:`ArgumentParser` supports the creation of such subcommands with the
1555-
:meth:`add_subparsers` method. The :meth:`add_subparsers` method is normally
1562+
:meth:`!add_subparsers` method. The :meth:`!add_subparsers` method is normally
15561563
called with no arguments and returns a special action object. This object
15571564
has a single method, :meth:`~_SubParsersAction.add_parser`, which takes a
1558-
command name and any :class:`ArgumentParser` constructor arguments, and
1559-
returns an :class:`ArgumentParser` object that can be modified as usual.
1565+
command name and any :class:`!ArgumentParser` constructor arguments, and
1566+
returns an :class:`!ArgumentParser` object that can be modified as usual.
15601567

15611568
Description of parameters:
15621569

@@ -1572,7 +1579,7 @@ Sub-commands
15721579
subparser argument
15731580

15741581
* *parser_class* - class which will be used to create sub-parser instances, by
1575-
default the class of the current parser (e.g. ArgumentParser)
1582+
default the class of the current parser (e.g. :class:`ArgumentParser`)
15761583

15771584
* action_ - the basic type of action to be taken when this argument is
15781585
encountered at the command line
@@ -1753,7 +1760,7 @@ Sub-commands
17531760
Namespace(subparser_name='2', y='frobble')
17541761

17551762
.. versionchanged:: 3.7
1756-
New *required* keyword argument.
1763+
New *required* keyword-only parameter.
17571764

17581765

17591766
FileType objects
@@ -1796,7 +1803,7 @@ Argument groups
17961803
"positional arguments" and "options" when displaying help
17971804
messages. When there is a better conceptual grouping of arguments than this
17981805
default one, appropriate groups can be created using the
1799-
:meth:`add_argument_group` method::
1806+
:meth:`!add_argument_group` method::
18001807

18011808
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
18021809
>>> group = parser.add_argument_group('group')
@@ -1813,7 +1820,7 @@ Argument groups
18131820
has an :meth:`~ArgumentParser.add_argument` method just like a regular
18141821
:class:`ArgumentParser`. When an argument is added to the group, the parser
18151822
treats it just like a normal argument, but displays the argument in a
1816-
separate group for help messages. The :meth:`add_argument_group` method
1823+
separate group for help messages. The :meth:`!add_argument_group` method
18171824
accepts *title* and *description* arguments which can be used to
18181825
customize this display::
18191826

@@ -1855,7 +1862,7 @@ Mutual exclusion
18551862

18561863
.. method:: ArgumentParser.add_mutually_exclusive_group(required=False)
18571864

1858-
Create a mutually exclusive group. :mod:`argparse` will make sure that only
1865+
Create a mutually exclusive group. :mod:`!argparse` will make sure that only
18591866
one of the arguments in the mutually exclusive group was present on the
18601867
command line::
18611868

@@ -2068,7 +2075,7 @@ Intermixed parsing
20682075
and :meth:`~ArgumentParser.parse_known_intermixed_args` methods
20692076
support this parsing style.
20702077

2071-
These parsers do not support all the argparse features, and will raise
2078+
These parsers do not support all the :mod:`!argparse` features, and will raise
20722079
exceptions if unsupported features are used. In particular, subparsers,
20732080
and mutually exclusive groups that include both
20742081
optionals and positionals are not supported.

0 commit comments

Comments
 (0)