@@ -722,8 +722,9 @@ The :meth:`~ArgumentParser.add_argument` method must know whether an optional
722
722
argument, like ``-f `` or ``--foo ``, or a positional argument, like a list of
723
723
filenames, is expected. The first arguments passed to
724
724
:meth: `~ArgumentParser.add_argument ` must therefore be either a series of
725
- flags, or a simple argument name. For example, an optional argument could
726
- be created like::
725
+ flags, or a simple argument name.
726
+
727
+ For example, an optional argument could be created like::
727
728
728
729
>>> parser.add_argument('-f', '--foo')
729
730
@@ -765,8 +766,9 @@ how the command-line arguments should be handled. The supplied actions are:
765
766
Namespace(foo='1')
766
767
767
768
* ``'store_const' `` - This stores the value specified by the const _ keyword
768
- argument. The ``'store_const' `` action is most commonly used with
769
- optional arguments that specify some sort of flag. For example::
769
+ argument; note that the const _ keyword argument defaults to ``None ``. The
770
+ ``'store_const' `` action is most commonly used with optional arguments that
771
+ specify some sort of flag. For example::
770
772
771
773
>>> parser = argparse.ArgumentParser()
772
774
>>> parser.add_argument('--foo', action='store_const', const=42)
@@ -795,8 +797,8 @@ how the command-line arguments should be handled. The supplied actions are:
795
797
Namespace(foo=['1', '2'])
796
798
797
799
* ``'append_const' `` - This stores a list, and appends the value specified by
798
- the const _ keyword argument to the list. (Note that the const _ keyword
799
- argument defaults to ``None ``.) The ``'append_const' `` action is typically
800
+ the const _ keyword argument to the list; note that the const _ keyword
801
+ argument defaults to ``None ``. The ``'append_const' `` action is typically
800
802
useful when multiple arguments need to store constants to the same list. For
801
803
example::
802
804
@@ -979,17 +981,20 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
979
981
``action='store_const' `` or ``action='append_const' ``. These actions add the
980
982
``const `` value to one of the attributes of the object returned by
981
983
:meth: `~ArgumentParser.parse_args `. See the action _ description for examples.
984
+ If ``const `` is not provided to :meth: `~ArgumentParser.add_argument `, it will
985
+ receive a default value of ``None ``.
986
+
982
987
983
988
* When :meth: `~ArgumentParser.add_argument ` is called with option strings
984
989
(like ``-f `` or ``--foo ``) and ``nargs='?' ``. This creates an optional
985
990
argument that can be followed by zero or one command-line arguments.
986
991
When parsing the command line, if the option string is encountered with no
987
- command-line argument following it, the value of ``const `` will be assumed instead.
988
- See the nargs _ description for examples.
989
-
990
- With the ``'store_const' `` and ``'append_const' `` actions, the ``const ``
991
- keyword argument must be given. For other actions, it defaults to ``None ``.
992
+ command-line argument following it, the value of ``const `` will be assumed to
993
+ be ``None `` instead. See the nargs _ description for examples.
992
994
995
+ .. versionchanged :: 3.11
996
+ ``const=None `` by default, including when ``action='append_const' `` or
997
+ ``action='store_const' ``.
993
998
994
999
default
995
1000
^^^^^^^
0 commit comments