Skip to content

Commit 419b328

Browse files
committed
Update documentation
1 parent e796fee commit 419b328

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

Doc/library/argparse.rst

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

615649
The 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

11311167
Any 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

Comments
 (0)