mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707)
This commit is contained in:
parent
1cf8424a62
commit
0ad173249d
4 changed files with 41 additions and 13 deletions
|
@ -722,8 +722,9 @@ The :meth:`~ArgumentParser.add_argument` method must know whether an optional
|
||||||
argument, like ``-f`` or ``--foo``, or a positional argument, like a list of
|
argument, like ``-f`` or ``--foo``, or a positional argument, like a list of
|
||||||
filenames, is expected. The first arguments passed to
|
filenames, is expected. The first arguments passed to
|
||||||
:meth:`~ArgumentParser.add_argument` must therefore be either a series of
|
:meth:`~ArgumentParser.add_argument` must therefore be either a series of
|
||||||
flags, or a simple argument name. For example, an optional argument could
|
flags, or a simple argument name.
|
||||||
be created like::
|
|
||||||
|
For example, an optional argument could be created like::
|
||||||
|
|
||||||
>>> parser.add_argument('-f', '--foo')
|
>>> parser.add_argument('-f', '--foo')
|
||||||
|
|
||||||
|
@ -765,8 +766,9 @@ how the command-line arguments should be handled. The supplied actions are:
|
||||||
Namespace(foo='1')
|
Namespace(foo='1')
|
||||||
|
|
||||||
* ``'store_const'`` - This stores the value specified by the const_ keyword
|
* ``'store_const'`` - This stores the value specified by the const_ keyword
|
||||||
argument. The ``'store_const'`` action is most commonly used with
|
argument; note that the const_ keyword argument defaults to ``None``. The
|
||||||
optional arguments that specify some sort of flag. For example::
|
``'store_const'`` action is most commonly used with optional arguments that
|
||||||
|
specify some sort of flag. For example::
|
||||||
|
|
||||||
>>> parser = argparse.ArgumentParser()
|
>>> parser = argparse.ArgumentParser()
|
||||||
>>> parser.add_argument('--foo', action='store_const', const=42)
|
>>> 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:
|
||||||
Namespace(foo=['1', '2'])
|
Namespace(foo=['1', '2'])
|
||||||
|
|
||||||
* ``'append_const'`` - This stores a list, and appends the value specified by
|
* ``'append_const'`` - This stores a list, and appends the value specified by
|
||||||
the const_ keyword argument to the list. (Note that the const_ keyword
|
the const_ keyword argument to the list; note that the const_ keyword
|
||||||
argument defaults to ``None``.) The ``'append_const'`` action is typically
|
argument defaults to ``None``. The ``'append_const'`` action is typically
|
||||||
useful when multiple arguments need to store constants to the same list. For
|
useful when multiple arguments need to store constants to the same list. For
|
||||||
example::
|
example::
|
||||||
|
|
||||||
|
@ -979,17 +981,20 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
|
||||||
``action='store_const'`` or ``action='append_const'``. These actions add the
|
``action='store_const'`` or ``action='append_const'``. These actions add the
|
||||||
``const`` value to one of the attributes of the object returned by
|
``const`` value to one of the attributes of the object returned by
|
||||||
:meth:`~ArgumentParser.parse_args`. See the action_ description for examples.
|
: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
|
* When :meth:`~ArgumentParser.add_argument` is called with option strings
|
||||||
(like ``-f`` or ``--foo``) and ``nargs='?'``. This creates an optional
|
(like ``-f`` or ``--foo``) and ``nargs='?'``. This creates an optional
|
||||||
argument that can be followed by zero or one command-line arguments.
|
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
|
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.
|
command-line argument following it, the value of ``const`` will be assumed to
|
||||||
See the nargs_ description for examples.
|
be ``None`` 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``.
|
|
||||||
|
|
||||||
|
.. versionchanged:: 3.11
|
||||||
|
``const=None`` by default, including when ``action='append_const'`` or
|
||||||
|
``action='store_const'``.
|
||||||
|
|
||||||
default
|
default
|
||||||
^^^^^^^
|
^^^^^^^
|
||||||
|
|
|
@ -855,6 +855,7 @@ class Action(_AttributeHolder):
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
raise NotImplementedError(_('.__call__() not defined'))
|
raise NotImplementedError(_('.__call__() not defined'))
|
||||||
|
|
||||||
|
|
||||||
class BooleanOptionalAction(Action):
|
class BooleanOptionalAction(Action):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
option_strings,
|
option_strings,
|
||||||
|
@ -936,7 +937,7 @@ class _StoreConstAction(Action):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
option_strings,
|
option_strings,
|
||||||
dest,
|
dest,
|
||||||
const,
|
const=None,
|
||||||
default=None,
|
default=None,
|
||||||
required=False,
|
required=False,
|
||||||
help=None,
|
help=None,
|
||||||
|
@ -1031,7 +1032,7 @@ class _AppendConstAction(Action):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
option_strings,
|
option_strings,
|
||||||
dest,
|
dest,
|
||||||
const,
|
const=None,
|
||||||
default=None,
|
default=None,
|
||||||
required=False,
|
required=False,
|
||||||
help=None,
|
help=None,
|
||||||
|
|
|
@ -745,6 +745,25 @@ class TestOptionalsActionAppendWithDefault(ParserTestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
class TestOptionalsActionAppendConst(ParserTestCase):
|
||||||
"""Tests the append_const action for an Optional"""
|
"""Tests the append_const action for an Optional"""
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
argparse actions store_const and append_const each receive a default value
|
||||||
|
of None when the ``const`` kwarg is not provided. Previously, this raised a
|
||||||
|
:exc:`TypeError`.
|
Loading…
Add table
Add a link
Reference in a new issue