mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid container for '… (GH-20964) (GH-21527)
This commit is contained in:
parent
a16ac4e43c
commit
760552ceb8
1 changed files with 14 additions and 0 deletions
|
@ -1133,6 +1133,20 @@ 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
|
||||||
|
>>> class GameMove(Enum):
|
||||||
|
... 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