Skip to content

Commit ba8d0e3

Browse files
[3.15] GH-59633: Clarify dest collisions in argparse docs (GH-150987) (#151188)
GH-59633: Clarify dest collisions in argparse docs (GH-150987) (cherry picked from commit 82cb7d4) Co-authored-by: Savannah Ostrowski <savannah@python.org>
1 parent 489d6af commit ba8d0e3

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Doc/library/argparse.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,11 @@ behavior::
13721372
>>> parser.parse_args('--foo XXX'.split())
13731373
Namespace(bar='XXX')
13741374

1375+
Multiple arguments may share the same ``dest``. By default, the value from the
1376+
last such argument given on the command line wins. Use ``action='append'`` to
1377+
collect values from all of them into a list instead. For conflicting *option
1378+
strings* rather than ``dest`` names, see conflict_handler_.
1379+
13751380
.. versionchanged:: 3.15
13761381
Single-dash long option now takes precedence over short options.
13771382

@@ -1780,6 +1785,11 @@ Subcommands
17801785
present, and when the ``b`` command is specified, only the ``foo`` and
17811786
``baz`` attributes are present.
17821787

1788+
If a subparser defines an argument with the same ``dest`` as the parent
1789+
parser, the two share a single namespace attribute, so the parent's value
1790+
won't be retained. Users should give them distinct ``dest`` values to
1791+
keep both.
1792+
17831793
Similarly, when a help message is requested from a subparser, only the help
17841794
for that particular parser will be printed. The help message will not
17851795
include parent parser or sibling parser messages. (A help message for each

0 commit comments

Comments
 (0)