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