mirror of
https://github.com/python/cpython.git
synced 2025-09-30 12:21:51 +00:00
bpo-42501: Revise the usage note for Enums with the choices (GH-23563) (GH-23573)
This commit is contained in:
parent
cf22aa3bc6
commit
aab9390334
1 changed files with 2 additions and 13 deletions
|
@ -1133,20 +1133,9 @@ container should match the type_ specified::
|
||||||
|
|
||||||
Any container can be passed as the *choices* value, so :class:`list` objects,
|
Any container can be passed as the *choices* value, so :class:`list` objects,
|
||||||
:class:`set` objects, and custom containers are all supported.
|
:class:`set` objects, and custom containers are all supported.
|
||||||
This includes :class:`enum.Enum`, which could be used to restrain
|
|
||||||
argument's choices; if we reuse previous rock/paper/scissors game example,
|
|
||||||
this could be as follows::
|
|
||||||
|
|
||||||
>>> from enum import Enum
|
Use of :class:`enum.Enum` is not recommended because it is difficult to
|
||||||
>>> class GameMove(Enum):
|
control its appearance in usage, help, and error messages.
|
||||||
... ROCK = 'rock'
|
|
||||||
... PAPER = 'paper'
|
|
||||||
... SCISSORS = 'scissors'
|
|
||||||
...
|
|
||||||
>>> parser = argparse.ArgumentParser(prog='game.py')
|
|
||||||
>>> parser.add_argument('move', type=GameMove, choices=GameMove)
|
|
||||||
>>> parser.parse_args(['rock'])
|
|
||||||
Namespace(move=<GameMove.ROCK: 'rock'>)
|
|
||||||
|
|
||||||
|
|
||||||
required
|
required
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue