Skip to content

Commit 71e39fb

Browse files
committed
Issue python#16933 (merge from 3.2): Improve choices examples in argparse docs.
2 parents ed6b4c0 + 174ef67 commit 71e39fb

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Doc/library/argparse.rst

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,32 +1022,33 @@ choices
10221022
^^^^^^^
10231023

10241024
Some command-line arguments should be selected from a restricted set of values.
1025-
These can be handled by passing a container object as the ``choices`` keyword
1025+
These can be handled by passing a container object as the *choices* keyword
10261026
argument to :meth:`~ArgumentParser.add_argument`. When the command line is
1027-
parsed, argument values will be checked, and an error message will be displayed if
1028-
the argument was not one of the acceptable values::
1029-
1030-
>>> parser = argparse.ArgumentParser(prog='PROG')
1031-
>>> parser.add_argument('foo', choices='abc')
1032-
>>> parser.parse_args('c'.split())
1033-
Namespace(foo='c')
1034-
>>> parser.parse_args('X'.split())
1035-
usage: PROG [-h] {a,b,c}
1036-
PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c')
1037-
1038-
Note that inclusion in the ``choices`` container is checked after any type_
1039-
conversions have been performed, so the type of the objects in the ``choices``
1027+
parsed, argument values will be checked, and an error message will be displayed
1028+
if the argument was not one of the acceptable values::
1029+
1030+
>>> parser = argparse.ArgumentParser(prog='game.py')
1031+
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
1032+
>>> parser.parse_args(['rock'])
1033+
Namespace(move='rock')
1034+
>>> parser.parse_args(['fire'])
1035+
usage: game.py [-h] {rock,paper,scissors}
1036+
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
1037+
'paper', 'scissors')
1038+
1039+
Note that inclusion in the *choices* container is checked after any type_
1040+
conversions have been performed, so the type of the objects in the *choices*
10401041
container should match the type_ specified::
10411042

1042-
>>> parser = argparse.ArgumentParser(prog='PROG')
1043-
>>> parser.add_argument('foo', type=complex, choices=[1, 1j])
1044-
>>> parser.parse_args('1j'.split())
1045-
Namespace(foo=1j)
1046-
>>> parser.parse_args('-- -4'.split())
1047-
usage: PROG [-h] {1,1j}
1048-
PROG: error: argument foo: invalid choice: (-4+0j) (choose from 1, 1j)
1049-
1050-
Any object that supports the ``in`` operator can be passed as the ``choices``
1043+
>>> parser = argparse.ArgumentParser(prog='doors.py')
1044+
>>> parser.add_argument('door', type=int, choices=range(1, 4))
1045+
>>> print(parser.parse_args(['3']))
1046+
Namespace(door=3)
1047+
>>> parser.parse_args(['4'])
1048+
usage: doors.py [-h] {1,2,3}
1049+
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)
1050+
1051+
Any object that supports the ``in`` operator can be passed as the *choices*
10511052
value, so :class:`dict` objects, :class:`set` objects, custom containers,
10521053
etc. are all supported.
10531054

0 commit comments

Comments
 (0)