aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-07-17 16:43:03 -0700
committerGitHub <noreply@github.com>2020-07-17 16:43:03 -0700
commit760552ceb8c5f5ca4f1bf13f47543b42b25e0b83 (patch)
tree1eb3be5fc3119425a6b51a01c42b1b31353f8c8c
parentbpo-41304: Update NEWS to include CVE-2020-15801 reference (GH-21521) (diff)
downloadcpython-760552ceb8c5f5ca4f1bf13f47543b42b25e0b83.tar.gz
cpython-760552ceb8c5f5ca4f1bf13f47543b42b25e0b83.tar.bz2
cpython-760552ceb8c5f5ca4f1bf13f47543b42b25e0b83.zip
bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid container for '… (GH-20964) (GH-21527)
-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 5e0096cae73..0b64dfe47f7 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