argument, like ``-f`` or ``--foo``, or a positional argument, like a list of
filenames, is expected. The first arguments passed to
:meth:`~ArgumentParser.add_argument` must therefore be either a series of
-flags, or a simple argument name. For example, an optional argument could
-be created like::
+flags, or a simple argument name.
+
+For example, an optional argument could be created like::
>>> parser.add_argument('-f', '--foo')
Namespace(foo='1')
* ``'store_const'`` - This stores the value specified by the const_ keyword
- argument. The ``'store_const'`` action is most commonly used with
- optional arguments that specify some sort of flag. For example::
+ argument; note that the const_ keyword argument defaults to ``None``. The
+ ``'store_const'`` action is most commonly used with optional arguments that
+ specify some sort of flag. For example::
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_const', const=42)
Namespace(foo=['1', '2'])
* ``'append_const'`` - This stores a list, and appends the value specified by
- the const_ keyword argument to the list. (Note that the const_ keyword
- argument defaults to ``None``.) The ``'append_const'`` action is typically
+ the const_ keyword argument to the list; note that the const_ keyword
+ argument defaults to ``None``. The ``'append_const'`` action is typically
useful when multiple arguments need to store constants to the same list. For
example::
``action='store_const'`` or ``action='append_const'``. These actions add the
``const`` value to one of the attributes of the object returned by
:meth:`~ArgumentParser.parse_args`. See the action_ description for examples.
+ If ``const`` is not provided to :meth:`~ArgumentParser.add_argument`, it will
+ receive a default value of ``None``.
+
* When :meth:`~ArgumentParser.add_argument` is called with option strings
(like ``-f`` or ``--foo``) and ``nargs='?'``. This creates an optional
argument that can be followed by zero or one command-line arguments.
When parsing the command line, if the option string is encountered with no
- command-line argument following it, the value of ``const`` will be assumed instead.
- See the nargs_ description for examples.
-
-With the ``'store_const'`` and ``'append_const'`` actions, the ``const``
-keyword argument must be given. For other actions, it defaults to ``None``.
+ command-line argument following it, the value of ``const`` will be assumed to
+ be ``None`` instead. See the nargs_ description for examples.
+.. versionchanged:: 3.11
+ ``const=None`` by default, including when ``action='append_const'`` or
+ ``action='store_const'``.
default
^^^^^^^
]
+class TestConstActionsMissingConstKwarg(ParserTestCase):
+ """Tests that const gets default value of None when not provided"""
+
+ argument_signatures = [
+ Sig('-f', action='append_const'),
+ Sig('--foo', action='append_const'),
+ Sig('-b', action='store_const'),
+ Sig('--bar', action='store_const')
+ ]
+ failures = ['-f v', '--foo=bar', '--foo bar']
+ successes = [
+ ('', NS(f=None, foo=None, b=None, bar=None)),
+ ('-f', NS(f=[None], foo=None, b=None, bar=None)),
+ ('--foo', NS(f=None, foo=[None], b=None, bar=None)),
+ ('-b', NS(f=None, foo=None, b=None, bar=None)),
+ ('--bar', NS(f=None, foo=None, b=None, bar=None)),
+ ]
+
+
class TestOptionalsActionAppendConst(ParserTestCase):
"""Tests the append_const action for an Optional"""