Skip to content

Commit 0bbe65c

Browse files
committed
Improve docs on *type* and *choices*.
1 parent a16586c commit 0bbe65c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Doc/library/argparse.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,21 +1124,28 @@ if the argument was not one of the acceptable values::
11241124
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
11251125
'paper', 'scissors')
11261126

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.
1130-
11311127
Any sequence can be passed as the *choices* value, so :class:`list` objects,
11321128
:class:`tuple` objects, and custom sequences are all supported.
11331129

1134-
Use of :class:`enum.Enum` is not recommended because it is difficult to
1135-
control its appearance in usage, help, and error messages.
1136-
11371130
Formatted choices override the default *metavar* which is normally derived
11381131
from *dest*. This is usually what you want because the user never sees the
11391132
*dest* parameter. If this display isn't desirable (perhaps because there are
11401133
many choices), just specify an explicit metavar_.
11411134

1135+
Note that inclusion in the *choices* sequence is checked after any type_
1136+
conversions have been performed, so the type of the objects in the *choices*
1137+
sequence should match the type_ specified. This may interfere with
1138+
the display of the choices in usage, help and error messages, because the
1139+
*choices* may not be familiar to the end-user.
1140+
1141+
Use of :class:`enum.Enum` is not recommended because it is difficult to
1142+
control its appearance in these messages.
1143+
1144+
If you have to use both *type* and *choices* you can use a wrapper class
1145+
that formats the value after type conversion back to the vocabulary of the
1146+
end-user. However, it is usually easier to not specify *type* and perform
1147+
type conversion in application code.
1148+
11421149

11431150
.. _required:
11441151

0 commit comments

Comments
 (0)