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