Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
92e2fc83
Commit
92e2fc83
authored
Jan 11, 2013
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #16933: Improve choices examples in argparse documentation.
parent
dfae912d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
23 deletions
+24
-23
Doc/library/argparse.rst
Doc/library/argparse.rst
+24
-23
No files found.
Doc/library/argparse.rst
View file @
92e2fc83
...
@@ -1005,32 +1005,33 @@ choices
...
@@ -1005,32 +1005,33 @@ choices
^^^^^^^
^^^^^^^
Some command-line arguments should be selected from a restricted set of values.
Some command-line arguments should be selected from a restricted set of values.
These can be handled by passing a container object as the
``choices``
keyword
These can be handled by passing a container object as the
*choices*
keyword
argument to :meth:`~ArgumentParser.add_argument`. When the command line is
argument to :meth:`~ArgumentParser.add_argument`. When the command line is
parsed, argument values will be checked, and an error message will be displayed if
parsed, argument values will be checked, and an error message will be displayed
the argument was not one of the acceptable values::
if the argument was not one of the acceptable values::
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('foo', choices='abc')
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
>>> parser.parse_args('c'.split())
>>> parser.parse_args(['rock'])
Namespace(foo='c')
Namespace(move='rock')
>>> parser.parse_args('X'.split())
>>> parser.parse_args(['fire'])
usage: PROG [-h] {a,b,c}
usage: game.py [-h] {rock,paper,scissors}
PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c')
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')
Note that inclusion in the ``choices`` container is checked after any type_
conversions have been performed, so the type of the objects in the ``choices``
Note that inclusion in the *choices* container is checked after any type_
conversions have been performed, so the type of the objects in the *choices*
container should match the type_ specified::
container should match the type_ specified::
>>> parser = argparse.ArgumentParser(prog='
PROG
')
>>> parser = argparse.ArgumentParser(prog='
doors.py
')
>>> parser.add_argument('
foo', type=complex, choices=[1, 1j]
)
>>> parser.add_argument('
door', type=int, choices=range(1, 4)
)
>>> p
arser.parse_args('1j'.split(
))
>>> p
rint(parser.parse_args(['3']
))
Namespace(
foo=1j
)
Namespace(
door=3
)
>>> parser.parse_args(
'-- -4'.split()
)
>>> parser.parse_args(
['4']
)
usage:
PROG [-h] {1,1j
}
usage:
doors.py [-h] {1,2,3
}
PROG: error: argument foo: invalid choice: (-4+0j) (choose from 1, 1j
)
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3
)
Any object that supports the ``in`` operator can be passed as the
``choices``
Any object that supports the ``in`` operator can be passed as the
*choices*
value, so :class:`dict` objects, :class:`set` objects, custom containers,
value, so :class:`dict` objects, :class:`set` objects, custom containers,
etc. are all supported.
etc. are all supported.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment