Skip to content

Commit

Permalink
bpo-42501: Revise the usage note for Enums with the choices (pythonGH…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger authored Nov 30, 2020
1 parent e41bfd1 commit 7f82f22
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1133,20 +1133,9 @@ 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'>)

Use of :class:`enum.Enum` is not recommended because it is difficult to
control its appearance in usage, help, and error messages.


required
Expand Down

0 comments on commit 7f82f22

Please sign in to comment.