| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,7 +74,7 @@ ArgumentParser objects | |||
| 74 | 74 | prefix_chars='-', fromfile_prefix_chars=None, \ | |
| 75 | 75 | argument_default=None, conflict_handler='error', \ | |
| 76 | 76 | add_help=True, allow_abbrev=True, exit_on_error=True, \ | |
| 77 | - *, suggest_on_error=False, color=True) | ||
| 77 | + *, suggest_on_error=True, color=True) | ||
| 78 | 78 | ||
| 79 | 79 | Create a new :class:`ArgumentParser` object. All parameters should be passed | |
| 80 | 80 | as keyword arguments. Each parameter has its own more detailed description | |
@@ -117,7 +117,7 @@ ArgumentParser objects | |||
| 117 | 117 | error info when an error occurs. (default: ``True``) | |
| 118 | 118 | ||
| 119 | 119 | * suggest_on_error_ - Enables suggestions for mistyped argument choices | |
| 120 | - and subparser names (default: ``False``) | ||
| 120 | + and subparser names (default: ``True``) | ||
| 121 | 121 | ||
| 122 | 122 | * color_ - Allow color output (default: ``True``) | |
| 123 | 123 | ||
@@ -134,6 +134,9 @@ ArgumentParser objects | |||
| 134 | 134 | .. versionchanged:: 3.14 | |
| 135 | 135 | *suggest_on_error* and *color* parameters were added. | |
| 136 | 136 | ||
| 137 | + .. versionchanged:: 3.15 | ||
| 138 | + *suggest_on_error* default changed to ``True``. | ||
| 139 | + | ||
| 137 | 140 | The following sections describe how each of these are used. | |
| 138 | 141 | ||
| 139 | 142 | ||
@@ -596,13 +599,11 @@ suggest_on_error | |||
| 596 | 599 | ^^^^^^^^^^^^^^^^ | |
| 597 | 600 | ||
| 598 | 601 | By default, when a user passes an invalid argument choice or subparser name, | |
| 599 | - :class:`ArgumentParser` will exit with error info and list the permissible | ||
| 600 | - argument choices (if specified) or subparser names as part of the error message. | ||
| 601 | - | ||
| 602 | - If the user would like to enable suggestions for mistyped argument choices and | ||
| 603 | - subparser names, the feature can be enabled by setting ``suggest_on_error`` to | ||
| 604 | - ``True``. Note that this only applies for arguments when the choices specified | ||
| 605 | - are strings:: | ||
| 602 | + :class:`ArgumentParser` will exit with error info and provide suggestions for | ||
| 603 | + mistyped arguments. The error message will list the permissible argument | ||
| 604 | + choices (if specified) or subparser names, along with a "maybe you meant" | ||
| 605 | + suggestion if a close match is found. Note that this only applies for arguments | ||
| 606 | + when the choices specified are strings:: | ||
| 606 | 607 | ||
| 607 | 608 | >>> parser = argparse.ArgumentParser(description='Process some integers.', | |
| 608 | 609 | suggest_on_error=True) | |
@@ -612,16 +613,14 @@ are strings:: | |||
| 612 | 613 | >>> parser.parse_args(['--action', 'sumn', 1, 2, 3]) | |
| 613 | 614 | tester.py: error: argument --action: invalid choice: 'sumn', maybe you meant 'sum'? (choose from 'sum', 'max') | |
| 614 | 615 | ||
| 615 | - If you're writing code that needs to be compatible with older Python versions | ||
| 616 | - and want to opportunistically use ``suggest_on_error`` when it's available, you | ||
| 617 | - can set it as an attribute after initializing the parser instead of using the | ||
| 618 | - keyword argument:: | ||
| 616 | + You can disable suggestions by setting ``suggest_on_error`` to ``False``:: | ||
| 619 | 617 | ||
| 620 | - >>> parser = argparse.ArgumentParser(description='Process some integers.') | ||
| 621 | - >>> parser.suggest_on_error = True | ||
| 618 | + >>> parser = argparse.ArgumentParser(description='Process some integers.', | ||
| 619 | + suggest_on_error=False) | ||
| 622 | 620 | ||
| 623 | 621 | .. versionadded:: 3.14 | |
| 624 | - | ||
| 622 | + .. versionchanged:: 3.15 | ||
| 623 | + Changed default value of ``suggest_on_error`` from ``False`` to ``True``. | ||
| 625 | 624 | ||
| 626 | 625 | color | |
| 627 | 626 | ^^^^^ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -317,6 +317,13 @@ New modules | |||
| 317 | 317 | Improved modules | |
| 318 | 318 | ================ | |
| 319 | 319 | ||
| 320 | + argparse | ||
| 321 | + -------- | ||
| 322 | + | ||
| 323 | + * Changed the *suggest_on_error* parameter of :class:`argparse.ArgumentParser` to | ||
| 324 | + default to ``True``. This enables suggestions for mistyped arguments by default. | ||
| 325 | + (Contributed by Jakob Schluse in :gh:`140450`.) | ||
| 326 | + | ||
| 320 | 327 | calendar | |
| 321 | 328 | -------- | |
| 322 | 329 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1857,7 +1857,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): | |||
| 1857 | 1857 | - exit_on_error -- Determines whether or not ArgumentParser exits with | |
| 1858 | 1858 | error info when an error occurs | |
| 1859 | 1859 | - suggest_on_error - Enables suggestions for mistyped argument choices | |
| 1860 | - and subparser names (default: ``False``) | ||
| 1860 | + and subparser names (default: ``True``) | ||
| 1861 | 1861 | - color - Allow color output in help messages (default: ``False``) | |
| 1862 | 1862 | """ | |
| 1863 | 1863 | ||
@@ -1876,7 +1876,7 @@ def __init__(self, | |||
| 1876 | 1876 | allow_abbrev=True, | |
| 1877 | 1877 | exit_on_error=True, | |
| 1878 | 1878 | *, | |
| 1879 | - suggest_on_error=False, | ||
| 1879 | + suggest_on_error=True, | ||
| 1880 | 1880 | color=True, | |
| 1881 | 1881 | ): | |
| 1882 | 1882 | superinit = super(ArgumentParser, self).__init__ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2287,7 +2287,7 @@ class TestArgumentAndSubparserSuggestions(TestCase): | |||
| 2287 | 2287 | """Test error handling and suggestion when a user makes a typo""" | |
| 2288 | 2288 | ||
| 2289 | 2289 | def test_wrong_argument_error_with_suggestions(self): | |
| 2290 | - parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2290 | + parser = ErrorRaisingArgumentParser() | ||
| 2291 | 2291 | parser.add_argument('foo', choices=['bar', 'baz']) | |
| 2292 | 2292 | with self.assertRaises(ArgumentParserError) as excinfo: | |
| 2293 | 2293 | parser.parse_args(('bazz',)) | |
@@ -2307,7 +2307,7 @@ def test_wrong_argument_error_no_suggestions(self): | |||
| 2307 | 2307 | ) | |
| 2308 | 2308 | ||
| 2309 | 2309 | def test_wrong_argument_subparsers_with_suggestions(self): | |
| 2310 | - parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2310 | + parser = ErrorRaisingArgumentParser() | ||
| 2311 | 2311 | subparsers = parser.add_subparsers(required=True) | |
| 2312 | 2312 | subparsers.add_parser('foo') | |
| 2313 | 2313 | subparsers.add_parser('bar') | |
@@ -2331,18 +2331,19 @@ def test_wrong_argument_subparsers_no_suggestions(self): | |||
| 2331 | 2331 | excinfo.exception.stderr, | |
| 2332 | 2332 | ) | |
| 2333 | 2333 | ||
| 2334 | - def test_wrong_argument_no_suggestion_implicit(self): | ||
| 2335 | - parser = ErrorRaisingArgumentParser() | ||
| 2334 | + def test_wrong_argument_with_suggestion_explicit(self): | ||
| 2335 | + parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2336 | 2336 | parser.add_argument('foo', choices=['bar', 'baz']) | |
| 2337 | 2337 | with self.assertRaises(ArgumentParserError) as excinfo: | |
| 2338 | 2338 | parser.parse_args(('bazz',)) | |
| 2339 | 2339 | self.assertIn( | |
| 2340 | - "error: argument foo: invalid choice: 'bazz' (choose from bar, baz)", | ||
| 2340 | + "error: argument foo: invalid choice: 'bazz', maybe you meant" | ||
| 2341 | + " 'baz'? (choose from bar, baz)", | ||
| 2341 | 2342 | excinfo.exception.stderr, | |
| 2342 | 2343 | ) | |
| 2343 | 2344 | ||
| 2344 | 2345 | def test_suggestions_choices_empty(self): | |
| 2345 | - parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2346 | + parser = ErrorRaisingArgumentParser() | ||
| 2346 | 2347 | parser.add_argument('foo', choices=[]) | |
| 2347 | 2348 | with self.assertRaises(ArgumentParserError) as excinfo: | |
| 2348 | 2349 | parser.parse_args(('bazz',)) | |
@@ -2352,7 +2353,7 @@ def test_suggestions_choices_empty(self): | |||
| 2352 | 2353 | ) | |
| 2353 | 2354 | ||
| 2354 | 2355 | def test_suggestions_choices_int(self): | |
| 2355 | - parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2356 | + parser = ErrorRaisingArgumentParser() | ||
| 2356 | 2357 | parser.add_argument('foo', choices=[1, 2]) | |
| 2357 | 2358 | with self.assertRaises(ArgumentParserError) as excinfo: | |
| 2358 | 2359 | parser.parse_args(('3',)) | |
@@ -2362,7 +2363,7 @@ def test_suggestions_choices_int(self): | |||
| 2362 | 2363 | ) | |
| 2363 | 2364 | ||
| 2364 | 2365 | def test_suggestions_choices_mixed_types(self): | |
| 2365 | - parser = ErrorRaisingArgumentParser(suggest_on_error=True) | ||
| 2366 | + parser = ErrorRaisingArgumentParser() | ||
| 2366 | 2367 | parser.add_argument('foo', choices=[1, '2']) | |
| 2367 | 2368 | with self.assertRaises(ArgumentParserError) as excinfo: | |
| 2368 | 2369 | parser.parse_args(('3',)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1681,6 +1681,7 @@ David Scherer | |||
| 1681 | 1681 | Wolfgang Scherer | |
| 1682 | 1682 | Felix Scherz | |
| 1683 | 1683 | Hynek Schlawack | |
| 1684 | + Jakob Schluse | ||
| 1684 | 1685 | Bob Schmertz | |
| 1685 | 1686 | Gregor Schmid | |
| 1686 | 1687 | Ralf Schmitt | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Change the default of ``suggest_on_error`` to ``True`` in | ||
| 2 | + ``argparse.ArgumentParser``. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments