mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-140448: Default suggest_on_error to True in argparse.ArgumentParser (#140450)
This commit is contained in:
parent
d51be28876
commit
d2f3cfd384
6 changed files with 36 additions and 26 deletions
|
|
@ -2287,7 +2287,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
|
|||
"""Test error handling and suggestion when a user makes a typo"""
|
||||
|
||||
def test_wrong_argument_error_with_suggestions(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
parser.add_argument('foo', choices=['bar', 'baz'])
|
||||
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||
parser.parse_args(('bazz',))
|
||||
|
|
@ -2307,7 +2307,7 @@ def test_wrong_argument_error_no_suggestions(self):
|
|||
)
|
||||
|
||||
def test_wrong_argument_subparsers_with_suggestions(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
subparsers = parser.add_subparsers(required=True)
|
||||
subparsers.add_parser('foo')
|
||||
subparsers.add_parser('bar')
|
||||
|
|
@ -2331,18 +2331,19 @@ def test_wrong_argument_subparsers_no_suggestions(self):
|
|||
excinfo.exception.stderr,
|
||||
)
|
||||
|
||||
def test_wrong_argument_no_suggestion_implicit(self):
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
def test_wrong_argument_with_suggestion_explicit(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
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,
|
||||
)
|
||||
|
||||
def test_suggestions_choices_empty(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
parser.add_argument('foo', choices=[])
|
||||
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||
parser.parse_args(('bazz',))
|
||||
|
|
@ -2352,7 +2353,7 @@ def test_suggestions_choices_empty(self):
|
|||
)
|
||||
|
||||
def test_suggestions_choices_int(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
parser.add_argument('foo', choices=[1, 2])
|
||||
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||
parser.parse_args(('3',))
|
||||
|
|
@ -2362,7 +2363,7 @@ def test_suggestions_choices_int(self):
|
|||
)
|
||||
|
||||
def test_suggestions_choices_mixed_types(self):
|
||||
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
|
||||
parser = ErrorRaisingArgumentParser()
|
||||
parser.add_argument('foo', choices=[1, '2'])
|
||||
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||
parser.parse_args(('3',))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue