Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3d4c21

Browse files
committed
Issue #13540: Merge changes from 3.3
2 parents 400daed + 89e186f commit b3d4c21

1 file changed

Lines changed: 53 additions & 19 deletions

File tree

Doc/library/argparse.rst

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

688688
* ``'store'`` - This just stores the argument's value. This is the default
689689
action. For example::
@@ -757,28 +757,18 @@ how the command-line arguments should be handled. The supported actions are:
757757
>>> parser.parse_args(['--version'])
758758
PROG 2.0
759759

760-
You can also specify an arbitrary action by passing an object that implements
761-
the Action API. The easiest way to do this is to extend
762-
:class:`argparse.Action`, supplying an appropriate ``__call__`` method. The
763-
``__call__`` method should accept four parameters:
764-
765-
* ``parser`` - The ArgumentParser object which contains this action.
766-
767-
* ``namespace`` - The :class:`Namespace` object that will be returned by
768-
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
769-
object.
770-
771-
* ``values`` - The associated command-line arguments, with any type conversions
772-
applied. (Type conversions are specified with the type_ keyword argument to
773-
:meth:`~ArgumentParser.add_argument`.)
774-
775-
* ``option_string`` - The option string that was used to invoke this action.
776-
The ``option_string`` argument is optional, and will be absent if the action
777-
is associated with a positional argument.
760+
You may also specify an arbitrary action by passing an Action subclass or
761+
other object that implements the same interface. The recommended way to do
762+
this is to extend :class:`Action`, overriding the ``__call__`` method
763+
and optionally the ``__init__`` method.
778764

779765
An example of a custom action::
780766

781767
>>> class FooAction(argparse.Action):
768+
... def __init__(self, option_strings, dest, nargs=None, **kwargs):
769+
... if nargs is not None:
770+
... raise ValueError("nargs not allowed")
771+
... super(FooAction, self).__init__(option_strings, dest, **kwargs)
782772
... def __call__(self, parser, namespace, values, option_string=None):
783773
... print('%r %r %r' % (namespace, values, option_string))
784774
... setattr(namespace, self.dest, values)
@@ -792,6 +782,7 @@ An example of a custom action::
792782
>>> args
793783
Namespace(bar='1', foo='2')
794784

785+
For more details, see :class:`Action`.
795786

796787
nargs
797788
^^^^^
@@ -1238,6 +1229,49 @@ behavior::
12381229
>>> parser.parse_args('--foo XXX'.split())
12391230
Namespace(bar='XXX')
12401231

1232+
Action classes
1233+
^^^^^^^^^^^^^^
1234+
1235+
Action classes implement the Action API, a callable which returns a callable
1236+
which processes arguments from the command-line. Any object which follows
1237+
this API may be passed as the ``action`` parameter to
1238+
:method:`add_argument`.
1239+
1240+
.. class:: Action(option_strings, dest, nargs=None, const=None, default=None,
1241+
type=None, choices=None, required=False, help=None,
1242+
metavar=None)
1243+
1244+
Action objects are used by an ArgumentParser to represent the information
1245+
needed to parse a single argument from one or more strings from the
1246+
command line. The Action class must accept the two positional arguments
1247+
plus any keyword arguments passed to :method:`ArgumentParser.add_argument`
1248+
except for the ``action`` itself.
1249+
1250+
Instances of Action (or return value of any callable to the ``action``
1251+
parameter) should have attributes "dest", "option_strings", "default", "type",
1252+
"required", "help", etc. defined. The easiest way to ensure these attributes
1253+
are defined is to call ``Action.__init__``.
1254+
1255+
Action instances should be callable, so subclasses must override the
1256+
``__call__`` method, which should accept four parameters:
1257+
1258+
* ``parser`` - The ArgumentParser object which contains this action.
1259+
1260+
* ``namespace`` - The :class:`Namespace` object that will be returned by
1261+
:meth:`~ArgumentParser.parse_args`. Most actions add an attribute to this
1262+
object using :func:`setattr`.
1263+
1264+
* ``values`` - The associated command-line arguments, with any type conversions
1265+
applied. Type conversions are specified with the type_ keyword argument to
1266+
:meth:`~ArgumentParser.add_argument`.
1267+
1268+
* ``option_string`` - The option string that was used to invoke this action.
1269+
The ``option_string`` argument is optional, and will be absent if the action
1270+
is associated with a positional argument.
1271+
1272+
The ``__call__`` method may perform arbitrary actions, but will typically set
1273+
attributes on the ``namespace`` based on ``dest`` and ``values``.
1274+
12411275

12421276
The parse_args() method
12431277
-----------------------

0 commit comments

Comments
 (0)