@@ -50,8 +50,8 @@ the extracted data in a :class:`argparse.Namespace` object::
50
50
print(args.filename, args.count, args.verbose)
51
51
52
52
.. 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 >`.
55
55
56
56
ArgumentParser objects
57
57
----------------------
@@ -100,7 +100,7 @@ ArgumentParser objects
100
100
* allow_abbrev _ - Allows long options to be abbreviated if the
101
101
abbreviation is unambiguous. (default: ``True ``)
102
102
103
- * exit_on_error _ - Determines whether or not ArgumentParser exits with
103
+ * exit_on_error _ - Determines whether or not :class: ` ! ArgumentParser` exits with
104
104
error info when an error occurs. (default: ``True ``)
105
105
106
106
.. versionchanged :: 3.5
@@ -372,7 +372,7 @@ Most command-line options will use ``-`` as the prefix, e.g. ``-f/--foo``.
372
372
Parsers that need to support different or additional prefix
373
373
characters, e.g. for options
374
374
like ``+f `` or ``/foo ``, may specify them using the ``prefix_chars= `` argument
375
- to the ArgumentParser constructor::
375
+ to the :class: ` ArgumentParser ` constructor::
376
376
377
377
>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')
378
378
>>> parser.add_argument('+f')
@@ -503,9 +503,9 @@ string was overridden.
503
503
add_help
504
504
^^^^^^^^
505
505
506
- By default, ArgumentParser objects add an option which simply displays
506
+ By default, :class: ` ArgumentParser ` objects add an option which simply displays
507
507
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.
509
509
510
510
Occasionally, it may be useful to disable the addition of this help option.
511
511
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
559
559
The add_argument() method
560
560
-------------------------
561
561
562
- .. method :: ArgumentParser.add_argument(name or flags..., [action], [nargs], \
562
+ .. method :: ArgumentParser.add_argument(name or flags..., *, [action], [nargs], \
563
563
[const], [default], [type], [choices], [required], \
564
564
[help], [metavar], [dest], [deprecated])
565
565
566
566
Define how a single command-line argument should be parsed. Each parameter
567
567
has its own more detailed description below, but in short they are:
568
568
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' ``.
571
571
572
572
* action _ - The basic type of action to be taken when this argument is
573
573
encountered at the command line.
@@ -735,22 +735,24 @@ how the command-line arguments should be handled. The supplied actions are:
735
735
Only actions that consume command-line arguments (e.g. ``'store' ``,
736
736
``'append' `` or ``'extend' ``) can be used with positional arguments.
737
737
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
742
739
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 ``::
748
744
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
750
752
751
753
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.
754
756
755
757
An example of a custom action::
756
758
@@ -780,7 +782,7 @@ For more details, see :class:`Action`.
780
782
nargs
781
783
^^^^^
782
784
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
784
786
single action to be taken. The ``nargs `` keyword argument associates a
785
787
different number of command-line arguments with a single action.
786
788
See also :ref: `specifying-ambiguous-arguments `. The supported values are:
@@ -1069,7 +1071,7 @@ many choices), just specify an explicit metavar_.
1069
1071
required
1070
1072
^^^^^^^^
1071
1073
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 ``
1073
1075
indicate *optional * arguments, which can always be omitted at the command line.
1074
1076
To make an option *required *, ``True `` can be specified for the ``required= ``
1075
1077
keyword argument to :meth: `~ArgumentParser.add_argument `::
@@ -1122,7 +1124,7 @@ specifiers include the program name, ``%(prog)s`` and most keyword arguments to
1122
1124
As the help string supports %-formatting, if you want a literal ``% `` to appear
1123
1125
in the help string, you must escape it as ``%% ``.
1124
1126
1125
- :mod: `argparse ` supports silencing the help entry for certain options, by
1127
+ :mod: `! argparse ` supports silencing the help entry for certain options, by
1126
1128
setting the ``help `` value to ``argparse.SUPPRESS ``::
1127
1129
1128
1130
>>> parser = argparse.ArgumentParser(prog='frobble')
@@ -1140,7 +1142,7 @@ metavar
1140
1142
^^^^^^^
1141
1143
1142
1144
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 _
1144
1146
value as the "name" of each object. By default, for positional argument
1145
1147
actions, the dest _ value is used directly, and for optional argument actions,
1146
1148
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::
1272
1274
Action classes
1273
1275
^^^^^^^^^^^^^^
1274
1276
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
1276
1278
which processes arguments from the command-line. Any object which follows
1277
1279
this API may be passed as the ``action `` parameter to
1278
1280
:meth: `~ArgumentParser.add_argument `.
@@ -1281,40 +1283,45 @@ this API may be passed as the ``action`` parameter to
1281
1283
type=None, choices=None, required=False, help=None, \
1282
1284
metavar=None)
1283
1285
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
1285
1287
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
1287
1289
plus any keyword arguments passed to :meth: `ArgumentParser.add_argument `
1288
1290
except for the ``action `` itself.
1289
1291
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:
1294
1302
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.
1297
1304
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 `.
1299
1308
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 `.
1303
1312
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 .
1307
1316
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 ``.
1311
1319
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()
1314
1321
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.
1318
1325
1319
1326
1320
1327
The parse_args() method
@@ -1327,7 +1334,7 @@ The parse_args() method
1327
1334
1328
1335
Previous calls to :meth: `add_argument ` determine exactly what objects are
1329
1336
created and how they are assigned. See the documentation for
1330
- :meth: `add_argument ` for details.
1337
+ :meth: `! add_argument ` for details.
1331
1338
1332
1339
* args _ - List of strings to parse. The default is taken from
1333
1340
:data: `sys.argv `.
@@ -1483,7 +1490,7 @@ This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
1483
1490
Beyond ``sys.argv ``
1484
1491
^^^^^^^^^^^^^^^^^^^
1485
1492
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
1487
1494
of :data: `sys.argv `. This can be accomplished by passing a list of strings to
1488
1495
:meth: `~ArgumentParser.parse_args `. This is useful for testing at the
1489
1496
interactive prompt::
@@ -1541,9 +1548,9 @@ Other utilities
1541
1548
Sub-commands
1542
1549
^^^^^^^^^^^^
1543
1550
1544
- .. method :: ArgumentParser.add_subparsers([title], [description], [prog], \
1551
+ .. method :: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
1545
1552
[parser_class], [action], \
1546
- [option_strings], [ dest], [required], \
1553
+ [dest], [required], \
1547
1554
[help], [metavar])
1548
1555
1549
1556
Many programs split up their functionality into a number of subcommands,
@@ -1552,11 +1559,11 @@ Sub-commands
1552
1559
this way can be a particularly good idea when a program performs several
1553
1560
different functions which require different kinds of command-line arguments.
1554
1561
: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
1556
1563
called with no arguments and returns a special action object. This object
1557
1564
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.
1560
1567
1561
1568
Description of parameters:
1562
1569
@@ -1572,7 +1579,7 @@ Sub-commands
1572
1579
subparser argument
1573
1580
1574
1581
* *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 ` )
1576
1583
1577
1584
* action _ - the basic type of action to be taken when this argument is
1578
1585
encountered at the command line
@@ -1753,7 +1760,7 @@ Sub-commands
1753
1760
Namespace(subparser_name='2', y='frobble')
1754
1761
1755
1762
.. versionchanged :: 3.7
1756
- New *required * keyword argument .
1763
+ New *required * keyword-only parameter .
1757
1764
1758
1765
1759
1766
FileType objects
@@ -1796,7 +1803,7 @@ Argument groups
1796
1803
"positional arguments" and "options" when displaying help
1797
1804
messages. When there is a better conceptual grouping of arguments than this
1798
1805
default one, appropriate groups can be created using the
1799
- :meth: `add_argument_group ` method::
1806
+ :meth: `! add_argument_group ` method::
1800
1807
1801
1808
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
1802
1809
>>> group = parser.add_argument_group('group')
@@ -1813,7 +1820,7 @@ Argument groups
1813
1820
has an :meth: `~ArgumentParser.add_argument ` method just like a regular
1814
1821
:class: `ArgumentParser `. When an argument is added to the group, the parser
1815
1822
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
1817
1824
accepts *title * and *description * arguments which can be used to
1818
1825
customize this display::
1819
1826
@@ -1855,7 +1862,7 @@ Mutual exclusion
1855
1862
1856
1863
.. method :: ArgumentParser.add_mutually_exclusive_group(required=False)
1857
1864
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
1859
1866
one of the arguments in the mutually exclusive group was present on the
1860
1867
command line::
1861
1868
@@ -2068,7 +2075,7 @@ Intermixed parsing
2068
2075
and :meth: `~ArgumentParser.parse_known_intermixed_args ` methods
2069
2076
support this parsing style.
2070
2077
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
2072
2079
exceptions if unsupported features are used. In particular, subparsers,
2073
2080
and mutually exclusive groups that include both
2074
2081
optionals and positionals are not supported.
0 commit comments