8000 Issue #13540: Expanded argparse documents to clarify the action API · python/cpython@f28cf7a · GitHub
[go: up one dir, main page]

Skip to content

Commit f28cf7a

Browse files
committed
Issue #13540: Expanded argparse documents to clarify the action API
1 parent 915a30f commit f28cf7a

File tree

1 file changed

+76
-19
lines changed

1 file changed

+76
-19
lines changed

Doc/library/argparse.rst

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ action
663663
actions can do just about anything with the command-line arguments associated with
664664
them, though most actions simply add an attribute to the object returned by
665665
:meth:`~ArgumentParser.parse_args`. The ``action`` keyword argument specifies
666-
how the command-line arguments should be handled. The supported actions are:
666+
how the command-line arguments should be handled. The supplied actions are:
667667

668668
* ``'store'`` - This just stores the argument's value. This is the default
669669
action. For example::
@@ -737,24 +737,9 @@ how the command-line arguments should be handled. The supported actions are:
737737
>>> parser.parse_args(['--version'])
738738
PROG 2.0
739739

740-
You can also specify an arbitrary action by passing an object that implements
741-
the Action API. The easiest way to do this is to extend
742-
:class:`argparse.Action`, supplying an appropriate ``__call__`` method. The
743-
``__call__`` method should accept four parameters:
744-
745-
* ``parser`` - The ArgumentParser object which contains this action.
746-
747-
* ``namespace`` - The :class:`Namespace` object that will be returned by
748-
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
749-
object.
750-
751-
* ``values`` - The associated command-line arguments, with any type conversions
752-
applied. (Type conversions are specified with the type_ keyword argument to
753-
:meth:`~ArgumentParser.add_argument`.)
754-
755-
* ``option_string`` - The option string that was used to invoke this action.
756-
The ``option_string`` argument is optional, and will be absent if the action
757-
is associated with a positional argument.
740+
You may also specify an arbitrary action by passing an Action class or other
741+
class that implements the same interface. The recommended way to do this is
742+
to extend :class:`argparse.Action`, overriding the ``__call__`` method.
758743

759744
An example of a custom action::
760745

@@ -772,6 +757,9 @@ An example of a custom action::
772757
>>> args
773758
Namespace(bar='1', foo='2')
774759

760+
Many actions also override the ``__init__`` method, validating the parameters
761+
to the argument definition and raising a ValueError or other Exception on
762+
failure.
775763

776764
nargs
777765
^^^^^
@@ -1218,6 +1206,75 @@ behavior::
12181206
>>> parser.parse_args('--foo XXX'.split())
12191207
Namespace(bar='XXX')
12201208

1209+
Action classes
1210+
^^^^^^^^^^^^^^
1211+
1212+
.. class:: Action(option_strings, dest, nargs=None, const=None, default=None,
1213+
type=None, choices=None, required=False, help=None,
1214+
metavar=None)
1215+
1216+
Action objects are used by an ArgumentParser to represent the information
1217+
needed to parse a single argument from one or more strings from the
1218+
command line. The keyword arguments to the Action constructor are made
1219+
available as attributes of Action instances.
1220+
1221+
* ``option_strings`` - A list of command-line option strings which
1222+
should be associated with this action.
1223+
1224+
* ``dest`` - The name of the attribute to hold the created object(s)
1225+
1226+
* ``nargs`` - The number of command-line arguments that should be
1227+
consumed. By default, one argument will be consumed and a single
1228+
value will be produced. Other values include:
1229+
- N (an integer) consumes N arguments (and produces a list)
1230+
- '?' consumes zero or one arguments
1231+
- '*' consumes zero or more arguments (and produces a list)
1232+
- '+' consumes one or more arguments (and produces a list)
1233+
Note that the difference between the default and nargs=1 is that
1234+
with the default, a single value will be produced, while with
1235+
nargs=1, a list containing a single value will be produced.
1236+
1237+
* ``const`` - The value to be produced if the option is specified and the
1238+
option uses an action that takes no values.
1239+
1240+
* ``default`` - The value to be produced if the option is not specified.
1241+
1242+
* ``type`` - The type which the command-line arguments should be converted
1243+
to, should be one of 'string', 'int', 'float', 'complex' or a
1244+
callable object that accepts a single string argument. If None,
1245+
'string' is assumed.
1246+
1247+
* ``choices`` - A container of values that should be allowed. If not None,
1248+
after a command-line argument has been converted to the appropriate
1249+
type, an exception will be raised if it is not a member of this
1250+
collection.
1251+
1252+
* ``required`` - True if the action must always be specified at the
1253+
command line. This is only meaningful for optional command-line
1254+
arguments.
1255+
1256+
* ``help`` - The help string describing the argument.
1257+
1258+
* ``metavar`` - The name to be used for the option's argument with the
1259+
help string. If None, the 'dest' value will be used as the name.
1260+
1261+
Action classes must also override the ``__call__`` method, which should accept
1262+
four parameters:
1263+
1264+
* ``parser`` - The ArgumentParser object which contains this action.
1265+
1266+
* ``namespace`` - The :class:`Namespace` object that will be returned by
1267+
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
1268+
object using :func:`setattr`.
1269+
1270+
* ``values`` - The associated command-line arguments, with any type conversions
1271+
applied. Type conversions are specified with the type_ keyword argument to
1272+
:meth:`~ArgumentParser.add_argument`.
1273+
1274+
* ``option_string`` - The option string that was used to invoke this action.
1275+
The ``option_string`` argument is optional, and will be absent if the action
1276+
is associated with a positional argument.
1277+
12211278

12221279
The parse_args() method
12231280
-----------------------

0 commit comments

Comments
 (0)
0