mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
[3.13] gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870) (GH-125964)
(cherry picked from commit da8673da36
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
95bcbcb7c8
commit
e4204e879e
1 changed files with 15 additions and 11 deletions
|
@ -692,6 +692,21 @@ how the command-line arguments should be handled. The supplied actions are:
|
|||
>>> parser.parse_args('--str --int'.split())
|
||||
Namespace(types=[<class 'str'>, <class 'int'>])
|
||||
|
||||
* ``'extend'`` - This stores a list and appends each item from the multi-value
|
||||
argument list to it.
|
||||
The ``'extend'`` action is typically used with the nargs_ keyword argument
|
||||
value ``'+'`` or ``'*'``.
|
||||
Note that when nargs_ is ``None`` (the default) or ``'?'``, each
|
||||
character of the argument string will be appended to the list.
|
||||
Example usage::
|
||||
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
|
||||
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
|
||||
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
|
||||
|
||||
.. versionadded:: 3.8
|
||||
|
||||
* ``'count'`` - This counts the number of times a keyword argument occurs. For
|
||||
example, this is useful for increasing verbosity levels::
|
||||
|
||||
|
@ -717,17 +732,6 @@ how the command-line arguments should be handled. The supplied actions are:
|
|||
>>> parser.parse_args(['--version'])
|
||||
PROG 2.0
|
||||
|
||||
* ``'extend'`` - This stores a list, and extends each argument value to the
|
||||
list.
|
||||
Example usage::
|
||||
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
|
||||
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
|
||||
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
|
||||
|
||||
.. versionadded:: 3.8
|
||||
|
||||
Only actions that consume command-line arguments (e.g. ``'store'``,
|
||||
``'append'`` or ``'extend'``) can be used with positional arguments.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue