@@ -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])
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.
@@ -733,22 +733,24 @@ how the command-line arguments should be handled. The supplied actions are:
733
733
Only actions that consume command-line arguments (e.g. ``'store' ``,
734
734
``'append' `` or ``'extend' ``) can be used with positional arguments.
735
735
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
740
737
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 ``::
746
742
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
748
750
749
751
The 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.
752
754
753
755
An example of a custom action::
754
756
@@ -778,7 +780,7 @@ For more details, see :class:`Action`.
778
780
nargs
779
781
^^^^^
780
782
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
782
784
single action to be taken. The ``nargs `` keyword argument associates a
783
785
different number of command-line arguments with a single action.
784
786
See also :ref: `specifying-ambiguous-arguments `. The supported values are:
@@ -1067,7 +1069,7 @@ many choices), just specify an explicit metavar_.
1067
1069
required
1068
1070
^^^^^^^^
1069
1071
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 ``
1071
1073
indicate *optional * arguments, which can always be omitted at the command line.
1072
1074
To make an option *required *, ``True `` can be specified for the ``required= ``
1073
1075
keyword argument to :meth: `~ArgumentParser.add_argument `::
@@ -1120,7 +1122,7 @@ specifiers include the program name, ``%(prog)s`` and most keyword arguments to
1120
1122
As the help string supports %-formatting, if you want a literal ``% `` to appear
1121
1123
in the help string, you must escape it as ``%% ``.
1122
1124
1123
- :mod: `argparse ` supports silencing the help entry for certain options, by
1125
+ :mod: `! argparse ` supports silencing the help entry for certain options, by
1124
1126
setting the ``help `` value to ``argparse.SUPPRESS ``::
1125
1127
1126
1128
>>> parser = argparse.ArgumentParser(prog='frobble')
@@ -1138,7 +1140,7 @@ metavar
1138
1140
^^^^^^^
1139
1141
1140
1142
When :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 _
1142
1144
value as the "name" of each object. By default, for positional argument
1143
1145
actions, the dest _ value is used directly, and for optional argument actions,
1144
1146
the dest _ value is uppercased. So, a single positional argument with
@@ -1242,7 +1244,7 @@ behavior::
1242
1244
Action classes
1243
1245
^^^^^^^^^^^^^^
1244
1246
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
1246
1248
which processes arguments from the command-line. Any object which follows
1247
1249
this API may be passed as the ``action `` parameter to
1248
1250
:meth: `~ArgumentParser.add_argument `.
@@ -1251,40 +1253,45 @@ this API may be passed as the ``action`` parameter to
1251
1253
type=None, choices=None, required=False, help=None, \
1252
1254
metavar=None)
1253
1255
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
1255
1257
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
1257
1259
plus any keyword arguments passed to :meth: `ArgumentParser.add_argument `
1258
1260
except for the ``action `` itself.
1259
1261
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:
1264
1272
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.
1267
1274
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 `.
1269
1278
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 `.
1273
1282
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 .
1277
1286
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 ``.
1281
1289
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()
1284
1291
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.
1288
1295
1289
1296
1290
1297
The parse_args() method
@@ -1297,7 +1304,7 @@ The parse_args() method
1297
1304
1298
1305
Previous calls to :meth: `add_argument ` determine exactly what objects are
1299
1306
created and how they are assigned. See the documentation for
1300
- :meth: `add_argument ` for details.
1307
+ :meth: `! add_argument ` for details.
1301
1308
1302
1309
* args _ - List of strings to parse. The default is taken from
1303
1310
:data: `sys.argv `.
@@ -1453,7 +1460,7 @@ This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
1453
1460
Beyond ``sys.argv ``
1454
1461
^^^^^^^^^^^^^^^^^^^
1455
1462
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
1457
1464
of :data: `sys.argv `. This can be accomplished by passing a list of strings to
1458
1465
:meth: `~ArgumentParser.parse_args `. This is useful for testing at the
1459
1466
interactive prompt::
@@ -1511,9 +1518,9 @@ Other utilities
1511
1518
Sub-commands
1512
1519
^^^^^^^^^^^^
1513
1520
1514
- .. method :: ArgumentParser.add_subparsers([title], [description], [prog], \
1521
+ .. method :: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
1515
1522
[parser_class], [action], \
1516
- [option_strings], [ dest], [required], \
1523
+ [dest], [required], \
1517
1524
[help], [metavar])
1518
1525
1519
1526
Many programs split up their functionality into a number of subcommands,
@@ -1522,11 +1529,11 @@ Sub-commands
1522
1529
this way can be a particularly good idea when a program performs several
1523
1530
different functions which require different kinds of command-line arguments.
1524
1531
: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
1526
1533
called with no arguments and returns a special action object. This object
1527
1534
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.
1530
1537
1531
1538
Description of parameters:
1532
1539
@@ -1542,7 +1549,7 @@ Sub-commands
1542
1549
subparser argument
1543
1550
1544
1551
* *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 ` )
1546
1553
1547
1554
* action _ - the basic type of action to be taken when this argument is
1548
1555
encountered at the command line
@@ -1708,7 +1715,7 @@ Sub-commands
1708
1715
Namespace(subparser_name='2', y='frobble')
1709
1716
1710
1717
.. versionchanged :: 3.7
1711
- New *required * keyword argument .
1718
+ New *required * keyword-only parameter .
1712
1719
1713
1720
1714
1721
FileType objects
@@ -1751,7 +1758,7 @@ Argument groups
1751
1758
"positional arguments" and "options" when displaying help
1752
1759
messages. When there is a better conceptual grouping of arguments than this
1753
1760
default one, appropriate groups can be created using the
1754
- :meth: `add_argument_group ` method::
1761
+ :meth: `! add_argument_group ` method::
1755
1762
1756
1763
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
1757
1764
>>> group = parser.add_argument_group('group')
@@ -1768,7 +1775,7 @@ Argument groups
1768
1775
has an :meth: `~ArgumentParser.add_argument ` method just like a regular
1769
1776
:class: `ArgumentParser `. When an argument is added to the group, the parser
1770
1777
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
1772
1779
accepts *title * and *description * arguments which can be used to
1773
1780
customize this display::
1774
1781
@@ -1810,7 +1817,7 @@ Mutual exclusion
1810
1817
1811
1818
.. method :: ArgumentParser.add_mutually_exclusive_group(required=False)
1812
1819
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
1814
1821
one of the arguments in the mutually exclusive group was present on the
1815
1822
command line::
1816
1823
@@ -2023,7 +2030,7 @@ Intermixed parsing
2023
2030
and :meth: `~ArgumentParser.parse_known_intermixed_args ` methods
2024
2031
support this parsing style.
2025
2032
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
2027
2034
exceptions if unsupported features are used. In particular, subparsers,
2028
2035
and mutually exclusive groups that include both
2029
2036
optionals and positionals are not supported.
0 commit comments