[Python-checkins] (no subject)

Vincent Férotin webhook-mailer at python.org
Sat Jun 20 08:55:10 EDT 2020




To: python-checkins at python.org
Subject: bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid
 container for =?utf-8?q?=27=E2=80=A6?= (GH-20964)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/344c2a75c1c13de781962a3f80552e66a4c8=
9024
commit: 344c2a75c1c13de781962a3f80552e66a4c89024
branch: master
author: Vincent F=C3=A9rotin <vincent.ferotin at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-06-20T05:55:05-07:00
summary:

bpo-41024: doc: Explicitly mention use of 'enum.Enum' as a valid container fo=
r '=E2=80=A6 (GH-20964)



=E2=80=A6choices' argument of 'argparse.ArgumentParser.add_argument'.

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

Automerge-Triggered-By: @csabella

files:
M Doc/library/argparse.rst

diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 5e0096cae73a7..0b64dfe47f768 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -1133,6 +1133,20 @@ container should match the type_ specified::
=20
 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 =3D 'rock'
+   ...     PAPER =3D 'paper'
+   ...     SCISSORS =3D 'scissors'
+   ...
+   >>> parser =3D argparse.ArgumentParser(prog=3D'game.py')
+   >>> parser.add_argument('move', type=3DGameMove, choices=3DGameMove)
+   >>> parser.parse_args(['rock'])
+   Namespace(move=3D<GameMove.ROCK: 'rock'>)
=20
=20
 required



More information about the Python-checkins mailing list