From 344c2a75c1c13de781962a3f80552e66a4c89024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20F=C3=A9rotin?= Date: Sat, 20 Jun 2020 14:55:05 +0200 Subject: =?UTF-8?q?bpo-41024:=20doc:=20Explicitly=20mention=20use=20of=20'?= =?UTF-8?q?enum.Enum'=20as=20a=20valid=20container=20for=20'=E2=80=A6=20(G?= =?UTF-8?q?H-20964)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …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 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 5e0096c..0b64dfe 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=) required -- cgit v0.12