Skip to content

Commit ad9b851

Browse files
committed
Update documentation
1 parent 285da4a commit ad9b851

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

Doc/library/argparse.rst

Lines changed: 42 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.
@@ -612,6 +615,38 @@ keyword argument::
612615
.. versionadded:: 3.14
613616

614617

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+
615650
The add_argument() method
616651
-------------------------
617652

@@ -1124,9 +1159,12 @@ if the argument was not one of the acceptable values::
11241159
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11251160
'paper', 'scissors')
11261161

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.
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+
11301168

11311169
Any sequence can be passed as the *choices* value, so :class:`list` objects,
11321170
:class:`tuple` objects, and custom sequences are all supported.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
:mod:`argparse` now allows `choices` to be entered as strings and calls
2-
convert on the available choices during checking.
1+
:mod:`argparse` Add option ``convert_choices`` to :class:`ArgumentParser`. Combined with ``type``, allows ``choices``
2+
to be entered as strings and converted during argument checking.
3+

0 commit comments

Comments
 (0)