Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ArgumentParser objects
prefix_chars='-', fromfile_prefix_chars=None, \
argument_default=None, conflict_handler='error', \
add_help=True, allow_abbrev=True, exit_on_error=True, \
*, suggest_on_error=False, color=True)
*, suggest_on_error=True, color=True)

Create a new :class:`ArgumentParser` object. All parameters should be passed
as keyword arguments. Each parameter has its own more detailed description
Expand Down Expand Up @@ -117,7 +117,7 @@ ArgumentParser objects
error info when an error occurs. (default: ``True``)

* suggest_on_error_ - Enables suggestions for mistyped argument choices
and subparser names (default: ``False``)
and subparser names (default: ``True``)

* color_ - Allow color output (default: ``True``)

Expand All @@ -134,6 +134,9 @@ ArgumentParser objects
.. versionchanged:: 3.14
*suggest_on_error* and *color* parameters were added.

.. versionchanged:: 3.15
*suggest_on_error* default changed to ``True``.

The following sections describe how each of these are used.


Expand Down Expand Up @@ -596,13 +599,11 @@ suggest_on_error
^^^^^^^^^^^^^^^^

By default, when a user passes an invalid argument choice or subparser name,
:class:`ArgumentParser` will exit with error info and list the permissible
argument choices (if specified) or subparser names as part of the error message.

If the user would like to enable suggestions for mistyped argument choices and
subparser names, the feature can be enabled by setting ``suggest_on_error`` to
``True``. Note that this only applies for arguments when the choices specified
are strings::
:class:`ArgumentParser` will exit with error info and provide suggestions for
mistyped arguments. The error message will list the permissible argument
choices (if specified) or subparser names, along with a "maybe you meant"
suggestion if a close match is found. Note that this only applies for arguments
when the choices specified are strings::

>>> parser = argparse.ArgumentParser(description='Process some integers.',
suggest_on_error=True)
Expand All @@ -612,16 +613,15 @@ are strings::
>>> parser.parse_args(['--action', 'sumn', 1, 2, 3])
tester.py: error: argument --action: invalid choice: 'sumn', maybe you meant 'sum'? (choose from 'sum', 'max')

If you're writing code that needs to be compatible with older Python versions
and want to opportunistically use ``suggest_on_error`` when it's available, you
can set it as an attribute after initializing the parser instead of using the
keyword argument::
If you prefer the old behavior without suggestions, you can disable this feature
by setting ``suggest_on_error`` to ``False``::

>>> parser = argparse.ArgumentParser(description='Process some integers.')
>>> parser.suggest_on_error = True
>>> parser = argparse.ArgumentParser(description='Process some integers.',
suggest_on_error=False)

.. versionadded:: 3.14

.. versionchanged:: 3.15
Changed default value of ``suggest_on_error`` from ``False`` to ``True``.

color
^^^^^
Expand Down
4 changes: 2 additions & 2 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
- exit_on_error -- Determines whether or not ArgumentParser exits with
error info when an error occurs
- suggest_on_error - Enables suggestions for mistyped argument choices
and subparser names (default: ``False``)
and subparser names (default: ``True``)
- color - Allow color output in help messages (default: ``False``)
"""

Expand All @@ -1876,7 +1876,7 @@ def __init__(self,
allow_abbrev=True,
exit_on_error=True,
*,
suggest_on_error=False,
suggest_on_error=True,
color=True,
):
superinit = super(ArgumentParser, self).__init__
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2331,13 +2331,14 @@ def test_wrong_argument_subparsers_no_suggestions(self):
excinfo.exception.stderr,
)

def test_wrong_argument_no_suggestion_implicit(self):
def test_wrong_argument_with_suggestion_implicit(self):
parser = ErrorRaisingArgumentParser()
parser.add_argument('foo', choices=['bar', 'baz'])
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('bazz',))
self.assertIn(
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
"error: argument foo: invalid choice: 'bazz', maybe you meant"
" 'baz'? (choose from bar, baz)",
excinfo.exception.stderr,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change the default of ``suggest_on_error`` to ``True`` in
``argparse.ArgumentParser``.
Loading