8000 [3.13] gh-84545: Clarify the 'extend' action documentation in argpars… · python/cpython@e4204e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4204e8

Browse files
[3.13] gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870) (GH-125964)
(cherry picked from commit da8673d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 95bcbcb commit e4204e8

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
@@ -692,6 +692,21 @@ how the command-line arguments should be handled. The supplied actions are:
692692
>>> parser.parse_args('--str --int'.split())
693693
Namespace(types=[<class 'str'>, <class 'int'>])
694694

695+
* ``'extend'`` - This stores a list and appends each item from the multi-value
696+
argument list to it.
697+
The ``'extend'`` action is typically used with the nargs_ keyword argument
698+
value ``'+'`` or ``'*'``.
699+
Note that when nargs_ is ``None`` (the default) or ``'?'``, each
700+
character of the argument string will be appended to the list.
701+
Example usage::
702+
703+
>>> parser = argparse.ArgumentParser()
704+
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
705+
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
706+
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
707+
708+
.. versionadded:: 3.8
709+
695710
* ``'count'`` - This counts the number of times a keyword argument occurs. For
696711
example, this is useful for increasing verbosity levels::
697712

@@ -717,17 +732,6 @@ how the command-line arguments should be handled. The supplied actions are:
717732
>>> parser.parse_args(['--version'])
718733
PROG 2.0
719734

720-
* ``'extend'`` - This stores a list, and extends each argument value to the
721-
list.
722-
Example usage::
723-
724-
>>> parser = argparse.ArgumentParser()
725-
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
726-
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
727-
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
728-
729-
.. versionadded:: 3.8
730-
731735
Only actions that consume command-line arguments (e.g. ``'store'``,
732736
``'append'`` or ``'extend'``) can be used with positional arguments.
733737

0 commit comments

Comments
 (0)
0