Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(199135)

Side by Side Diff: Doc/library/argparse.rst

Issue 16418: argparse with many choices can generate absurdly long usage message
Patch Set: Created 7 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 :mod:`argparse` --- Parser for command-line options, arguments and sub-commands 1 :mod:`argparse` --- Parser for command-line options, arguments and sub-commands
2 =============================================================================== 2 ===============================================================================
3 3
4 .. module:: argparse 4 .. module:: argparse
5 :synopsis: Command-line option and argument parsing library. 5 :synopsis: Command-line option and argument parsing library.
6 .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com> 6 .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com>
7 .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com> 7 .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com>
8 8
9 .. versionadded:: 3.2 9 .. versionadded:: 3.2
10 10
(...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 ... 998 ...
999 >>> parser = argparse.ArgumentParser(prog='PROG') 999 >>> parser = argparse.ArgumentParser(prog='PROG')
1000 >>> parser.add_argument('foo', type=perfect_square) 1000 >>> parser.add_argument('foo', type=perfect_square)
1001 >>> parser.parse_args('9'.split()) 1001 >>> parser.parse_args('9'.split())
1002 Namespace(foo=9) 1002 Namespace(foo=9)
1003 >>> parser.parse_args('7'.split()) 1003 >>> parser.parse_args('7'.split())
1004 usage: PROG [-h] foo 1004 usage: PROG [-h] foo
1005 PROG: error: argument foo: '7' is not a perfect square 1005 PROG: error: argument foo: '7' is not a perfect square
1006 1006
1007 The choices_ keyword argument may be more convenient for type checkers that 1007 The choices_ keyword argument may be more convenient for type checkers that
1008 simply check against a range of values:: 1008 simply check against a small range of values::
1009 1009
1010 >>> parser = argparse.ArgumentParser(prog='PROG') 1010 >>> parser = argparse.ArgumentParser(prog='PROG')
1011 >>> parser.add_argument('foo', type=int, choices=range(5, 10)) 1011 >>> parser.add_argument('foo', type=int, choices=range(5, 10))
1012 >>> parser.parse_args('7'.split()) 1012 >>> parser.parse_args('7'.split())
1013 Namespace(foo=7) 1013 Namespace(foo=7)
1014 >>> parser.parse_args('11'.split()) 1014 >>> parser.parse_args('11'.split())
1015 usage: PROG [-h] {5,6,7,8,9} 1015 usage: PROG [-h] {5,6,7,8,9}
1016 PROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9) 1016 PROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9)
1017 1017
1018 See the choices_ section for more details. 1018 See the choices_ section for more details.
1019 1019
1020 1020
1021 choices 1021 choices
1022 ^^^^^^^ 1022 ^^^^^^^
1023 1023
1024 Some command-line arguments should be selected from a restricted set of values. 1024 Some command-line arguments should be selected from a small set of known
1025 These can be handled by passing a container object as the ``choices`` keyword 1025 values. These can be handled by passing a container object as the ``choices``
ezio.melotti 2012/11/17 18:38:56 Not your fault, but this should be *choices*.
cjerdonek 2012/11/17 18:49:14 I noticed and thought about this when creating the
1026 argument to :meth:`~ArgumentParser.add_argument`. When the command line is 1026 keyword argument to :meth:`~ArgumentParser.add_argument`. When the command
1027 parsed, argument values will be checked, and an error message will be displayed if 1027 line is parsed, if the value for such an argument is not one of the allowed
1028 the argument was not one of the acceptable values:: 1028 choices, an error message will be displayed listing the allowed choices::
1029 1029
1030 >>> parser = argparse.ArgumentParser(prog='PROG') 1030 >>> parser = argparse.ArgumentParser(prog='PROG')
1031 >>> parser.add_argument('foo', choices='abc') 1031 >>> parser.add_argument('foo', choices='abc')
1032 >>> parser.parse_args('c'.split()) 1032 >>> parser.parse_args('c'.split())
1033 Namespace(foo='c') 1033 Namespace(foo='c')
1034 >>> parser.parse_args('X'.split()) 1034 >>> parser.parse_args('X'.split())
1035 usage: PROG [-h] {a,b,c} 1035 usage: PROG [-h] {a,b,c}
1036 PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c') 1036 PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c')
1037 1037
1038 Note that inclusion in the ``choices`` container is checked after any type_ 1038 Note that inclusion in the ``choices`` container is checked after any type_
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
1892 * Replace :class:`optparse.Values` with :class:`Namespace` and 1892 * Replace :class:`optparse.Values` with :class:`Namespace` and
1893 :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with 1893 :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with
1894 :exc:`ArgumentError`. 1894 :exc:`ArgumentError`.
1895 1895
1896 * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with 1896 * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
1897 the standard Python syntax to use dictionaries to format strings, that is, 1897 the standard Python syntax to use dictionaries to format strings, that is,
1898 ``%(default)s`` and ``%(prog)s``. 1898 ``%(default)s`` and ``%(prog)s``.
1899 1899
1900 * Replace the OptionParser constructor ``version`` argument with a call to 1900 * Replace the OptionParser constructor ``version`` argument with a call to
1901 ``parser.add_argument('--version', action='version', version='<the version>')` ` 1901 ``parser.add_argument('--version', action='version', version='<the version>')` `
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+