@@ -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, convert_choices=False )
77+ suggest_on_error=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,9 +119,6 @@ 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-
125122
126123 .. versionchanged :: 3.5
127124 *allow_abbrev * parameter was added.
@@ -615,38 +612,6 @@ keyword argument::
615612.. versionadded :: 3.14
616613
617614
618- convert_choices
619- ^^^^^^^^^^^^^^^
620-
621- By default, when a user passes both a ``type `` and a ``choices `` argument, the
622- ``choices `` need to be specified in the target type, after conversion.
623- This can cause confusing ``usage `` and ``help `` strings. If the user would like
624- to specify ``choices `` in the same vocabulary as the end-user would enter them,
625- this feature can be enabled by setting ``convert_choices `` to ``True ``::
626-
627- >>> parser = argparse.ArgumentParser(convert_choices=True)
628- >>> parser.add_argument('when',
629- ... choices=['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su'],
630- ... type=to_dow)
631- >>> parser.print_help()
632- usage: example_broken.py [-h] [--days {mo,tu,we,th,fr,sa,su}]
633-
634- options:
635- -h, --help show this help message and exit
636- --days {mo,tu,we,th,fr,sa,su}
637-
638-
639- If you're writing code that needs to be compatible with older Python versions
640- and want to opportunistically use ``convert_choices `` when it's available, you
641- can set it as an attribute after initializing the parser instead of using the
642- keyword argument::
643-
644- >>> parser = argparse.ArgumentParser()
645- >>> parser.convert_choices = True
646-
647- .. versionadded :: next
648-
649-
650615The add_argument() method
651616-------------------------
652617
@@ -674,6 +639,9 @@ The add_argument() method
674639
675640 * choices _ - A sequence of the allowable values for the argument.
676641
642+ * convert_choices _ - Whether to convert the choices _ using the type _ callable
643+ before checking.
644+
677645 * required _ - Whether or not the command-line option may be omitted
678646 (optionals only).
679647
@@ -1159,24 +1127,39 @@ if the argument was not one of the acceptable values::
11591127 game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11601128 'paper', 'scissors')
11611129
1162- Note that, by default, inclusion in the *choices * sequence is checked after
1163- any type _ conversions have been performed, so the type of the objects in the
1164- *choices * sequence should match the type _ specified. This can lead to
1165- confusing ``usage `` messages. If you want to convert *choices * using type _
1166- before checking, set the ``convert_choices `` flag on :class: `~ArgumentParser `.
1167-
1168-
11691130Any sequence can be passed as the *choices * value, so :class: `list ` objects,
11701131:class: `tuple ` objects, and custom sequences are all supported.
11711132
1172- Use of :class: `enum.Enum ` is not recommended because it is difficult to
1173- control its appearance in usage, help, and error messages.
1174-
11751133Formatted choices override the default *metavar * which is normally derived
11761134from *dest *. This is usually what you want because the user never sees the
11771135*dest * parameter. If this display isn't desirable (perhaps because there are
11781136many choices), just specify an explicit metavar _.
11791137
1138+ .. _convert_choices :
1139+
1140+ convert_choices
1141+ ^^^^^^^^^^^^^^^
1142+
1143+ By default, when a user passes both a ``type `` and a ``choices `` argument, the
1144+ ``choices `` need to be specified in the target type, after conversion.
1145+ This can cause confusing ``usage `` and ``help `` strings. If the user would like
1146+ to specify ``choices `` in the same vocabulary as the end-user would enter them,
1147+ this feature can be enabled by setting ``convert_choices `` to ``True ``::
1148+
1149+ >>> parser = argparse.ArgumentParser()
1150+ >>> parser.add_argument('when',
1151+ ... choices=['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su'],
1152+ ... convert_choices=True,
1153+ ... type=to_dow)
1154+ >>> parser.print_help()
1155+ usage: example_broken.py [-h] [--days {mo,tu,we,th,fr,sa,su}]
1156+
1157+ options:
1158+ -h, --help show this help message and exit
1159+ --days {mo,tu,we,th,fr,sa,su}
1160+
1161+ .. versionadded :: next
1162+
11801163
11811164.. _required :
11821165
0 commit comments