@@ -61,7 +61,8 @@ ArgumentParser objects
6161 formatter_class=argparse.HelpFormatter, \
6262 prefix_chars='-', fromfile_prefix_chars=None, \
6363 argument_default=None, conflict_handler='error', \
64- add_help=True, allow_abbrev=True, exit_on_error=True)
64+ add_help=True, allow_abbrev=True, exit_on_error=True, \
65+ suggest_on_error=False)
6566
6667 Create a new :class: `ArgumentParser ` object. All parameters should be passed
6768 as keyword arguments. Each parameter has its own more detailed description
@@ -103,6 +104,10 @@ ArgumentParser objects
103104 * exit_on_error _ - Determines whether or not ArgumentParser exits with
104105 error info when an error occurs. (default: ``True ``)
105106
107+ * suggest_on_error _ - Enables suggestions for mistyped argument choices
108+ and subparser names (default: ``False ``)
109+
110+
106111 .. versionchanged :: 3.5
107112 *allow_abbrev * parameter was added.
108113
@@ -559,6 +564,27 @@ If the user would like to catch errors manually, the feature can be enabled by s
559564
560565.. versionadded :: 3.9
561566
567+ suggest_on_error
568+ ^^^^^^^^^^^^^^^^
569+
570+ By default, when a user passes an invalid argument choice or subparser name,
571+ :class: `ArgumentParser ` will exit with error info and list the permissible
572+ argument choices (if specified) or subparser names as part of the error message.
573+
574+ If the user would like to enable suggestions for mistyped argument choices and
575+ subparser names, the feature can be enabled by setting ``suggest_on_error `` to
576+ ``True ``. Note that this only applies for arguments when the choices specified
577+ are strings::
578+
579+ >>> parser = argparse.ArgumentParser(description='Process some integers.', suggest_on_error=True)
580+ >>> parser.add_argument('--action', choices=['sum', 'max'])
581+ >>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
582+ ... help='an integer for the accumulator')
583+ >>> parser.parse_args(['--action', 'sumn', 1, 2, 3])
584+ tester.py: error: argument --action: invalid choice: 'sumn', maybe you meant 'sum'? (choose from 'sum', 'max')
585+
586+ .. versionadded :: 3.14
587+
562588
563589The add_argument() method
564590-------------------------
@@ -1868,6 +1894,10 @@ Argument groups
18681894 The function exists on the API by accident through inheritance and
18691895 will be removed in the future.
18701896
1897+ .. deprecated :: 3.14
1898+ Passing prefix_chars _ to :meth: `add_argument_group `
1899+ is now deprecated.
1900+
18711901
18721902Mutual exclusion
18731903^^^^^^^^^^^^^^^^
0 commit comments