diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-10-25 08:41:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-25 08:41:38 (GMT) |
commit | da8673da362a2135cd621ac619d3aced6bb55100 (patch) | |
tree | 5f3293d4f34960030739296740504a5bbacf08f3 /Doc/library | |
parent | 75401febc91a449cc4f4391d663e9a96ce91bb6c (diff) | |
download | cpython-da8673da362a2135cd621ac619d3aced6bb55100.zip cpython-da8673da362a2135cd621ac619d3aced6bb55100.tar.gz cpython-da8673da362a2135cd621ac619d3aced6bb55100.tar.bz2 |
gh-84545: Clarify the 'extend' action documentation in argparse (GH-125870)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/argparse.rst | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 65663d4..9406982 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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 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:: @@ -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. |