Skip to content

Commit 67b2701

Browse files
[3.12] gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870) (GH-125965)
(cherry picked from commit da8673d) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6a8e8f4 commit 67b2701

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Doc/library/argparse.rst

+15-11
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,21 @@ how the command-line arguments should be handled. The supplied actions are:
690690
>>> parser.parse_args('--str --int'.split())
691691
Namespace(types=[<class 'str'>, <class 'int'>])
692692

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

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

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

0 commit comments

Comments
 (0)