summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Férotin <vincent.ferotin@gmail.com>2020-06-20 14:55:05 +0200
committerGitHub <noreply@github.com>2020-06-20 05:55:05 -0700
commit344c2a75c1c13de781962a3f80552e66a4c89024 (patch)
tree5564d52fafc241ca232dc7e9f5dbc54893e7ac99
parent935586845815f5b4c7814794413f6a812d4bd45f (diff)
downloadcpython-git-344c2a75c1c13de781962a3f80552e66a4c89024.tar.gz
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
-rw-r--r--Doc/library/argparse.rst14
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 5e0096cae7..0b64dfe47f 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -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