Skip to content

Commit 1705e51

Browse files
gh-155403: Clarify argparse default precedence (#160064)
1 parent a930e52 commit 1705e51

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎Doc/library/argparse.rst‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,9 @@ how the command-line arguments should be handled. The supplied actions are:
789789
>>> parser.parse_args('--foo --bar'.split())
790790
Namespace(foo=True, bar=False, baz=True)
791791

792+
These defaults apply when the options are absent. They can be overridden
793+
by default_ or argument_default_.
794+
792795
* ``'append'`` - This appends each argument value to a list.
793796
It is useful for allowing an option to be specified multiple times.
794797
If the default value is a non-empty list, the parsed value for the option
@@ -1016,10 +1019,8 @@ default
10161019

10171020
All optional arguments and some positional arguments may be omitted at the
10181021
command line. The ``default`` keyword argument of
1019-
:meth:`~ArgumentParser.add_argument`, whose value defaults to ``None``,
1020-
specifies what value should be used if the command-line argument is not present.
1021-
For optional arguments, the ``default`` value is used when the option string
1022-
was not present at the command line::
1022+
:meth:`~ArgumentParser.add_argument` specifies what value should be used if the
1023+
command-line argument is not present. For example::
10231024

10241025
>>> parser = argparse.ArgumentParser()
10251026
>>> parser.add_argument('--foo', default=42)
@@ -1028,6 +1029,11 @@ was not present at the command line::
10281029
>>> parser.parse_args([])
10291030
Namespace(foo=42)
10301031

1032+
If ``default`` is omitted, :meth:`~ArgumentParser.add_argument` uses a value
1033+
previously set for the dest_ with :meth:`~ArgumentParser.set_defaults`, or
1034+
otherwise a non-``None`` argument_default_. If neither is set, the action's
1035+
own default is used.
1036+
10311037
If the target namespace already has an attribute set, the action *default*
10321038
will not overwrite it::
10331039

0 commit comments

Comments
 (0)