https://github.com/python/cpython/commit/a2341ad86bd6d4361f19550eefb1e7c8b006f37a commit: a2341ad86bd6d4361f19550eefb1e7c8b006f37a branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: savannahostrowski <[email protected]> date: 2026-10-22T09:20:06-07:00 summary:
[3.14] gh-155403: Clarify argparse `default` precedence (GH-160064) (#160260) gh-155403: Clarify argparse `default` precedence (GH-160064) (cherry picked from commit 1705e512b3098dce53991234a49636de2167efc3) Co-authored-by: Savannah Ostrowski <[email protected]> files: M Doc/library/argparse.rst diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 20b69f9d1b821a..ad893388952c8f 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -785,6 +785,9 @@ how the command-line arguments should be handled. The supplied actions are: >>> parser.parse_args('--foo --bar'.split()) Namespace(foo=True, bar=False, baz=True) + These defaults apply when the options are absent. They can be overridden + by default_ or argument_default_. + * ``'append'`` - This appends each argument value to a list. It is useful for allowing an option to be specified multiple times. If the default value is a non-empty list, the parsed value will start @@ -1008,10 +1011,8 @@ default All optional arguments and some positional arguments may be omitted at the command line. The ``default`` keyword argument of -:meth:`~ArgumentParser.add_argument`, whose value defaults to ``None``, -specifies what value should be used if the command-line argument is not present. -For optional arguments, the ``default`` value is used when the option string -was not present at the command line:: +:meth:`~ArgumentParser.add_argument` specifies what value should be used if the +command-line argument is not present. For example:: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default=42) @@ -1020,6 +1021,11 @@ was not present at the command line:: >>> parser.parse_args([]) Namespace(foo=42) +If ``default`` is omitted, :meth:`~ArgumentParser.add_argument` uses a value +previously set for the dest_ with :meth:`~ArgumentParser.set_defaults`, or +otherwise a non-``None`` argument_default_. If neither is set, the action's +own default is used. + If the target namespace already has an attribute set, the action *default* will not overwrite it:: _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
