Skip to content

Commit ea23396

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 9dd852d commit ea23396

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
@@ -690,9 +690,9 @@ how the command-line arguments should be handled. The supplied actions are:
690690

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

697697
>>> parser = argparse.ArgumentParser()
698698
>>> parser.add_argument('--foo', action='append', default=['0'])
@@ -717,12 +717,16 @@ how the command-line arguments should be handled. The supplied actions are:
717717
value ``'+'`` or ``'*'``.
718718
Note that when nargs_ is ``None`` (the default) or ``'?'``, each
719719
character of the argument string will be appended to the list.
720+
If the default value is a non-empty list, the parsed value for the option
721+
will start with the default list's elements and any values from the
722+
command line will be appended after those default values.
720723
Example usage::
721724

722725
>>> parser = argparse.ArgumentParser()
723-
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
726+
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str,
727+
... default=["d1"])
724728
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
725-
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
729+
Namespace(foo=['d1', 'f1', 'f2', 'f3', 'f4'])
726730

727731
.. versionadded:: 3.8
728732

0 commit comments

Comments
 (0)