Skip to content

Commit ad5982b

Browse files
v0ropaevmiss-islington
authored andcommitted
gh-110131: Document how a non-empty default works with append and extend (GH-158155)
(cherry picked from commit 70bc048) Co-authored-by: Dmitry Voropaev <144015565+v0ropaev@users.noreply.github.com>
1 parent fc53775 commit ad5982b

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎Doc/library/argparse.rst‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ how the command-line arguments should be handled. The supplied actions are:
791791

792792
* ``'append'`` - This appends each argument value to a list.
793793
It is useful for allowing an option to be specified multiple times.
794-
If the default value is a non-empty list, the parsed value will start
795-
with the default list's elements and any values from the command line
796-
will be appended after those default values. Example usage::
794+
If the default value is a non-empty list, the parsed value for the option
795+
will start with the default list's elements and any values from the
796+
command line will be appended after those default values. Example usage::
797797

798798
>>> parser = argparse.ArgumentParser()
799799
>>> parser.add_argument('--foo', action='append', default=['0'])
@@ -818,12 +818,16 @@ how the command-line arguments should be handled. The supplied actions are:
818818
value ``'+'`` or ``'*'``.
819819
Note that when nargs_ is ``None`` (the default) or ``'?'``, each
820820
character of the argument string will be appended to the list.
821+
If the default value is a non-empty list, the parsed value for the option
822+
will start with the default list's elements and any values from the
823+
command line will be appended after those default values.
821824
Example usage::
822825

823826
>>> parser = argparse.ArgumentParser()
824-
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
827+
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str,
828+
... default=["d1"])
825829
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
826-
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
830+
Namespace(foo=['d1', 'f1', 'f2', 'f3', 'f4'])
827831

828832
.. versionadded:: 3.8
829833

0 commit comments

Comments
 (0)