Skip to content

Commit

Permalink
pythongh-84545: Clarify the 'extend' action documentation in argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Oct 23, 2024
1 parent c75ff2e commit 6324868
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,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 the list.
The ``'extend'`` action is typically used with the nargs_ keyword argument
value ``'+'`` or ``'*'``;
note that when nargs_ is ``None`` (by default) or ``'?'``, separate
characters 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::

Expand All @@ -766,17 +781,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.

Expand Down

0 comments on commit 6324868

Please sign in to comment.