8000 gh-84545: Clarify the 'extend' action documentation in argparse (GH-1… · python/cpython@da8673d · GitHub
[go: up one dir, main page]

Skip to content

Commit da8673d

Browse files
gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870)
1 parent 75401fe commit da8673d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Doc/library/argparse.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,21 @@ how the command-line arguments should be handled. The supplied actions are:
741741
>>> parser.parse_args('--str --int'.split())
742742
Namespace(types=[<class 'str'>, <class 'int'>])
743743

744+
* ``'extend'`` - This stores a list and appends each item from the multi-value
745+
argument list to it.
746+
The ``'extend'`` action is typically used with the nargs_ keyword argument
747+
value ``'+'`` or ``'*'``.
748+
Note that when nargs_ is ``None`` (the default) or ``'?'``, each
749+
character of the argument string will be appended to the list.
750+
Example usage::
751+
752+
>>> parser = argparse.ArgumentParser()
753+
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
754+
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
755+
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
756+
757+
.. versionadded:: 3.8
758+
744759
* ``'count'`` - This counts the number of times a keyword argument occurs. For
745760
example, this is useful for increasing verbosity levels::
746761

@@ -766,17 +781,6 @@ how the command-line arguments should be handled. The supplied actions are:
766781
>>> parser.parse_args(['--version'])
767782
PROG 2.0
768783

769-
* ``'extend'`` - This stores a list, and extends each argument value to the
770-
list.
771-
Example usage::
772-
773-
>>> parser = argparse.ArgumentParser()
774-
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
775-
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
776-
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
777-
778-
.. versionadded:: 3.8
779-
780784
Only actions that consume command-line arguments (e.g. ``'store'``,
781785
``'append'`` or ``'extend'``) can be used with positional arguments.
782786

0 commit comments

Comments
 (0)
0