@@ -74,7 +74,7 @@ ArgumentParser objects
7474 prefix_chars='-', fromfile_prefix_chars=None, \
7575 argument_default=None, conflict_handler='error', \
7676 add_help=True, allow_abbrev=True, exit_on_error=True, \
77- suggest_on_error=False)
77+ suggest_on_error=False, convert_choices=False )
7878
7979 Create a new :class: `ArgumentParser ` object. All parameters should be passed
8080 as keyword arguments. Each parameter has its own more detailed description
@@ -119,6 +119,9 @@ ArgumentParser objects
119119 * suggest_on_error _ - Enables suggestions for mistyped argument choices
120120 and subparser names (default: ``False ``)
121121
122+ * convert_choices _ - Runs the ``choices `` through the ``type `` callable
123+ during checking (default: ``False ``)
124+
122125
123126 .. versionchanged :: 3.5
124127 *allow_abbrev * parameter was added.
@@ -611,6 +614,37 @@ keyword argument::
611614
612615.. versionadded :: 3.14
613616
617+ convert_choices
618+ ^^^^^^^^^^^^^^^
619+
620+ By default, when a user passes both a ``type `` and a ``choices `` argument, the
621+ ``choices `` need to be specified in the target type, after conversion.
622+ This can cause confusing ``usage `` and ``help `` strings. If the user would like
623+ to specify ``choices `` in the same vocabulary as the end-user would enter them,
624+ this feature can be enabled by setting ``convert_choices `` to ``True ``.
625+
626+ >>> parser = argparse.ArgumentParser(convert_choices = True )
627+ >>> parser.add_argument(' when' ,
628+ ... choices= [' mo' , ' tu' , ' we' , ' th' , ' fr' , ' sa' , ' su' ],
629+ ... type = to_dow)
630+ >>> parser.print_help()
631+ usage: example_broken.py [-h] [--days {mo,tu,we,th,fr,sa,su}]
632+
633+ options:
634+ -h, --help show this help message and exit
635+ --days {mo,tu,we,th,fr,sa,su}
636+
637+
638+ If you're writing code that needs to be compatible with older Python versions
639+ and want to opportunistically use ``convert_choices `` when it's available, you
640+ can set it as an attribute after initializing the parser instead of using the
641+ keyword argument::
642+
643+ >>> parser = argparse.ArgumentParser()
644+ >>> parser.convert_choices = True
645+
646+ .. versionadded :: 3.14
647+
614648
615649The add_argument() method
616650-------------------------
@@ -1124,9 +1158,11 @@ if the argument was not one of the acceptable values::
11241158 game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11251159 'paper', 'scissors')
11261160
1127- Note that inclusion in the *choices * sequence is checked after any type _
1128- conversions have been performed, so the type of the objects in the *choices *
1129- sequence should match the type _ specified.
1161+ Note that, by default, inclusion in the *choices * sequence is checked after
1162+ any type _ conversions have been performed, so the type of the objects in the
1163+ *choices * sequence should match the type _ specified. This can lead to
1164+ confusing ``usage `` messages. If you want to convert *choices * using type _
1165+ before checking, set the ``convert_choices `` flag on :class'`ArgumentParser `.
11301166
11311167Any sequence can be passed as the *choices * value, so :class: `list ` objects,
11321168:class: `tuple ` objects, and custom sequences are all supported.
0 commit comments