8000 [3.13] gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870) by miss-islington · Pull Request #125964 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870) #125964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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.

Expand Down
Loading
0