]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid container for '...
authorVincent Férotin <vincent.ferotin@gmail.com>
Sat, 20 Jun 2020 12:55:05 +0000 (14:55 +0200)
committerGitHub <noreply@github.com>
Sat, 20 Jun 2020 12:55:05 +0000 (05:55 -0700)
…choices' argument of 'argparse.ArgumentParser.add_argument'.

Here's a short first proposal of doc. enhancement addressing [bpo-41024]().

Automerge-Triggered-By: @csabella
Doc/library/argparse.rst

index 5e0096cae73a798b3704a06f09523c86fb0e2aec..0b64dfe47f7689d57b0e466658ff8a4abafd1e2c 100644 (file)
@@ -1133,6 +1133,20 @@ container should match the type_ specified::
 
 Any container can be passed as the *choices* value, so :class:`list` objects,
 :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