Skip to content

Commit 344c2a7

Browse files
bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid container for '… (GH-20964)
…choices' argument of 'argparse.ArgumentParser.add_argument'. Here's a short first proposal of doc. enhancement addressing [bpo-41024](). Automerge-Triggered-By: @csabella
1 parent 9355868 commit 344c2a7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Doc/library/argparse.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,20 @@ container should match the type_ specified::
11331133

11341134
Any container can be passed as the *choices* value, so :class:`list` objects,
11351135
:class:`set` objects, and custom containers are all supported.
1136+
This includes :class:`enum.Enum`, which could be used to restrain
1137+
argument's choices; if we reuse previous rock/paper/scissors game example,
1138+
this could be as follows::
1139+
1140+
>>> from enum import Enum
1141+
>>> class GameMove(Enum):
1142+
... ROCK = 'rock'
1143+
... PAPER = 'paper'
1144+
... SCISSORS = 'scissors'
1145+
...
1146+
>>> parser = argparse.ArgumentParser(prog='game.py')
1147+
>>> parser.add_argument('move', type=GameMove, choices=GameMove)
1148+
>>> parser.parse_args(['rock'])
1149+
Namespace(move=<GameMove.ROCK: 'rock'>)
11361150

11371151

11381152
required

0 commit comments

Comments
 (0)