Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1 | :mod:`argparse` --- Parser for command line options, arguments and sub-commands |
| 2 | =============================================================================== |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: argparse |
| 5 | :synopsis: Command-line option and argument parsing library. |
| 6 | .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com> |
| 7 | .. versionadded:: 2.7 |
| 8 | .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com> |
| 9 | |
| 10 | |
| 11 | The :mod:`argparse` module makes it easy to write user friendly command line |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 12 | interfaces. The program defines what arguments it requires, and :mod:`argparse` |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 13 | will figure out how to parse those out of :data:`sys.argv`. The :mod:`argparse` |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 14 | module also automatically generates help and usage messages and issues errors |
| 15 | when users give the program invalid arguments. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 16 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 17 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 18 | Example |
| 19 | ------- |
| 20 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 21 | The following code is a Python program that takes a list of integers and |
| 22 | produces either the sum or the max:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 23 | |
| 24 | import argparse |
| 25 | |
| 26 | parser = argparse.ArgumentParser(description='Process some integers.') |
| 27 | parser.add_argument('integers', metavar='N', type=int, nargs='+', |
| 28 | help='an integer for the accumulator') |
| 29 | parser.add_argument('--sum', dest='accumulate', action='store_const', |
| 30 | const=sum, default=max, |
| 31 | help='sum the integers (default: find the max)') |
| 32 | |
| 33 | args = parser.parse_args() |
| 34 | print args.accumulate(args.integers) |
| 35 | |
| 36 | Assuming the Python code above is saved into a file called ``prog.py``, it can |
| 37 | be run at the command line and provides useful help messages:: |
| 38 | |
| 39 | $ prog.py -h |
| 40 | usage: prog.py [-h] [--sum] N [N ...] |
| 41 | |
| 42 | Process some integers. |
| 43 | |
| 44 | positional arguments: |
| 45 | N an integer for the accumulator |
| 46 | |
| 47 | optional arguments: |
| 48 | -h, --help show this help message and exit |
| 49 | --sum sum the integers (default: find the max) |
| 50 | |
| 51 | When run with the appropriate arguments, it prints either the sum or the max of |
| 52 | the command-line integers:: |
| 53 | |
| 54 | $ prog.py 1 2 3 4 |
| 55 | 4 |
| 56 | |
| 57 | $ prog.py 1 2 3 4 --sum |
| 58 | 10 |
| 59 | |
| 60 | If invalid arguments are passed in, it will issue an error:: |
| 61 | |
| 62 | $ prog.py a b c |
| 63 | usage: prog.py [-h] [--sum] N [N ...] |
| 64 | prog.py: error: argument N: invalid int value: 'a' |
| 65 | |
| 66 | The following sections walk you through this example. |
| 67 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 68 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 69 | Creating a parser |
| 70 | ^^^^^^^^^^^^^^^^^ |
| 71 | |
Benjamin Peterson | ac80c15 | 2010-03-03 21:28:25 +0000 | [diff] [blame] | 72 | The first step in using the :mod:`argparse` is creating an |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 73 | :class:`ArgumentParser` object:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 74 | |
| 75 | >>> parser = argparse.ArgumentParser(description='Process some integers.') |
| 76 | |
| 77 | The :class:`ArgumentParser` object will hold all the information necessary to |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 78 | parse the command line into python data types. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | Adding arguments |
| 82 | ^^^^^^^^^^^^^^^^ |
| 83 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 84 | Filling an :class:`ArgumentParser` with information about program arguments is |
| 85 | done by making calls to the :meth:`~ArgumentParser.add_argument` method. |
| 86 | Generally, these calls tell the :class:`ArgumentParser` how to take the strings |
| 87 | on the command line and turn them into objects. This information is stored and |
| 88 | used when :meth:`~ArgumentParser.parse_args` is called. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 89 | |
| 90 | >>> parser.add_argument('integers', metavar='N', type=int, nargs='+', |
| 91 | ... help='an integer for the accumulator') |
| 92 | >>> parser.add_argument('--sum', dest='accumulate', action='store_const', |
| 93 | ... const=sum, default=max, |
| 94 | ... help='sum the integers (default: find the max)') |
| 95 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 96 | Later, calling :meth:`parse_args` will return an object with |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 97 | two attributes, ``integers`` and ``accumulate``. The ``integers`` attribute |
| 98 | will be a list of one or more ints, and the ``accumulate`` attribute will be |
| 99 | either the :func:`sum` function, if ``--sum`` was specified at the command line, |
| 100 | or the :func:`max` function if it was not. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 101 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 102 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 103 | Parsing arguments |
| 104 | ^^^^^^^^^^^^^^^^^ |
| 105 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 106 | :class:`ArgumentParser` parses args through the |
| 107 | :meth:`~ArgumentParser.parse_args` method. This will inspect the command-line, |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 108 | convert each arg to the appropriate type and then invoke the appropriate action. |
| 109 | In most cases, this means a simple namespace object will be built up from |
| 110 | attributes parsed out of the command-line:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 111 | |
| 112 | >>> parser.parse_args(['--sum', '7', '-1', '42']) |
| 113 | Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42]) |
| 114 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 115 | In a script, :meth:`~ArgumentParser.parse_args` will typically be called with no |
| 116 | arguments, and the :class:`ArgumentParser` will automatically determine the |
| 117 | command-line args from :data:`sys.argv`. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | ArgumentParser objects |
| 121 | ---------------------- |
| 122 | |
| 123 | .. class:: ArgumentParser([description], [epilog], [prog], [usage], [add_help], [argument_default], [parents], [prefix_chars], [conflict_handler], [formatter_class]) |
| 124 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 125 | Create a new :class:`ArgumentParser` object. Each parameter has its own more |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 126 | detailed description below, but in short they are: |
| 127 | |
| 128 | * description_ - Text to display before the argument help. |
| 129 | |
| 130 | * epilog_ - Text to display after the argument help. |
| 131 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 132 | * add_help_ - Add a -h/--help option to the parser. (default: ``True``) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 133 | |
| 134 | * argument_default_ - Set the global default value for arguments. |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 135 | (default: ``None``) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 137 | * parents_ - A list of :class:`ArgumentParser` objects whose arguments should |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 138 | also be included. |
| 139 | |
| 140 | * prefix_chars_ - The set of characters that prefix optional arguments. |
| 141 | (default: '-') |
| 142 | |
| 143 | * fromfile_prefix_chars_ - The set of characters that prefix files from |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 144 | which additional arguments should be read. (default: ``None``) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 145 | |
| 146 | * formatter_class_ - A class for customizing the help output. |
| 147 | |
| 148 | * conflict_handler_ - Usually unnecessary, defines strategy for resolving |
| 149 | conflicting optionals. |
| 150 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 151 | * prog_ - The name of the program (default: |
| 152 | :data:`sys.argv[0]`) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 153 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 154 | * usage_ - The string describing the program usage (default: generated) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 155 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 156 | The following sections describe how each of these are used. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | description |
| 160 | ^^^^^^^^^^^ |
| 161 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 162 | Most calls to the :class:`ArgumentParser` constructor will use the |
| 163 | ``description=`` keyword argument. This argument gives a brief description of |
| 164 | what the program does and how it works. In help messages, the description is |
| 165 | displayed between the command-line usage string and the help messages for the |
| 166 | various arguments:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 167 | |
| 168 | >>> parser = argparse.ArgumentParser(description='A foo that bars') |
| 169 | >>> parser.print_help() |
| 170 | usage: argparse.py [-h] |
| 171 | |
| 172 | A foo that bars |
| 173 | |
| 174 | optional arguments: |
| 175 | -h, --help show this help message and exit |
| 176 | |
| 177 | By default, the description will be line-wrapped so that it fits within the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 178 | given space. To change this behavior, see the formatter_class_ argument. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | epilog |
| 182 | ^^^^^^ |
| 183 | |
| 184 | Some programs like to display additional description of the program after the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 185 | description of the arguments. Such text can be specified using the ``epilog=`` |
| 186 | argument to :class:`ArgumentParser`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 187 | |
| 188 | >>> parser = argparse.ArgumentParser( |
| 189 | ... description='A foo that bars', |
| 190 | ... epilog="And that's how you'd foo a bar") |
| 191 | >>> parser.print_help() |
| 192 | usage: argparse.py [-h] |
| 193 | |
| 194 | A foo that bars |
| 195 | |
| 196 | optional arguments: |
| 197 | -h, --help show this help message and exit |
| 198 | |
| 199 | And that's how you'd foo a bar |
| 200 | |
| 201 | As with the description_ argument, the ``epilog=`` text is by default |
| 202 | line-wrapped, but this behavior can be adjusted with the formatter_class_ |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 203 | argument to :class:`ArgumentParser`. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | add_help |
| 207 | ^^^^^^^^ |
| 208 | |
R. David Murray | 1cbf78e | 2010-08-03 18:14:01 +0000 | [diff] [blame] | 209 | By default, ArgumentParser objects add an option which simply displays |
| 210 | the parser's help message. For example, consider a file named |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 211 | ``myprogram.py`` containing the following code:: |
| 212 | |
| 213 | import argparse |
| 214 | parser = argparse.ArgumentParser() |
| 215 | parser.add_argument('--foo', help='foo help') |
| 216 | args = parser.parse_args() |
| 217 | |
| 218 | If ``-h`` or ``--help`` is supplied is at the command-line, the ArgumentParser |
| 219 | help will be printed:: |
| 220 | |
| 221 | $ python myprogram.py --help |
| 222 | usage: myprogram.py [-h] [--foo FOO] |
| 223 | |
| 224 | optional arguments: |
| 225 | -h, --help show this help message and exit |
| 226 | --foo FOO foo help |
| 227 | |
| 228 | Occasionally, it may be useful to disable the addition of this help option. |
| 229 | This can be achieved by passing ``False`` as the ``add_help=`` argument to |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 230 | :class:`ArgumentParser`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 231 | |
| 232 | >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False) |
| 233 | >>> parser.add_argument('--foo', help='foo help') |
| 234 | >>> parser.print_help() |
| 235 | usage: PROG [--foo FOO] |
| 236 | |
| 237 | optional arguments: |
| 238 | --foo FOO foo help |
| 239 | |
R. David Murray | 1cbf78e | 2010-08-03 18:14:01 +0000 | [diff] [blame] | 240 | The help option is typically ``-h/--help``. The exception to this is |
| 241 | if the ``prefix_chars=`` is specified and does not include ``'-'``, in |
| 242 | which case ``-h`` and ``--help`` are not valid options. In |
| 243 | this case, the first character in ``prefix_chars`` is used to prefix |
| 244 | the help options:: |
| 245 | |
| 246 | >>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='+/') |
| 247 | >>> parser.print_help() |
| 248 | usage: PROG [+h] |
| 249 | |
| 250 | optional arguments: |
| 251 | +h, ++help show this help message and exit |
| 252 | |
| 253 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 254 | prefix_chars |
| 255 | ^^^^^^^^^^^^ |
| 256 | |
| 257 | Most command-line options will use ``'-'`` as the prefix, e.g. ``-f/--foo``. |
R. David Murray | 1cbf78e | 2010-08-03 18:14:01 +0000 | [diff] [blame] | 258 | Parsers that need to support different or additional prefix |
| 259 | characters, e.g. for options |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 260 | like ``+f`` or ``/foo``, may specify them using the ``prefix_chars=`` argument |
| 261 | to the ArgumentParser constructor:: |
| 262 | |
| 263 | >>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+') |
| 264 | >>> parser.add_argument('+f') |
| 265 | >>> parser.add_argument('++bar') |
| 266 | >>> parser.parse_args('+f X ++bar Y'.split()) |
| 267 | Namespace(bar='Y', f='X') |
| 268 | |
| 269 | The ``prefix_chars=`` argument defaults to ``'-'``. Supplying a set of |
| 270 | characters that does not include ``'-'`` will cause ``-f/--foo`` options to be |
| 271 | disallowed. |
| 272 | |
| 273 | |
| 274 | fromfile_prefix_chars |
| 275 | ^^^^^^^^^^^^^^^^^^^^^ |
| 276 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 277 | Sometimes, for example when dealing with a particularly long argument lists, it |
| 278 | may make sense to keep the list of arguments in a file rather than typing it out |
| 279 | at the command line. If the ``fromfile_prefix_chars=`` argument is given to the |
| 280 | :class:`ArgumentParser` constructor, then arguments that start with any of the |
| 281 | specified characters will be treated as files, and will be replaced by the |
| 282 | arguments they contain. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 283 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 284 | >>> with open('args.txt', 'w') as fp: |
| 285 | ... fp.write('-f\nbar') |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 286 | >>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
| 287 | >>> parser.add_argument('-f') |
| 288 | >>> parser.parse_args(['-f', 'foo', '@args.txt']) |
| 289 | Namespace(f='bar') |
| 290 | |
| 291 | Arguments read from a file must by default be one per line (but see also |
| 292 | :meth:`convert_arg_line_to_args`) and are treated as if they were in the same |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 293 | place as the original file referencing argument on the command line. So in the |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 294 | example above, the expression ``['-f', 'foo', '@args.txt']`` is considered |
| 295 | equivalent to the expression ``['-f', 'foo', '-f', 'bar']``. |
| 296 | |
| 297 | The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that |
| 298 | arguments will never be treated as file references. |
| 299 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 300 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 301 | argument_default |
| 302 | ^^^^^^^^^^^^^^^^ |
| 303 | |
| 304 | Generally, argument defaults are specified either by passing a default to |
| 305 | :meth:`add_argument` or by calling the :meth:`set_defaults` methods with a |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 306 | specific set of name-value pairs. Sometimes however, it may be useful to |
| 307 | specify a single parser-wide default for arguments. This can be accomplished by |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 308 | passing the ``argument_default=`` keyword argument to :class:`ArgumentParser`. |
| 309 | For example, to globally suppress attribute creation on :meth:`parse_args` |
| 310 | calls, we supply ``argument_default=SUPPRESS``:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 311 | |
| 312 | >>> parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) |
| 313 | >>> parser.add_argument('--foo') |
| 314 | >>> parser.add_argument('bar', nargs='?') |
| 315 | >>> parser.parse_args(['--foo', '1', 'BAR']) |
| 316 | Namespace(bar='BAR', foo='1') |
| 317 | >>> parser.parse_args([]) |
| 318 | Namespace() |
| 319 | |
| 320 | |
| 321 | parents |
| 322 | ^^^^^^^ |
| 323 | |
| 324 | Sometimes, several parsers share a common set of arguments. Rather than |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 325 | repeating the definitions of these arguments, a single parser with all the |
| 326 | shared arguments and passed to ``parents=`` argument to :class:`ArgumentParser` |
| 327 | can be used. The ``parents=`` argument takes a list of :class:`ArgumentParser` |
| 328 | objects, collects all the positional and optional actions from them, and adds |
| 329 | these actions to the :class:`ArgumentParser` object being constructed:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 330 | |
| 331 | >>> parent_parser = argparse.ArgumentParser(add_help=False) |
| 332 | >>> parent_parser.add_argument('--parent', type=int) |
| 333 | |
| 334 | >>> foo_parser = argparse.ArgumentParser(parents=[parent_parser]) |
| 335 | >>> foo_parser.add_argument('foo') |
| 336 | >>> foo_parser.parse_args(['--parent', '2', 'XXX']) |
| 337 | Namespace(foo='XXX', parent=2) |
| 338 | |
| 339 | >>> bar_parser = argparse.ArgumentParser(parents=[parent_parser]) |
| 340 | >>> bar_parser.add_argument('--bar') |
| 341 | >>> bar_parser.parse_args(['--bar', 'YYY']) |
| 342 | Namespace(bar='YYY', parent=None) |
| 343 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 344 | Note that most parent parsers will specify ``add_help=False``. Otherwise, the |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 345 | :class:`ArgumentParser` will see two ``-h/--help`` options (one in the parent |
| 346 | and one in the child) and raise an error. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 347 | |
| 348 | |
| 349 | formatter_class |
| 350 | ^^^^^^^^^^^^^^^ |
| 351 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 352 | :class:`ArgumentParser` objects allow the help formatting to be customized by |
| 353 | specifying an alternate formatting class. Currently, there are three such |
| 354 | classes: :class:`argparse.RawDescriptionHelpFormatter`, |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 355 | :class:`argparse.RawTextHelpFormatter` and |
| 356 | :class:`argparse.ArgumentDefaultsHelpFormatter`. The first two allow more |
| 357 | control over how textual descriptions are displayed, while the last |
| 358 | automatically adds information about argument default values. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 359 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 360 | By default, :class:`ArgumentParser` objects line-wrap the description_ and |
| 361 | epilog_ texts in command-line help messages:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 362 | |
| 363 | >>> parser = argparse.ArgumentParser( |
| 364 | ... prog='PROG', |
| 365 | ... description='''this description |
| 366 | ... was indented weird |
| 367 | ... but that is okay''', |
| 368 | ... epilog=''' |
| 369 | ... likewise for this epilog whose whitespace will |
| 370 | ... be cleaned up and whose words will be wrapped |
| 371 | ... across a couple lines''') |
| 372 | >>> parser.print_help() |
| 373 | usage: PROG [-h] |
| 374 | |
| 375 | this description was indented weird but that is okay |
| 376 | |
| 377 | optional arguments: |
| 378 | -h, --help show this help message and exit |
| 379 | |
| 380 | likewise for this epilog whose whitespace will be cleaned up and whose words |
| 381 | will be wrapped across a couple lines |
| 382 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 383 | Passing :class:`argparse.RawDescriptionHelpFormatter` as ``formatter_class=`` |
Benjamin Peterson | c516d19 | 2010-03-03 02:04:24 +0000 | [diff] [blame] | 384 | indicates that description_ and epilog_ are already correctly formatted and |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 385 | should not be line-wrapped:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 386 | |
| 387 | >>> parser = argparse.ArgumentParser( |
| 388 | ... prog='PROG', |
| 389 | ... formatter_class=argparse.RawDescriptionHelpFormatter, |
| 390 | ... description=textwrap.dedent('''\ |
| 391 | ... Please do not mess up this text! |
| 392 | ... -------------------------------- |
| 393 | ... I have indented it |
| 394 | ... exactly the way |
| 395 | ... I want it |
| 396 | ... ''')) |
| 397 | >>> parser.print_help() |
| 398 | usage: PROG [-h] |
| 399 | |
| 400 | Please do not mess up this text! |
| 401 | -------------------------------- |
| 402 | I have indented it |
| 403 | exactly the way |
| 404 | I want it |
| 405 | |
| 406 | optional arguments: |
| 407 | -h, --help show this help message and exit |
| 408 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 409 | :class:`RawTextHelpFormatter` maintains whitespace for all sorts of help text |
| 410 | including argument descriptions. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 411 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 412 | The other formatter class available, :class:`ArgumentDefaultsHelpFormatter`, |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 413 | will add information about the default value of each of the arguments:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 414 | |
| 415 | >>> parser = argparse.ArgumentParser( |
| 416 | ... prog='PROG', |
| 417 | ... formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 418 | >>> parser.add_argument('--foo', type=int, default=42, help='FOO!') |
| 419 | >>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!') |
| 420 | >>> parser.print_help() |
| 421 | usage: PROG [-h] [--foo FOO] [bar [bar ...]] |
| 422 | |
| 423 | positional arguments: |
| 424 | bar BAR! (default: [1, 2, 3]) |
| 425 | |
| 426 | optional arguments: |
| 427 | -h, --help show this help message and exit |
| 428 | --foo FOO FOO! (default: 42) |
| 429 | |
| 430 | |
| 431 | conflict_handler |
| 432 | ^^^^^^^^^^^^^^^^ |
| 433 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 434 | :class:`ArgumentParser` objects do not allow two actions with the same option |
| 435 | string. By default, :class:`ArgumentParser` objects raises an exception if an |
| 436 | attempt is made to create an argument with an option string that is already in |
| 437 | use:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 438 | |
| 439 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 440 | >>> parser.add_argument('-f', '--foo', help='old foo help') |
| 441 | >>> parser.add_argument('--foo', help='new foo help') |
| 442 | Traceback (most recent call last): |
| 443 | .. |
| 444 | ArgumentError: argument --foo: conflicting option string(s): --foo |
| 445 | |
| 446 | Sometimes (e.g. when using parents_) it may be useful to simply override any |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 447 | older arguments with the same option string. To get this behavior, the value |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 448 | ``'resolve'`` can be supplied to the ``conflict_handler=`` argument of |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 449 | :class:`ArgumentParser`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 450 | |
| 451 | >>> parser = argparse.ArgumentParser(prog='PROG', conflict_handler='resolve') |
| 452 | >>> parser.add_argument('-f', '--foo', help='old foo help') |
| 453 | >>> parser.add_argument('--foo', help='new foo help') |
| 454 | >>> parser.print_help() |
| 455 | usage: PROG [-h] [-f FOO] [--foo FOO] |
| 456 | |
| 457 | optional arguments: |
| 458 | -h, --help show this help message and exit |
| 459 | -f FOO old foo help |
| 460 | --foo FOO new foo help |
| 461 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 462 | Note that :class:`ArgumentParser` objects only remove an action if all of its |
| 463 | option strings are overridden. So, in the example above, the old ``-f/--foo`` |
| 464 | action is retained as the ``-f`` action, because only the ``--foo`` option |
| 465 | string was overridden. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 466 | |
| 467 | |
| 468 | prog |
| 469 | ^^^^ |
| 470 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 471 | By default, :class:`ArgumentParser` objects uses ``sys.argv[0]`` to determine |
| 472 | how to display the name of the program in help messages. This default is almost |
Ezio Melotti | 019551f | 2010-05-19 00:32:52 +0000 | [diff] [blame] | 473 | always desirable because it will make the help messages match how the program was |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 474 | invoked on the command line. For example, consider a file named |
| 475 | ``myprogram.py`` with the following code:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 476 | |
| 477 | import argparse |
| 478 | parser = argparse.ArgumentParser() |
| 479 | parser.add_argument('--foo', help='foo help') |
| 480 | args = parser.parse_args() |
| 481 | |
| 482 | The help for this program will display ``myprogram.py`` as the program name |
| 483 | (regardless of where the program was invoked from):: |
| 484 | |
| 485 | $ python myprogram.py --help |
| 486 | usage: myprogram.py [-h] [--foo FOO] |
| 487 | |
| 488 | optional arguments: |
| 489 | -h, --help show this help message and exit |
| 490 | --foo FOO foo help |
| 491 | $ cd .. |
| 492 | $ python subdir\myprogram.py --help |
| 493 | usage: myprogram.py [-h] [--foo FOO] |
| 494 | |
| 495 | optional arguments: |
| 496 | -h, --help show this help message and exit |
| 497 | --foo FOO foo help |
| 498 | |
| 499 | To change this default behavior, another value can be supplied using the |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 500 | ``prog=`` argument to :class:`ArgumentParser`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 501 | |
| 502 | >>> parser = argparse.ArgumentParser(prog='myprogram') |
| 503 | >>> parser.print_help() |
| 504 | usage: myprogram [-h] |
| 505 | |
| 506 | optional arguments: |
| 507 | -h, --help show this help message and exit |
| 508 | |
| 509 | Note that the program name, whether determined from ``sys.argv[0]`` or from the |
| 510 | ``prog=`` argument, is available to help messages using the ``%(prog)s`` format |
| 511 | specifier. |
| 512 | |
| 513 | :: |
| 514 | |
| 515 | >>> parser = argparse.ArgumentParser(prog='myprogram') |
| 516 | >>> parser.add_argument('--foo', help='foo of the %(prog)s program') |
| 517 | >>> parser.print_help() |
| 518 | usage: myprogram [-h] [--foo FOO] |
| 519 | |
| 520 | optional arguments: |
| 521 | -h, --help show this help message and exit |
| 522 | --foo FOO foo of the myprogram program |
| 523 | |
| 524 | |
| 525 | usage |
| 526 | ^^^^^ |
| 527 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 528 | By default, :class:`ArgumentParser` calculates the usage message from the |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 529 | arguments it contains:: |
| 530 | |
| 531 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 532 | >>> parser.add_argument('--foo', nargs='?', help='foo help') |
| 533 | >>> parser.add_argument('bar', nargs='+', help='bar help') |
| 534 | >>> parser.print_help() |
| 535 | usage: PROG [-h] [--foo [FOO]] bar [bar ...] |
| 536 | |
| 537 | positional arguments: |
| 538 | bar bar help |
| 539 | |
| 540 | optional arguments: |
| 541 | -h, --help show this help message and exit |
| 542 | --foo [FOO] foo help |
| 543 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 544 | The default message can be overridden with the ``usage=`` keyword argument:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 545 | |
| 546 | >>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]') |
| 547 | >>> parser.add_argument('--foo', nargs='?', help='foo help') |
| 548 | >>> parser.add_argument('bar', nargs='+', help='bar help') |
| 549 | >>> parser.print_help() |
| 550 | usage: PROG [options] |
| 551 | |
| 552 | positional arguments: |
| 553 | bar bar help |
| 554 | |
| 555 | optional arguments: |
| 556 | -h, --help show this help message and exit |
| 557 | --foo [FOO] foo help |
| 558 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 559 | The ``%(prog)s`` format specifier is available to fill in the program name in |
| 560 | your usage messages. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 561 | |
| 562 | |
| 563 | The add_argument() method |
| 564 | ------------------------- |
| 565 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 566 | .. method:: ArgumentParser.add_argument(name or flags..., [action], [nargs], [const], [default], [type], [choices], [required], [help], [metavar], [dest]) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 567 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 568 | Define how a single command line argument should be parsed. Each parameter |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 569 | has its own more detailed description below, but in short they are: |
| 570 | |
| 571 | * `name or flags`_ - Either a name or a list of option strings, e.g. ``foo`` |
| 572 | or ``-f, --foo`` |
| 573 | |
| 574 | * action_ - The basic type of action to be taken when this argument is |
| 575 | encountered at the command-line. |
| 576 | |
| 577 | * nargs_ - The number of command-line arguments that should be consumed. |
| 578 | |
| 579 | * const_ - A constant value required by some action_ and nargs_ selections. |
| 580 | |
| 581 | * default_ - The value produced if the argument is absent from the |
| 582 | command-line. |
| 583 | |
| 584 | * type_ - The type to which the command-line arg should be converted. |
| 585 | |
| 586 | * choices_ - A container of the allowable values for the argument. |
| 587 | |
| 588 | * required_ - Whether or not the command-line option may be omitted |
| 589 | (optionals only). |
| 590 | |
| 591 | * help_ - A brief description of what the argument does. |
| 592 | |
| 593 | * metavar_ - A name for the argument in usage messages. |
| 594 | |
| 595 | * dest_ - The name of the attribute to be added to the object returned by |
| 596 | :meth:`parse_args`. |
| 597 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 598 | The following sections describe how each of these are used. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 599 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 600 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 601 | name or flags |
| 602 | ^^^^^^^^^^^^^ |
| 603 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 604 | The :meth:`add_argument` method must know whether an optional argument, like |
| 605 | ``-f`` or ``--foo``, or a positional argument, like a list of filenames, is |
| 606 | expected. The first arguments passed to :meth:`add_argument` must therefore be |
| 607 | either a series of flags, or a simple argument name. For example, an optional |
| 608 | argument could be created like:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 609 | |
| 610 | >>> parser.add_argument('-f', '--foo') |
| 611 | |
| 612 | while a positional argument could be created like:: |
| 613 | |
| 614 | >>> parser.add_argument('bar') |
| 615 | |
| 616 | When :meth:`parse_args` is called, optional arguments will be identified by the |
| 617 | ``-`` prefix, and the remaining arguments will be assumed to be positional:: |
| 618 | |
| 619 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 620 | >>> parser.add_argument('-f', '--foo') |
| 621 | >>> parser.add_argument('bar') |
| 622 | >>> parser.parse_args(['BAR']) |
| 623 | Namespace(bar='BAR', foo=None) |
| 624 | >>> parser.parse_args(['BAR', '--foo', 'FOO']) |
| 625 | Namespace(bar='BAR', foo='FOO') |
| 626 | >>> parser.parse_args(['--foo', 'FOO']) |
| 627 | usage: PROG [-h] [-f FOO] bar |
| 628 | PROG: error: too few arguments |
| 629 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 630 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 631 | action |
| 632 | ^^^^^^ |
| 633 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 634 | :class:`ArgumentParser` objects associate command-line args with actions. These |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 635 | actions can do just about anything with the command-line args associated with |
| 636 | them, though most actions simply add an attribute to the object returned by |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 637 | :meth:`parse_args`. The ``action`` keyword argument specifies how the |
| 638 | command-line args should be handled. The supported actions are: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 639 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 640 | * ``'store'`` - This just stores the argument's value. This is the default |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 641 | action. For example:: |
| 642 | |
| 643 | >>> parser = argparse.ArgumentParser() |
| 644 | >>> parser.add_argument('--foo') |
| 645 | >>> parser.parse_args('--foo 1'.split()) |
| 646 | Namespace(foo='1') |
| 647 | |
| 648 | * ``'store_const'`` - This stores the value specified by the const_ keyword |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 649 | argument. (Note that the const_ keyword argument defaults to the rather |
| 650 | unhelpful ``None``.) The ``'store_const'`` action is most commonly used with |
| 651 | optional arguments that specify some sort of flag. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 652 | |
| 653 | >>> parser = argparse.ArgumentParser() |
| 654 | >>> parser.add_argument('--foo', action='store_const', const=42) |
| 655 | >>> parser.parse_args('--foo'.split()) |
| 656 | Namespace(foo=42) |
| 657 | |
| 658 | * ``'store_true'`` and ``'store_false'`` - These store the values ``True`` and |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 659 | ``False`` respectively. These are special cases of ``'store_const'``. For |
| 660 | example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 661 | |
| 662 | >>> parser = argparse.ArgumentParser() |
| 663 | >>> parser.add_argument('--foo', action='store_true') |
| 664 | >>> parser.add_argument('--bar', action='store_false') |
| 665 | >>> parser.parse_args('--foo --bar'.split()) |
| 666 | Namespace(bar=False, foo=True) |
| 667 | |
| 668 | * ``'append'`` - This stores a list, and appends each argument value to the |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 669 | list. This is useful to allow an option to be specified multiple times. |
| 670 | Example usage:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 671 | |
| 672 | >>> parser = argparse.ArgumentParser() |
| 673 | >>> parser.add_argument('--foo', action='append') |
| 674 | >>> parser.parse_args('--foo 1 --foo 2'.split()) |
| 675 | Namespace(foo=['1', '2']) |
| 676 | |
| 677 | * ``'append_const'`` - This stores a list, and appends the value specified by |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 678 | the const_ keyword argument to the list. (Note that the const_ keyword |
| 679 | argument defaults to ``None``.) The ``'append_const'`` action is typically |
| 680 | useful when multiple arguments need to store constants to the same list. For |
| 681 | example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 682 | |
| 683 | >>> parser = argparse.ArgumentParser() |
| 684 | >>> parser.add_argument('--str', dest='types', action='append_const', const=str) |
| 685 | >>> parser.add_argument('--int', dest='types', action='append_const', const=int) |
| 686 | >>> parser.parse_args('--str --int'.split()) |
| 687 | Namespace(types=[<type 'str'>, <type 'int'>]) |
| 688 | |
| 689 | * ``'version'`` - This expects a ``version=`` keyword argument in the |
| 690 | :meth:`add_argument` call, and prints version information and exits when |
| 691 | invoked. |
| 692 | |
| 693 | >>> import argparse |
| 694 | >>> parser = argparse.ArgumentParser(prog='PROG') |
Steven Bethard | 74bd9cf | 2010-05-24 02:38:00 +0000 | [diff] [blame] | 695 | >>> parser.add_argument('--version', action='version', version='%(prog)s 2.0') |
| 696 | >>> parser.parse_args(['--version']) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 697 | PROG 2.0 |
| 698 | |
| 699 | You can also specify an arbitrary action by passing an object that implements |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 700 | the Action API. The easiest way to do this is to extend |
| 701 | :class:`argparse.Action`, supplying an appropriate ``__call__`` method. The |
| 702 | ``__call__`` method should accept four parameters: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 703 | |
| 704 | * ``parser`` - The ArgumentParser object which contains this action. |
| 705 | |
| 706 | * ``namespace`` - The namespace object that will be returned by |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 707 | :meth:`parse_args`. Most actions add an attribute to this object. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 708 | |
| 709 | * ``values`` - The associated command-line args, with any type-conversions |
| 710 | applied. (Type-conversions are specified with the type_ keyword argument to |
| 711 | :meth:`add_argument`. |
| 712 | |
| 713 | * ``option_string`` - The option string that was used to invoke this action. |
| 714 | The ``option_string`` argument is optional, and will be absent if the action |
| 715 | is associated with a positional argument. |
| 716 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 717 | An example of a custom action:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 718 | |
| 719 | >>> class FooAction(argparse.Action): |
| 720 | ... def __call__(self, parser, namespace, values, option_string=None): |
Georg Brandl | 8891e23 | 2010-08-01 21:23:50 +0000 | [diff] [blame] | 721 | ... print '%r %r %r' % (namespace, values, option_string) |
| 722 | ... setattr(namespace, self.dest, values) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 723 | ... |
| 724 | >>> parser = argparse.ArgumentParser() |
| 725 | >>> parser.add_argument('--foo', action=FooAction) |
| 726 | >>> parser.add_argument('bar', action=FooAction) |
| 727 | >>> args = parser.parse_args('1 --foo 2'.split()) |
| 728 | Namespace(bar=None, foo=None) '1' None |
| 729 | Namespace(bar='1', foo=None) '2' '--foo' |
| 730 | >>> args |
| 731 | Namespace(bar='1', foo='2') |
| 732 | |
| 733 | |
| 734 | nargs |
| 735 | ^^^^^ |
| 736 | |
| 737 | ArgumentParser objects usually associate a single command-line argument with a |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 738 | single action to be taken. The ``nargs`` keyword argument associates a |
| 739 | different number of command-line arguments with a single action.. The supported |
| 740 | values are: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 741 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 742 | * N (an integer). N args from the command-line will be gathered together into a |
| 743 | list. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 744 | |
Georg Brandl | 35e7a8f | 2010-10-06 10:41:31 +0000 | [diff] [blame] | 745 | >>> parser = argparse.ArgumentParser() |
| 746 | >>> parser.add_argument('--foo', nargs=2) |
| 747 | >>> parser.add_argument('bar', nargs=1) |
| 748 | >>> parser.parse_args('c --foo a b'.split()) |
| 749 | Namespace(bar=['c'], foo=['a', 'b']) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 750 | |
Georg Brandl | 35e7a8f | 2010-10-06 10:41:31 +0000 | [diff] [blame] | 751 | Note that ``nargs=1`` produces a list of one item. This is different from |
| 752 | the default, in which the item is produced by itself. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 753 | |
| 754 | * ``'?'``. One arg will be consumed from the command-line if possible, and |
| 755 | produced as a single item. If no command-line arg is present, the value from |
| 756 | default_ will be produced. Note that for optional arguments, there is an |
| 757 | additional case - the option string is present but not followed by a |
| 758 | command-line arg. In this case the value from const_ will be produced. Some |
| 759 | examples to illustrate this:: |
| 760 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 761 | >>> parser = argparse.ArgumentParser() |
| 762 | >>> parser.add_argument('--foo', nargs='?', const='c', default='d') |
| 763 | >>> parser.add_argument('bar', nargs='?', default='d') |
| 764 | >>> parser.parse_args('XX --foo YY'.split()) |
| 765 | Namespace(bar='XX', foo='YY') |
| 766 | >>> parser.parse_args('XX --foo'.split()) |
| 767 | Namespace(bar='XX', foo='c') |
| 768 | >>> parser.parse_args(''.split()) |
| 769 | Namespace(bar='d', foo='d') |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 770 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 771 | One of the more common uses of ``nargs='?'`` is to allow optional input and |
| 772 | output files:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 773 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 774 | >>> parser = argparse.ArgumentParser() |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 775 | >>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
| 776 | ... default=sys.stdin) |
| 777 | >>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), |
| 778 | ... default=sys.stdout) |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 779 | >>> parser.parse_args(['input.txt', 'output.txt']) |
| 780 | Namespace(infile=<open file 'input.txt', mode 'r' at 0x...>, outfile=<open file 'output.txt', mode 'w' at 0x...>) |
| 781 | >>> parser.parse_args([]) |
| 782 | Namespace(infile=<open file '<stdin>', mode 'r' at 0x...>, outfile=<open file '<stdout>', mode 'w' at 0x...>) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 783 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 784 | * ``'*'``. All command-line args present are gathered into a list. Note that |
| 785 | it generally doesn't make much sense to have more than one positional argument |
| 786 | with ``nargs='*'``, but multiple optional arguments with ``nargs='*'`` is |
| 787 | possible. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 788 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 789 | >>> parser = argparse.ArgumentParser() |
| 790 | >>> parser.add_argument('--foo', nargs='*') |
| 791 | >>> parser.add_argument('--bar', nargs='*') |
| 792 | >>> parser.add_argument('baz', nargs='*') |
| 793 | >>> parser.parse_args('a b --foo x y --bar 1 2'.split()) |
| 794 | Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y']) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 795 | |
| 796 | * ``'+'``. Just like ``'*'``, all command-line args present are gathered into a |
| 797 | list. Additionally, an error message will be generated if there wasn't at |
| 798 | least one command-line arg present. For example:: |
| 799 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 800 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 801 | >>> parser.add_argument('foo', nargs='+') |
| 802 | >>> parser.parse_args('a b'.split()) |
| 803 | Namespace(foo=['a', 'b']) |
| 804 | >>> parser.parse_args(''.split()) |
| 805 | usage: PROG [-h] foo [foo ...] |
| 806 | PROG: error: too few arguments |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 807 | |
| 808 | If the ``nargs`` keyword argument is not provided, the number of args consumed |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 809 | is determined by the action_. Generally this means a single command-line arg |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 810 | will be consumed and a single item (not a list) will be produced. |
| 811 | |
| 812 | |
| 813 | const |
| 814 | ^^^^^ |
| 815 | |
| 816 | The ``const`` argument of :meth:`add_argument` is used to hold constant values |
| 817 | that are not read from the command line but are required for the various |
| 818 | ArgumentParser actions. The two most common uses of it are: |
| 819 | |
| 820 | * When :meth:`add_argument` is called with ``action='store_const'`` or |
| 821 | ``action='append_const'``. These actions add the ``const`` value to one of |
| 822 | the attributes of the object returned by :meth:`parse_args`. See the action_ |
| 823 | description for examples. |
| 824 | |
| 825 | * When :meth:`add_argument` is called with option strings (like ``-f`` or |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 826 | ``--foo``) and ``nargs='?'``. This creates an optional argument that can be |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 827 | followed by zero or one command-line args. When parsing the command-line, if |
| 828 | the option string is encountered with no command-line arg following it, the |
| 829 | value of ``const`` will be assumed instead. See the nargs_ description for |
| 830 | examples. |
| 831 | |
| 832 | The ``const`` keyword argument defaults to ``None``. |
| 833 | |
| 834 | |
| 835 | default |
| 836 | ^^^^^^^ |
| 837 | |
| 838 | All optional arguments and some positional arguments may be omitted at the |
| 839 | command-line. The ``default`` keyword argument of :meth:`add_argument`, whose |
| 840 | value defaults to ``None``, specifies what value should be used if the |
| 841 | command-line arg is not present. For optional arguments, the ``default`` value |
| 842 | is used when the option string was not present at the command line:: |
| 843 | |
| 844 | >>> parser = argparse.ArgumentParser() |
| 845 | >>> parser.add_argument('--foo', default=42) |
| 846 | >>> parser.parse_args('--foo 2'.split()) |
| 847 | Namespace(foo='2') |
| 848 | >>> parser.parse_args(''.split()) |
| 849 | Namespace(foo=42) |
| 850 | |
| 851 | For positional arguments with nargs_ ``='?'`` or ``'*'``, the ``default`` value |
| 852 | is used when no command-line arg was present:: |
| 853 | |
| 854 | >>> parser = argparse.ArgumentParser() |
| 855 | >>> parser.add_argument('foo', nargs='?', default=42) |
| 856 | >>> parser.parse_args('a'.split()) |
| 857 | Namespace(foo='a') |
| 858 | >>> parser.parse_args(''.split()) |
| 859 | Namespace(foo=42) |
| 860 | |
| 861 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 862 | Providing ``default=argparse.SUPPRESS`` causes no attribute to be added if the |
| 863 | command-line argument was not present.:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 864 | |
| 865 | >>> parser = argparse.ArgumentParser() |
| 866 | >>> parser.add_argument('--foo', default=argparse.SUPPRESS) |
| 867 | >>> parser.parse_args([]) |
| 868 | Namespace() |
| 869 | >>> parser.parse_args(['--foo', '1']) |
| 870 | Namespace(foo='1') |
| 871 | |
| 872 | |
| 873 | type |
| 874 | ^^^^ |
| 875 | |
| 876 | By default, ArgumentParser objects read command-line args in as simple strings. |
| 877 | However, quite often the command-line string should instead be interpreted as |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 878 | another type, like a :class:`float`, :class:`int` or :class:`file`. The |
| 879 | ``type`` keyword argument of :meth:`add_argument` allows any necessary |
Georg Brandl | 21e99f4 | 2010-03-07 15:23:59 +0000 | [diff] [blame] | 880 | type-checking and type-conversions to be performed. Many common built-in types |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 881 | can be used directly as the value of the ``type`` argument:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 882 | |
| 883 | >>> parser = argparse.ArgumentParser() |
| 884 | >>> parser.add_argument('foo', type=int) |
| 885 | >>> parser.add_argument('bar', type=file) |
| 886 | >>> parser.parse_args('2 temp.txt'.split()) |
| 887 | Namespace(bar=<open file 'temp.txt', mode 'r' at 0x...>, foo=2) |
| 888 | |
| 889 | To ease the use of various types of files, the argparse module provides the |
| 890 | factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 891 | ``file`` object. For example, ``FileType('w')`` can be used to create a |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 892 | writable file:: |
| 893 | |
| 894 | >>> parser = argparse.ArgumentParser() |
| 895 | >>> parser.add_argument('bar', type=argparse.FileType('w')) |
| 896 | >>> parser.parse_args(['out.txt']) |
| 897 | Namespace(bar=<open file 'out.txt', mode 'w' at 0x...>) |
| 898 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 899 | ``type=`` can take any callable that takes a single string argument and returns |
| 900 | the type-converted value:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 901 | |
| 902 | >>> def perfect_square(string): |
| 903 | ... value = int(string) |
| 904 | ... sqrt = math.sqrt(value) |
| 905 | ... if sqrt != int(sqrt): |
| 906 | ... msg = "%r is not a perfect square" % string |
| 907 | ... raise argparse.ArgumentTypeError(msg) |
| 908 | ... return value |
| 909 | ... |
| 910 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 911 | >>> parser.add_argument('foo', type=perfect_square) |
| 912 | >>> parser.parse_args('9'.split()) |
| 913 | Namespace(foo=9) |
| 914 | >>> parser.parse_args('7'.split()) |
| 915 | usage: PROG [-h] foo |
| 916 | PROG: error: argument foo: '7' is not a perfect square |
| 917 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 918 | The choices_ keyword argument may be more convenient for type checkers that |
| 919 | simply check against a range of values:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 920 | |
| 921 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 922 | >>> parser.add_argument('foo', type=int, choices=xrange(5, 10)) |
| 923 | >>> parser.parse_args('7'.split()) |
| 924 | Namespace(foo=7) |
| 925 | >>> parser.parse_args('11'.split()) |
| 926 | usage: PROG [-h] {5,6,7,8,9} |
| 927 | PROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9) |
| 928 | |
| 929 | See the choices_ section for more details. |
| 930 | |
| 931 | |
| 932 | choices |
| 933 | ^^^^^^^ |
| 934 | |
| 935 | Some command-line args should be selected from a restricted set of values. |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 936 | These can be handled by passing a container object as the ``choices`` keyword |
| 937 | argument to :meth:`add_argument`. When the command-line is parsed, arg values |
| 938 | will be checked, and an error message will be displayed if the arg was not one |
| 939 | of the acceptable values:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 940 | |
| 941 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 942 | >>> parser.add_argument('foo', choices='abc') |
| 943 | >>> parser.parse_args('c'.split()) |
| 944 | Namespace(foo='c') |
| 945 | >>> parser.parse_args('X'.split()) |
| 946 | usage: PROG [-h] {a,b,c} |
| 947 | PROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c') |
| 948 | |
| 949 | Note that inclusion in the ``choices`` container is checked after any type_ |
| 950 | conversions have been performed, so the type of the objects in the ``choices`` |
| 951 | container should match the type_ specified:: |
| 952 | |
| 953 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 954 | >>> parser.add_argument('foo', type=complex, choices=[1, 1j]) |
| 955 | >>> parser.parse_args('1j'.split()) |
| 956 | Namespace(foo=1j) |
| 957 | >>> parser.parse_args('-- -4'.split()) |
| 958 | usage: PROG [-h] {1,1j} |
| 959 | PROG: error: argument foo: invalid choice: (-4+0j) (choose from 1, 1j) |
| 960 | |
| 961 | Any object that supports the ``in`` operator can be passed as the ``choices`` |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 962 | value, so :class:`dict` objects, :class:`set` objects, custom containers, |
| 963 | etc. are all supported. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 964 | |
| 965 | |
| 966 | required |
| 967 | ^^^^^^^^ |
| 968 | |
| 969 | In general, the argparse module assumes that flags like ``-f`` and ``--bar`` |
| 970 | indicate *optional* arguments, which can always be omitted at the command-line. |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 971 | To make an option *required*, ``True`` can be specified for the ``required=`` |
| 972 | keyword argument to :meth:`add_argument`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 973 | |
| 974 | >>> parser = argparse.ArgumentParser() |
| 975 | >>> parser.add_argument('--foo', required=True) |
| 976 | >>> parser.parse_args(['--foo', 'BAR']) |
| 977 | Namespace(foo='BAR') |
| 978 | >>> parser.parse_args([]) |
| 979 | usage: argparse.py [-h] [--foo FOO] |
| 980 | argparse.py: error: option --foo is required |
| 981 | |
| 982 | As the example shows, if an option is marked as ``required``, :meth:`parse_args` |
| 983 | will report an error if that option is not present at the command line. |
| 984 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 985 | .. note:: |
| 986 | |
| 987 | Required options are generally considered bad form because users expect |
| 988 | *options* to be *optional*, and thus they should be avoided when possible. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 989 | |
| 990 | |
| 991 | help |
| 992 | ^^^^ |
| 993 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 994 | The ``help`` value is a string containing a brief description of the argument. |
| 995 | When a user requests help (usually by using ``-h`` or ``--help`` at the |
| 996 | command-line), these ``help`` descriptions will be displayed with each |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 997 | argument:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 998 | |
| 999 | >>> parser = argparse.ArgumentParser(prog='frobble') |
| 1000 | >>> parser.add_argument('--foo', action='store_true', |
| 1001 | ... help='foo the bars before frobbling') |
| 1002 | >>> parser.add_argument('bar', nargs='+', |
| 1003 | ... help='one of the bars to be frobbled') |
| 1004 | >>> parser.parse_args('-h'.split()) |
| 1005 | usage: frobble [-h] [--foo] bar [bar ...] |
| 1006 | |
| 1007 | positional arguments: |
| 1008 | bar one of the bars to be frobbled |
| 1009 | |
| 1010 | optional arguments: |
| 1011 | -h, --help show this help message and exit |
| 1012 | --foo foo the bars before frobbling |
| 1013 | |
| 1014 | The ``help`` strings can include various format specifiers to avoid repetition |
| 1015 | of things like the program name or the argument default_. The available |
| 1016 | specifiers include the program name, ``%(prog)s`` and most keyword arguments to |
| 1017 | :meth:`add_argument`, e.g. ``%(default)s``, ``%(type)s``, etc.:: |
| 1018 | |
| 1019 | >>> parser = argparse.ArgumentParser(prog='frobble') |
| 1020 | >>> parser.add_argument('bar', nargs='?', type=int, default=42, |
| 1021 | ... help='the bar to %(prog)s (default: %(default)s)') |
| 1022 | >>> parser.print_help() |
| 1023 | usage: frobble [-h] [bar] |
| 1024 | |
| 1025 | positional arguments: |
| 1026 | bar the bar to frobble (default: 42) |
| 1027 | |
| 1028 | optional arguments: |
| 1029 | -h, --help show this help message and exit |
| 1030 | |
| 1031 | |
| 1032 | metavar |
| 1033 | ^^^^^^^ |
| 1034 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1035 | When :class:`ArgumentParser` generates help messages, it need some way to refer |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1036 | to each expected argument. By default, ArgumentParser objects use the dest_ |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1037 | value as the "name" of each object. By default, for positional argument |
| 1038 | actions, the dest_ value is used directly, and for optional argument actions, |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1039 | the dest_ value is uppercased. So, a single positional argument with |
| 1040 | ``dest='bar'`` will that argument will be referred to as ``bar``. A single |
| 1041 | optional argument ``--foo`` that should be followed by a single command-line arg |
| 1042 | will be referred to as ``FOO``. An example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1043 | |
| 1044 | >>> parser = argparse.ArgumentParser() |
| 1045 | >>> parser.add_argument('--foo') |
| 1046 | >>> parser.add_argument('bar') |
| 1047 | >>> parser.parse_args('X --foo Y'.split()) |
| 1048 | Namespace(bar='X', foo='Y') |
| 1049 | >>> parser.print_help() |
| 1050 | usage: [-h] [--foo FOO] bar |
| 1051 | |
| 1052 | positional arguments: |
| 1053 | bar |
| 1054 | |
| 1055 | optional arguments: |
| 1056 | -h, --help show this help message and exit |
| 1057 | --foo FOO |
| 1058 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1059 | An alternative name can be specified with ``metavar``:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1060 | |
| 1061 | >>> parser = argparse.ArgumentParser() |
| 1062 | >>> parser.add_argument('--foo', metavar='YYY') |
| 1063 | >>> parser.add_argument('bar', metavar='XXX') |
| 1064 | >>> parser.parse_args('X --foo Y'.split()) |
| 1065 | Namespace(bar='X', foo='Y') |
| 1066 | >>> parser.print_help() |
| 1067 | usage: [-h] [--foo YYY] XXX |
| 1068 | |
| 1069 | positional arguments: |
| 1070 | XXX |
| 1071 | |
| 1072 | optional arguments: |
| 1073 | -h, --help show this help message and exit |
| 1074 | --foo YYY |
| 1075 | |
| 1076 | Note that ``metavar`` only changes the *displayed* name - the name of the |
| 1077 | attribute on the :meth:`parse_args` object is still determined by the dest_ |
| 1078 | value. |
| 1079 | |
| 1080 | Different values of ``nargs`` may cause the metavar to be used multiple times. |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1081 | Providing a tuple to ``metavar`` specifies a different display for each of the |
| 1082 | arguments:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1083 | |
| 1084 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1085 | >>> parser.add_argument('-x', nargs=2) |
| 1086 | >>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz')) |
| 1087 | >>> parser.print_help() |
| 1088 | usage: PROG [-h] [-x X X] [--foo bar baz] |
| 1089 | |
| 1090 | optional arguments: |
| 1091 | -h, --help show this help message and exit |
| 1092 | -x X X |
| 1093 | --foo bar baz |
| 1094 | |
| 1095 | |
| 1096 | dest |
| 1097 | ^^^^ |
| 1098 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1099 | Most :class:`ArgumentParser` actions add some value as an attribute of the |
| 1100 | object returned by :meth:`parse_args`. The name of this attribute is determined |
| 1101 | by the ``dest`` keyword argument of :meth:`add_argument`. For positional |
| 1102 | argument actions, ``dest`` is normally supplied as the first argument to |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1103 | :meth:`add_argument`:: |
| 1104 | |
| 1105 | >>> parser = argparse.ArgumentParser() |
| 1106 | >>> parser.add_argument('bar') |
| 1107 | >>> parser.parse_args('XXX'.split()) |
| 1108 | Namespace(bar='XXX') |
| 1109 | |
| 1110 | For optional argument actions, the value of ``dest`` is normally inferred from |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1111 | the option strings. :class:`ArgumentParser` generates the value of ``dest`` by |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1112 | taking the first long option string and stripping away the initial ``'--'`` |
| 1113 | string. If no long option strings were supplied, ``dest`` will be derived from |
| 1114 | the first short option string by stripping the initial ``'-'`` character. Any |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1115 | internal ``'-'`` characters will be converted to ``'_'`` characters to make sure |
| 1116 | the string is a valid attribute name. The examples below illustrate this |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1117 | behavior:: |
| 1118 | |
| 1119 | >>> parser = argparse.ArgumentParser() |
| 1120 | >>> parser.add_argument('-f', '--foo-bar', '--foo') |
| 1121 | >>> parser.add_argument('-x', '-y') |
| 1122 | >>> parser.parse_args('-f 1 -x 2'.split()) |
| 1123 | Namespace(foo_bar='1', x='2') |
| 1124 | >>> parser.parse_args('--foo 1 -y 2'.split()) |
| 1125 | Namespace(foo_bar='1', x='2') |
| 1126 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1127 | ``dest`` allows a custom attribute name to be provided:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1128 | |
| 1129 | >>> parser = argparse.ArgumentParser() |
| 1130 | >>> parser.add_argument('--foo', dest='bar') |
| 1131 | >>> parser.parse_args('--foo XXX'.split()) |
| 1132 | Namespace(bar='XXX') |
| 1133 | |
| 1134 | |
| 1135 | The parse_args() method |
| 1136 | ----------------------- |
| 1137 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1138 | .. method:: ArgumentParser.parse_args(args=None, namespace=None) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1139 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1140 | Convert argument strings to objects and assign them as attributes of the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1141 | namespace. Return the populated namespace. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1142 | |
| 1143 | Previous calls to :meth:`add_argument` determine exactly what objects are |
| 1144 | created and how they are assigned. See the documentation for |
| 1145 | :meth:`add_argument` for details. |
| 1146 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1147 | By default, the arg strings are taken from :data:`sys.argv`, and a new empty |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1148 | :class:`Namespace` object is created for the attributes. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1149 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1150 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1151 | Option value syntax |
| 1152 | ^^^^^^^^^^^^^^^^^^^ |
| 1153 | |
| 1154 | The :meth:`parse_args` method supports several ways of specifying the value of |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1155 | an option (if it takes one). In the simplest case, the option and its value are |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1156 | passed as two separate arguments:: |
| 1157 | |
| 1158 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1159 | >>> parser.add_argument('-x') |
| 1160 | >>> parser.add_argument('--foo') |
| 1161 | >>> parser.parse_args('-x X'.split()) |
| 1162 | Namespace(foo=None, x='X') |
| 1163 | >>> parser.parse_args('--foo FOO'.split()) |
| 1164 | Namespace(foo='FOO', x=None) |
| 1165 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1166 | For long options (options with names longer than a single character), the option |
| 1167 | and value can also be passed as a single command line argument, using ``=`` to |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1168 | separate them:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1169 | |
| 1170 | >>> parser.parse_args('--foo=FOO'.split()) |
| 1171 | Namespace(foo='FOO', x=None) |
| 1172 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1173 | For short options (options only one character long), the option and its value |
| 1174 | can be concatenated:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1175 | |
| 1176 | >>> parser.parse_args('-xX'.split()) |
| 1177 | Namespace(foo=None, x='X') |
| 1178 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1179 | Several short options can be joined together, using only a single ``-`` prefix, |
| 1180 | as long as only the last option (or none of them) requires a value:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1181 | |
| 1182 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1183 | >>> parser.add_argument('-x', action='store_true') |
| 1184 | >>> parser.add_argument('-y', action='store_true') |
| 1185 | >>> parser.add_argument('-z') |
| 1186 | >>> parser.parse_args('-xyzZ'.split()) |
| 1187 | Namespace(x=True, y=True, z='Z') |
| 1188 | |
| 1189 | |
| 1190 | Invalid arguments |
| 1191 | ^^^^^^^^^^^^^^^^^ |
| 1192 | |
| 1193 | While parsing the command-line, ``parse_args`` checks for a variety of errors, |
| 1194 | including ambiguous options, invalid types, invalid options, wrong number of |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1195 | positional arguments, etc. When it encounters such an error, it exits and |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1196 | prints the error along with a usage message:: |
| 1197 | |
| 1198 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1199 | >>> parser.add_argument('--foo', type=int) |
| 1200 | >>> parser.add_argument('bar', nargs='?') |
| 1201 | |
| 1202 | >>> # invalid type |
| 1203 | >>> parser.parse_args(['--foo', 'spam']) |
| 1204 | usage: PROG [-h] [--foo FOO] [bar] |
| 1205 | PROG: error: argument --foo: invalid int value: 'spam' |
| 1206 | |
| 1207 | >>> # invalid option |
| 1208 | >>> parser.parse_args(['--bar']) |
| 1209 | usage: PROG [-h] [--foo FOO] [bar] |
| 1210 | PROG: error: no such option: --bar |
| 1211 | |
| 1212 | >>> # wrong number of arguments |
| 1213 | >>> parser.parse_args(['spam', 'badger']) |
| 1214 | usage: PROG [-h] [--foo FOO] [bar] |
| 1215 | PROG: error: extra arguments found: badger |
| 1216 | |
| 1217 | |
| 1218 | Arguments containing ``"-"`` |
| 1219 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1220 | |
| 1221 | The ``parse_args`` method attempts to give errors whenever the user has clearly |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1222 | made a mistake, but some situations are inherently ambiguous. For example, the |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1223 | command-line arg ``'-1'`` could either be an attempt to specify an option or an |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1224 | attempt to provide a positional argument. The ``parse_args`` method is cautious |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1225 | here: positional arguments may only begin with ``'-'`` if they look like |
| 1226 | negative numbers and there are no options in the parser that look like negative |
| 1227 | numbers:: |
| 1228 | |
| 1229 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1230 | >>> parser.add_argument('-x') |
| 1231 | >>> parser.add_argument('foo', nargs='?') |
| 1232 | |
| 1233 | >>> # no negative number options, so -1 is a positional argument |
| 1234 | >>> parser.parse_args(['-x', '-1']) |
| 1235 | Namespace(foo=None, x='-1') |
| 1236 | |
| 1237 | >>> # no negative number options, so -1 and -5 are positional arguments |
| 1238 | >>> parser.parse_args(['-x', '-1', '-5']) |
| 1239 | Namespace(foo='-5', x='-1') |
| 1240 | |
| 1241 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1242 | >>> parser.add_argument('-1', dest='one') |
| 1243 | >>> parser.add_argument('foo', nargs='?') |
| 1244 | |
| 1245 | >>> # negative number options present, so -1 is an option |
| 1246 | >>> parser.parse_args(['-1', 'X']) |
| 1247 | Namespace(foo=None, one='X') |
| 1248 | |
| 1249 | >>> # negative number options present, so -2 is an option |
| 1250 | >>> parser.parse_args(['-2']) |
| 1251 | usage: PROG [-h] [-1 ONE] [foo] |
| 1252 | PROG: error: no such option: -2 |
| 1253 | |
| 1254 | >>> # negative number options present, so both -1s are options |
| 1255 | >>> parser.parse_args(['-1', '-1']) |
| 1256 | usage: PROG [-h] [-1 ONE] [foo] |
| 1257 | PROG: error: argument -1: expected one argument |
| 1258 | |
| 1259 | If you have positional arguments that must begin with ``'-'`` and don't look |
| 1260 | like negative numbers, you can insert the pseudo-argument ``'--'`` which tells |
| 1261 | ``parse_args`` that everything after that is a positional argument:: |
| 1262 | |
| 1263 | >>> parser.parse_args(['--', '-f']) |
| 1264 | Namespace(foo='-f', one=None) |
| 1265 | |
| 1266 | |
| 1267 | Argument abbreviations |
| 1268 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 1269 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1270 | The :meth:`parse_args` method allows long options to be abbreviated if the |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1271 | abbreviation is unambiguous:: |
| 1272 | |
| 1273 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1274 | >>> parser.add_argument('-bacon') |
| 1275 | >>> parser.add_argument('-badger') |
| 1276 | >>> parser.parse_args('-bac MMM'.split()) |
| 1277 | Namespace(bacon='MMM', badger=None) |
| 1278 | >>> parser.parse_args('-bad WOOD'.split()) |
| 1279 | Namespace(bacon=None, badger='WOOD') |
| 1280 | >>> parser.parse_args('-ba BA'.split()) |
| 1281 | usage: PROG [-h] [-bacon BACON] [-badger BADGER] |
| 1282 | PROG: error: ambiguous option: -ba could match -badger, -bacon |
| 1283 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1284 | An error is produced for arguments that could produce more than one options. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1285 | |
| 1286 | |
| 1287 | Beyond ``sys.argv`` |
| 1288 | ^^^^^^^^^^^^^^^^^^^ |
| 1289 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1290 | Sometimes it may be useful to have an ArgumentParser parse args other than those |
| 1291 | of :data:`sys.argv`. This can be accomplished by passing a list of strings to |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1292 | ``parse_args``. This is useful for testing at the interactive prompt:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1293 | |
| 1294 | >>> parser = argparse.ArgumentParser() |
| 1295 | >>> parser.add_argument( |
| 1296 | ... 'integers', metavar='int', type=int, choices=xrange(10), |
| 1297 | ... nargs='+', help='an integer in the range 0..9') |
| 1298 | >>> parser.add_argument( |
| 1299 | ... '--sum', dest='accumulate', action='store_const', const=sum, |
| 1300 | ... default=max, help='sum the integers (default: find the max)') |
| 1301 | >>> parser.parse_args(['1', '2', '3', '4']) |
| 1302 | Namespace(accumulate=<built-in function max>, integers=[1, 2, 3, 4]) |
| 1303 | >>> parser.parse_args('1 2 3 4 --sum'.split()) |
| 1304 | Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4]) |
| 1305 | |
| 1306 | |
| 1307 | Custom namespaces |
| 1308 | ^^^^^^^^^^^^^^^^^ |
| 1309 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1310 | It may also be useful to have an :class:`ArgumentParser` assign attributes to an |
| 1311 | already existing object, rather than the newly-created :class:`Namespace` object |
| 1312 | that is normally used. This can be achieved by specifying the ``namespace=`` |
| 1313 | keyword argument:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1314 | |
| 1315 | >>> class C(object): |
| 1316 | ... pass |
| 1317 | ... |
| 1318 | >>> c = C() |
| 1319 | >>> parser = argparse.ArgumentParser() |
| 1320 | >>> parser.add_argument('--foo') |
| 1321 | >>> parser.parse_args(args=['--foo', 'BAR'], namespace=c) |
| 1322 | >>> c.foo |
| 1323 | 'BAR' |
| 1324 | |
| 1325 | |
| 1326 | Other utilities |
| 1327 | --------------- |
| 1328 | |
| 1329 | Sub-commands |
| 1330 | ^^^^^^^^^^^^ |
| 1331 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1332 | .. method:: ArgumentParser.add_subparsers() |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1333 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1334 | Many programs split up their functionality into a number of sub-commands, |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1335 | for example, the ``svn`` program can invoke sub-commands like ``svn |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1336 | checkout``, ``svn update``, and ``svn commit``. Splitting up functionality |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1337 | this way can be a particularly good idea when a program performs several |
| 1338 | different functions which require different kinds of command-line arguments. |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1339 | :class:`ArgumentParser` supports the creation of such sub-commands with the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1340 | :meth:`add_subparsers` method. The :meth:`add_subparsers` method is normally |
| 1341 | called with no arguments and returns an special action object. This object |
| 1342 | has a single method, ``add_parser``, which takes a command name and any |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1343 | :class:`ArgumentParser` constructor arguments, and returns an |
| 1344 | :class:`ArgumentParser` object that can be modified as usual. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1345 | |
| 1346 | Some example usage:: |
| 1347 | |
| 1348 | >>> # create the top-level parser |
| 1349 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1350 | >>> parser.add_argument('--foo', action='store_true', help='foo help') |
| 1351 | >>> subparsers = parser.add_subparsers(help='sub-command help') |
| 1352 | >>> |
| 1353 | >>> # create the parser for the "a" command |
| 1354 | >>> parser_a = subparsers.add_parser('a', help='a help') |
| 1355 | >>> parser_a.add_argument('bar', type=int, help='bar help') |
| 1356 | >>> |
| 1357 | >>> # create the parser for the "b" command |
| 1358 | >>> parser_b = subparsers.add_parser('b', help='b help') |
| 1359 | >>> parser_b.add_argument('--baz', choices='XYZ', help='baz help') |
| 1360 | >>> |
| 1361 | >>> # parse some arg lists |
| 1362 | >>> parser.parse_args(['a', '12']) |
| 1363 | Namespace(bar=12, foo=False) |
| 1364 | >>> parser.parse_args(['--foo', 'b', '--baz', 'Z']) |
| 1365 | Namespace(baz='Z', foo=True) |
| 1366 | |
| 1367 | Note that the object returned by :meth:`parse_args` will only contain |
| 1368 | attributes for the main parser and the subparser that was selected by the |
| 1369 | command line (and not any other subparsers). So in the example above, when |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1370 | the ``"a"`` command is specified, only the ``foo`` and ``bar`` attributes are |
| 1371 | present, and when the ``"b"`` command is specified, only the ``foo`` and |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1372 | ``baz`` attributes are present. |
| 1373 | |
| 1374 | Similarly, when a help message is requested from a subparser, only the help |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1375 | for that particular parser will be printed. The help message will not |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1376 | include parent parser or sibling parser messages. (A help message for each |
| 1377 | subparser command, however, can be given by supplying the ``help=`` argument |
| 1378 | to ``add_parser`` as above.) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1379 | |
| 1380 | :: |
| 1381 | |
| 1382 | >>> parser.parse_args(['--help']) |
| 1383 | usage: PROG [-h] [--foo] {a,b} ... |
| 1384 | |
| 1385 | positional arguments: |
| 1386 | {a,b} sub-command help |
| 1387 | a a help |
| 1388 | b b help |
| 1389 | |
| 1390 | optional arguments: |
| 1391 | -h, --help show this help message and exit |
| 1392 | --foo foo help |
| 1393 | |
| 1394 | >>> parser.parse_args(['a', '--help']) |
| 1395 | usage: PROG a [-h] bar |
| 1396 | |
| 1397 | positional arguments: |
| 1398 | bar bar help |
| 1399 | |
| 1400 | optional arguments: |
| 1401 | -h, --help show this help message and exit |
| 1402 | |
| 1403 | >>> parser.parse_args(['b', '--help']) |
| 1404 | usage: PROG b [-h] [--baz {X,Y,Z}] |
| 1405 | |
| 1406 | optional arguments: |
| 1407 | -h, --help show this help message and exit |
| 1408 | --baz {X,Y,Z} baz help |
| 1409 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1410 | The :meth:`add_subparsers` method also supports ``title`` and ``description`` |
| 1411 | keyword arguments. When either is present, the subparser's commands will |
| 1412 | appear in their own group in the help output. For example:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1413 | |
| 1414 | >>> parser = argparse.ArgumentParser() |
| 1415 | >>> subparsers = parser.add_subparsers(title='subcommands', |
| 1416 | ... description='valid subcommands', |
| 1417 | ... help='additional help') |
| 1418 | >>> subparsers.add_parser('foo') |
| 1419 | >>> subparsers.add_parser('bar') |
| 1420 | >>> parser.parse_args(['-h']) |
| 1421 | usage: [-h] {foo,bar} ... |
| 1422 | |
| 1423 | optional arguments: |
| 1424 | -h, --help show this help message and exit |
| 1425 | |
| 1426 | subcommands: |
| 1427 | valid subcommands |
| 1428 | |
| 1429 | {foo,bar} additional help |
| 1430 | |
| 1431 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1432 | One particularly effective way of handling sub-commands is to combine the use |
| 1433 | of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so |
| 1434 | that each subparser knows which Python function it should execute. For |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1435 | example:: |
| 1436 | |
| 1437 | >>> # sub-command functions |
| 1438 | >>> def foo(args): |
| 1439 | ... print args.x * args.y |
| 1440 | ... |
| 1441 | >>> def bar(args): |
| 1442 | ... print '((%s))' % args.z |
| 1443 | ... |
| 1444 | >>> # create the top-level parser |
| 1445 | >>> parser = argparse.ArgumentParser() |
| 1446 | >>> subparsers = parser.add_subparsers() |
| 1447 | >>> |
| 1448 | >>> # create the parser for the "foo" command |
| 1449 | >>> parser_foo = subparsers.add_parser('foo') |
| 1450 | >>> parser_foo.add_argument('-x', type=int, default=1) |
| 1451 | >>> parser_foo.add_argument('y', type=float) |
| 1452 | >>> parser_foo.set_defaults(func=foo) |
| 1453 | >>> |
| 1454 | >>> # create the parser for the "bar" command |
| 1455 | >>> parser_bar = subparsers.add_parser('bar') |
| 1456 | >>> parser_bar.add_argument('z') |
| 1457 | >>> parser_bar.set_defaults(func=bar) |
| 1458 | >>> |
| 1459 | >>> # parse the args and call whatever function was selected |
| 1460 | >>> args = parser.parse_args('foo 1 -x 2'.split()) |
| 1461 | >>> args.func(args) |
| 1462 | 2.0 |
| 1463 | >>> |
| 1464 | >>> # parse the args and call whatever function was selected |
| 1465 | >>> args = parser.parse_args('bar XYZYX'.split()) |
| 1466 | >>> args.func(args) |
| 1467 | ((XYZYX)) |
| 1468 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1469 | This way, you can let :meth:`parse_args` does the job of calling the |
| 1470 | appropriate function after argument parsing is complete. Associating |
| 1471 | functions with actions like this is typically the easiest way to handle the |
| 1472 | different actions for each of your subparsers. However, if it is necessary |
| 1473 | to check the name of the subparser that was invoked, the ``dest`` keyword |
| 1474 | argument to the :meth:`add_subparsers` call will work:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1475 | |
| 1476 | >>> parser = argparse.ArgumentParser() |
| 1477 | >>> subparsers = parser.add_subparsers(dest='subparser_name') |
| 1478 | >>> subparser1 = subparsers.add_parser('1') |
| 1479 | >>> subparser1.add_argument('-x') |
| 1480 | >>> subparser2 = subparsers.add_parser('2') |
| 1481 | >>> subparser2.add_argument('y') |
| 1482 | >>> parser.parse_args(['2', 'frobble']) |
| 1483 | Namespace(subparser_name='2', y='frobble') |
| 1484 | |
| 1485 | |
| 1486 | FileType objects |
| 1487 | ^^^^^^^^^^^^^^^^ |
| 1488 | |
| 1489 | .. class:: FileType(mode='r', bufsize=None) |
| 1490 | |
| 1491 | The :class:`FileType` factory creates objects that can be passed to the type |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1492 | argument of :meth:`ArgumentParser.add_argument`. Arguments that have |
| 1493 | :class:`FileType` objects as their type will open command-line args as files |
| 1494 | with the requested modes and buffer sizes: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1495 | |
| 1496 | >>> parser = argparse.ArgumentParser() |
| 1497 | >>> parser.add_argument('--output', type=argparse.FileType('wb', 0)) |
| 1498 | >>> parser.parse_args(['--output', 'out']) |
| 1499 | Namespace(output=<open file 'out', mode 'wb' at 0x...>) |
| 1500 | |
| 1501 | FileType objects understand the pseudo-argument ``'-'`` and automatically |
| 1502 | convert this into ``sys.stdin`` for readable :class:`FileType` objects and |
| 1503 | ``sys.stdout`` for writable :class:`FileType` objects: |
| 1504 | |
| 1505 | >>> parser = argparse.ArgumentParser() |
| 1506 | >>> parser.add_argument('infile', type=argparse.FileType('r')) |
| 1507 | >>> parser.parse_args(['-']) |
| 1508 | Namespace(infile=<open file '<stdin>', mode 'r' at 0x...>) |
| 1509 | |
| 1510 | |
| 1511 | Argument groups |
| 1512 | ^^^^^^^^^^^^^^^ |
| 1513 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1514 | .. method:: ArgumentParser.add_argument_group(title=None, description=None) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1515 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1516 | By default, :class:`ArgumentParser` groups command-line arguments into |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1517 | "positional arguments" and "optional arguments" when displaying help |
| 1518 | messages. When there is a better conceptual grouping of arguments than this |
| 1519 | default one, appropriate groups can be created using the |
| 1520 | :meth:`add_argument_group` method:: |
| 1521 | |
| 1522 | >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False) |
| 1523 | >>> group = parser.add_argument_group('group') |
| 1524 | >>> group.add_argument('--foo', help='foo help') |
| 1525 | >>> group.add_argument('bar', help='bar help') |
| 1526 | >>> parser.print_help() |
| 1527 | usage: PROG [--foo FOO] bar |
| 1528 | |
| 1529 | group: |
| 1530 | bar bar help |
| 1531 | --foo FOO foo help |
| 1532 | |
| 1533 | The :meth:`add_argument_group` method returns an argument group object which |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1534 | has an :meth:`~ArgumentParser.add_argument` method just like a regular |
| 1535 | :class:`ArgumentParser`. When an argument is added to the group, the parser |
| 1536 | treats it just like a normal argument, but displays the argument in a |
| 1537 | separate group for help messages. The :meth:`add_argument_group` method |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1538 | accepts *title* and *description* arguments which can be used to |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1539 | customize this display:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1540 | |
| 1541 | >>> parser = argparse.ArgumentParser(prog='PROG', add_help=False) |
| 1542 | >>> group1 = parser.add_argument_group('group1', 'group1 description') |
| 1543 | >>> group1.add_argument('foo', help='foo help') |
| 1544 | >>> group2 = parser.add_argument_group('group2', 'group2 description') |
| 1545 | >>> group2.add_argument('--bar', help='bar help') |
| 1546 | >>> parser.print_help() |
| 1547 | usage: PROG [--bar BAR] foo |
| 1548 | |
| 1549 | group1: |
| 1550 | group1 description |
| 1551 | |
| 1552 | foo foo help |
| 1553 | |
| 1554 | group2: |
| 1555 | group2 description |
| 1556 | |
| 1557 | --bar BAR bar help |
| 1558 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1559 | Note that any arguments not your user defined groups will end up back in the |
| 1560 | usual "positional arguments" and "optional arguments" sections. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1561 | |
| 1562 | |
| 1563 | Mutual exclusion |
| 1564 | ^^^^^^^^^^^^^^^^ |
| 1565 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1566 | .. method:: add_mutually_exclusive_group(required=False) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1567 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1568 | Create a mutually exclusive group. argparse will make sure that only one of |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1569 | the arguments in the mutually exclusive group was present on the command |
| 1570 | line:: |
| 1571 | |
| 1572 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1573 | >>> group = parser.add_mutually_exclusive_group() |
| 1574 | >>> group.add_argument('--foo', action='store_true') |
| 1575 | >>> group.add_argument('--bar', action='store_false') |
| 1576 | >>> parser.parse_args(['--foo']) |
| 1577 | Namespace(bar=True, foo=True) |
| 1578 | >>> parser.parse_args(['--bar']) |
| 1579 | Namespace(bar=False, foo=False) |
| 1580 | >>> parser.parse_args(['--foo', '--bar']) |
| 1581 | usage: PROG [-h] [--foo | --bar] |
| 1582 | PROG: error: argument --bar: not allowed with argument --foo |
| 1583 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1584 | The :meth:`add_mutually_exclusive_group` method also accepts a *required* |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1585 | argument, to indicate that at least one of the mutually exclusive arguments |
| 1586 | is required:: |
| 1587 | |
| 1588 | >>> parser = argparse.ArgumentParser(prog='PROG') |
| 1589 | >>> group = parser.add_mutually_exclusive_group(required=True) |
| 1590 | >>> group.add_argument('--foo', action='store_true') |
| 1591 | >>> group.add_argument('--bar', action='store_false') |
| 1592 | >>> parser.parse_args([]) |
| 1593 | usage: PROG [-h] (--foo | --bar) |
| 1594 | PROG: error: one of the arguments --foo --bar is required |
| 1595 | |
| 1596 | Note that currently mutually exclusive argument groups do not support the |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1597 | *title* and *description* arguments of :meth:`add_argument_group`. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1598 | |
| 1599 | |
| 1600 | Parser defaults |
| 1601 | ^^^^^^^^^^^^^^^ |
| 1602 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1603 | .. method:: ArgumentParser.set_defaults(**kwargs) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1604 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1605 | Most of the time, the attributes of the object returned by :meth:`parse_args` |
| 1606 | will be fully determined by inspecting the command-line args and the argument |
Benjamin Peterson | c516d19 | 2010-03-03 02:04:24 +0000 | [diff] [blame] | 1607 | actions. :meth:`ArgumentParser.set_defaults` allows some additional |
| 1608 | attributes that are determined without any inspection of the command-line to |
| 1609 | be added:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1610 | |
| 1611 | >>> parser = argparse.ArgumentParser() |
| 1612 | >>> parser.add_argument('foo', type=int) |
| 1613 | >>> parser.set_defaults(bar=42, baz='badger') |
| 1614 | >>> parser.parse_args(['736']) |
| 1615 | Namespace(bar=42, baz='badger', foo=736) |
| 1616 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1617 | Note that parser-level defaults always override argument-level defaults:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1618 | |
| 1619 | >>> parser = argparse.ArgumentParser() |
| 1620 | >>> parser.add_argument('--foo', default='bar') |
| 1621 | >>> parser.set_defaults(foo='spam') |
| 1622 | >>> parser.parse_args([]) |
| 1623 | Namespace(foo='spam') |
| 1624 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1625 | Parser-level defaults can be particularly useful when working with multiple |
| 1626 | parsers. See the :meth:`~ArgumentParser.add_subparsers` method for an |
| 1627 | example of this type. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1628 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1629 | .. method:: ArgumentParser.get_default(dest) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1630 | |
| 1631 | Get the default value for a namespace attribute, as set by either |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1632 | :meth:`~ArgumentParser.add_argument` or by |
| 1633 | :meth:`~ArgumentParser.set_defaults`:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1634 | |
| 1635 | >>> parser = argparse.ArgumentParser() |
| 1636 | >>> parser.add_argument('--foo', default='badger') |
| 1637 | >>> parser.get_default('foo') |
| 1638 | 'badger' |
| 1639 | |
| 1640 | |
| 1641 | Printing help |
| 1642 | ^^^^^^^^^^^^^ |
| 1643 | |
| 1644 | In most typical applications, :meth:`parse_args` will take care of formatting |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1645 | and printing any usage or error messages. However, several formatting methods |
| 1646 | are available: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1647 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1648 | .. method:: ArgumentParser.print_usage(file=None) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1649 | |
| 1650 | Print a brief description of how the :class:`ArgumentParser` should be |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1651 | invoked on the command line. If *file* is ``None``, :data:`sys.stderr` is |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1652 | assumed. |
| 1653 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1654 | .. method:: ArgumentParser.print_help(file=None) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1655 | |
| 1656 | Print a help message, including the program usage and information about the |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1657 | arguments registered with the :class:`ArgumentParser`. If *file* is |
| 1658 | ``None``, :data:`sys.stderr` is assumed. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1659 | |
| 1660 | There are also variants of these methods that simply return a string instead of |
| 1661 | printing it: |
| 1662 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1663 | .. method:: ArgumentParser.format_usage() |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1664 | |
| 1665 | Return a string containing a brief description of how the |
| 1666 | :class:`ArgumentParser` should be invoked on the command line. |
| 1667 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1668 | .. method:: ArgumentParser.format_help() |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1669 | |
| 1670 | Return a string containing a help message, including the program usage and |
| 1671 | information about the arguments registered with the :class:`ArgumentParser`. |
| 1672 | |
| 1673 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1674 | Partial parsing |
| 1675 | ^^^^^^^^^^^^^^^ |
| 1676 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1677 | .. method:: ArgumentParser.parse_known_args(args=None, namespace=None) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1678 | |
| 1679 | Sometimes a script may only parse a few of the command line arguments, passing |
| 1680 | the remaining arguments on to another script or program. In these cases, the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1681 | :meth:`parse_known_args` method can be useful. It works much like |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1682 | :meth:`~ArgumentParser.parse_args` except that it does not produce an error when |
| 1683 | extra arguments are present. Instead, it returns a two item tuple containing |
| 1684 | the populated namespace and the list of remaining argument strings. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1685 | |
| 1686 | :: |
| 1687 | |
| 1688 | >>> parser = argparse.ArgumentParser() |
| 1689 | >>> parser.add_argument('--foo', action='store_true') |
| 1690 | >>> parser.add_argument('bar') |
| 1691 | >>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam']) |
| 1692 | (Namespace(bar='BAR', foo=True), ['--badger', 'spam']) |
| 1693 | |
| 1694 | |
| 1695 | Customizing file parsing |
| 1696 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1697 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1698 | .. method:: ArgumentParser.convert_arg_line_to_args(arg_line) |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1699 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1700 | Arguments that are read from a file (see the *fromfile_prefix_chars* |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1701 | keyword argument to the :class:`ArgumentParser` constructor) are read one |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1702 | argument per line. :meth:`convert_arg_line_to_args` can be overriden for |
| 1703 | fancier reading. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1704 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1705 | This method takes a single argument *arg_line* which is a string read from |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1706 | the argument file. It returns a list of arguments parsed from this string. |
| 1707 | The method is called once per line read from the argument file, in order. |
| 1708 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1709 | A useful override of this method is one that treats each space-separated word |
| 1710 | as an argument:: |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1711 | |
| 1712 | def convert_arg_line_to_args(self, arg_line): |
| 1713 | for arg in arg_line.split(): |
| 1714 | if not arg.strip(): |
| 1715 | continue |
| 1716 | yield arg |
| 1717 | |
| 1718 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame^] | 1719 | Exiting methods |
| 1720 | ^^^^^^^^^^^^^^^ |
| 1721 | |
| 1722 | .. method:: ArgumentParser.exit(status=0, message=None) |
| 1723 | |
| 1724 | This method terminates the program, exiting with the specified *status* |
| 1725 | and, if given, it prints a *message* before that. |
| 1726 | |
| 1727 | .. method:: ArgumentParser.error(message) |
| 1728 | |
| 1729 | This method prints a usage message including the *message* to the |
| 1730 | standard output and terminates the program with a status code of 2. |
| 1731 | |
| 1732 | |
Georg Brandl | 58df679 | 2010-07-03 10:25:47 +0000 | [diff] [blame] | 1733 | .. _argparse-from-optparse: |
| 1734 | |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1735 | Upgrading optparse code |
| 1736 | ----------------------- |
| 1737 | |
| 1738 | Originally, the argparse module had attempted to maintain compatibility with |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1739 | optparse. However, optparse was difficult to extend transparently, particularly |
| 1740 | with the changes required to support the new ``nargs=`` specifiers and better |
Georg Brandl | 404bd7f | 2010-04-25 10:16:00 +0000 | [diff] [blame] | 1741 | usage messages. When most everything in optparse had either been copy-pasted |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1742 | over or monkey-patched, it no longer seemed practical to try to maintain the |
| 1743 | backwards compatibility. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1744 | |
| 1745 | A partial upgrade path from optparse to argparse: |
| 1746 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1747 | * Replace all ``add_option()`` calls with :meth:`ArgumentParser.add_argument` calls. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1748 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1749 | * Replace ``options, args = parser.parse_args()`` with ``args = |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1750 | parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument` calls for the |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1751 | positional arguments. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1752 | |
| 1753 | * Replace callback actions and the ``callback_*`` keyword arguments with |
| 1754 | ``type`` or ``action`` arguments. |
| 1755 | |
| 1756 | * Replace string names for ``type`` keyword arguments with the corresponding |
| 1757 | type objects (e.g. int, float, complex, etc). |
| 1758 | |
Benjamin Peterson | 90c5802 | 2010-03-03 01:55:09 +0000 | [diff] [blame] | 1759 | * Replace :class:`optparse.Values` with :class:`Namespace` and |
| 1760 | :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with |
| 1761 | :exc:`ArgumentError`. |
Benjamin Peterson | a39e966 | 2010-03-02 22:05:59 +0000 | [diff] [blame] | 1762 | |
Georg Brandl | d2decd9 | 2010-03-02 22:17:38 +0000 | [diff] [blame] | 1763 | * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with |
| 1764 | the standard python syntax to use dictionaries to format strings, that is, |
| 1765 | ``%(default)s`` and ``%(prog)s``. |
Steven Bethard | 74bd9cf | 2010-05-24 02:38:00 +0000 | [diff] [blame] | 1766 | |
| 1767 | * Replace the OptionParser constructor ``version`` argument with a call to |
| 1768 | ``parser.add_argument('--version', action='version', version='<the version>')`` |