korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>15.4. argparse — Parser for command-line options, arguments and sub-commands — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="15. Generic Operating System Services" href="allos.html" /> <link rel="next" title="15.5. optparse — Parser for command line options" href="optparse.html" /> <link rel="prev" title="15.3. time — Time access and conversions" href="time.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="optparse.html" title="15.5. optparse — Parser for command line options" accesskey="N">next</a> |</li> <li class="right" > <a href="time.html" title="15.3. time — Time access and conversions" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="allos.html" accesskey="U">15. Generic Operating System Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-argparse"> <span id="argparse-parser-for-command-line-options-arguments-and-sub-commands"></span><h1>15.4. <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> — Parser for command-line options, arguments and sub-commands<a class="headerlink" href="#module-argparse" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/argparse.py">Lib/argparse.py</a></p> <hr class="docutils" /> <div class="sidebar"> <p class="first sidebar-title">Tutorial</p> <p class="last">This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the <a class="reference internal" href="../howto/argparse.html#id1"><em>argparse tutorial</em></a>.</p> </div> <p>The <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> will figure out how to parse those out of <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><tt class="xref py py-data docutils literal"><span class="pre">sys.argv</span></tt></a>. The <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.</p> <div class="section" id="example"> <h2>15.4.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2> <p>The following code is a Python program that takes a list of integers and produces either the sum or the max:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">argparse</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s">'Process some integers.'</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'integers'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'N'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'an integer for the accumulator'</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--sum'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'accumulate'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_const'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="nb">sum</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="nb">max</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'sum the integers (default: find the max)'</span><span class="p">)</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> <span class="k">print</span> <span class="n">args</span><span class="o">.</span><span class="n">accumulate</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">integers</span><span class="p">)</span> </pre></div> </div> <p>Assuming the Python code above is saved into a file called <tt class="docutils literal"><span class="pre">prog.py</span></tt>, it can be run at the command line and provides useful help messages:</p> <div class="highlight-python"><pre>$ prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max)</pre> </div> <p>When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:</p> <div class="highlight-python"><pre>$ prog.py 1 2 3 4 4 $ prog.py 1 2 3 4 --sum 10</pre> </div> <p>If invalid arguments are passed in, it will issue an error:</p> <div class="highlight-python"><pre>$ prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'</pre> </div> <p>The following sections walk you through this example.</p> <div class="section" id="creating-a-parser"> <h3>15.4.1.1. Creating a parser<a class="headerlink" href="#creating-a-parser" title="Permalink to this headline">¶</a></h3> <p>The first step in using the <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> is creating an <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s">'Process some integers.'</span><span class="p">)</span> </pre></div> </div> <p>The <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> object will hold all the information necessary to parse the command line into Python data types.</p> </div> <div class="section" id="adding-arguments"> <h3>15.4.1.2. Adding arguments<a class="headerlink" href="#adding-arguments" title="Permalink to this headline">¶</a></h3> <p>Filling an <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> with information about program arguments is done by making calls to the <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> method. Generally, these calls tell the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> how to take the strings on the command line and turn them into objects. This information is stored and used when <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> is called. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'integers'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'N'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'an integer for the accumulator'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--sum'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'accumulate'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_const'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">const</span><span class="o">=</span><span class="nb">sum</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="nb">max</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'sum the integers (default: find the max)'</span><span class="p">)</span> </pre></div> </div> <p>Later, calling <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will return an object with two attributes, <tt class="docutils literal"><span class="pre">integers</span></tt> and <tt class="docutils literal"><span class="pre">accumulate</span></tt>. The <tt class="docutils literal"><span class="pre">integers</span></tt> attribute will be a list of one or more ints, and the <tt class="docutils literal"><span class="pre">accumulate</span></tt> attribute will be either the <a class="reference internal" href="functions.html#sum" title="sum"><tt class="xref py py-func docutils literal"><span class="pre">sum()</span></tt></a> function, if <tt class="docutils literal"><span class="pre">--sum</span></tt> was specified at the command line, or the <a class="reference internal" href="functions.html#max" title="max"><tt class="xref py py-func docutils literal"><span class="pre">max()</span></tt></a> function if it was not.</p> </div> <div class="section" id="parsing-arguments"> <h3>15.4.1.3. Parsing arguments<a class="headerlink" href="#parsing-arguments" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> parses arguments through the <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> method. This will inspect the command line, convert each argument to the appropriate type and then invoke the appropriate action. In most cases, this means a simple <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object will be built up from attributes parsed out of the command line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--sum'</span><span class="p">,</span> <span class="s">'7'</span><span class="p">,</span> <span class="s">'-1'</span><span class="p">,</span> <span class="s">'42'</span><span class="p">])</span> <span class="go">Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])</span> </pre></div> </div> <p>In a script, <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will typically be called with no arguments, and the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> will automatically determine the command-line arguments from <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><tt class="xref py py-data docutils literal"><span class="pre">sys.argv</span></tt></a>.</p> </div> </div> <div class="section" id="argumentparser-objects"> <h2>15.4.2. ArgumentParser objects<a class="headerlink" href="#argumentparser-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="argparse.ArgumentParser"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">ArgumentParser</tt><big>(</big><em>prog=None</em>, <em>usage=None</em>, <em>description=None</em>, <em>epilog=None</em>, <em>parents=</em><span class="optional">[</span><span class="optional">]</span>, <em>formatter_class=argparse.HelpFormatter</em>, <em>prefix_chars='-'</em>, <em>fromfile_prefix_chars=None</em>, <em>argument_default=None</em>, <em>conflict_handler='error'</em>, <em>add_help=True</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> object. All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are:</p> <ul class="simple"> <li><a class="reference internal" href="#prog">prog</a> - The name of the program (default: <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt>)</li> <li><a class="reference internal" href="#usage">usage</a> - The string describing the program usage (default: generated from arguments added to parser)</li> <li><a class="reference internal" href="#description">description</a> - Text to display before the argument help (default: none)</li> <li><a class="reference internal" href="#epilog">epilog</a> - Text to display after the argument help (default: none)</li> <li><a class="reference internal" href="#parents">parents</a> - A list of <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects whose arguments should also be included</li> <li><a class="reference internal" href="#formatter-class">formatter_class</a> - A class for customizing the help output</li> <li><a class="reference internal" href="#prefix-chars">prefix_chars</a> - The set of characters that prefix optional arguments (default: ‘-‘)</li> <li><a class="reference internal" href="#fromfile-prefix-chars">fromfile_prefix_chars</a> - The set of characters that prefix files from which additional arguments should be read (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</li> <li><a class="reference internal" href="#argument-default">argument_default</a> - The global default value for arguments (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</li> <li><a class="reference internal" href="#conflict-handler">conflict_handler</a> - The strategy for resolving conflicting optionals (usually unnecessary)</li> <li><a class="reference internal" href="#add-help">add_help</a> - Add a -h/–help option to the parser (default: <tt class="docutils literal"><span class="pre">True</span></tt>)</li> </ul> </dd></dl> <p>The following sections describe how each of these are used.</p> <div class="section" id="prog"> <h3>15.4.2.1. prog<a class="headerlink" href="#prog" title="Permalink to this headline">¶</a></h3> <p>By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects uses <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> to determine how to display the name of the program in help messages. This default is almost always desirable because it will make the help messages match how the program was invoked on the command line. For example, consider a file named <tt class="docutils literal"><span class="pre">myprogram.py</span></tt> with the following code:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">argparse</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>The help for this program will display <tt class="docutils literal"><span class="pre">myprogram.py</span></tt> as the program name (regardless of where the program was invoked from):</p> <div class="highlight-python"><pre>$ python myprogram.py --help usage: myprogram.py [-h] [--foo FOO] optional arguments: -h, --help show this help message and exit --foo FOO foo help $ cd .. $ python subdir\myprogram.py --help usage: myprogram.py [-h] [--foo FOO] optional arguments: -h, --help show this help message and exit --foo FOO foo help</pre> </div> <p>To change this default behavior, another value can be supplied using the <tt class="docutils literal"><span class="pre">prog=</span></tt> argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'myprogram'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: myprogram [-h]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> <p>Note that the program name, whether determined from <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> or from the <tt class="docutils literal"><span class="pre">prog=</span></tt> argument, is available to help messages using the <tt class="docutils literal"><span class="pre">%(prog)s</span></tt> format specifier.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'myprogram'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo of the </span><span class="si">%(prog)s</span><span class="s"> program'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: myprogram [-h] [--foo FOO]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo FOO foo of the myprogram program</span> </pre></div> </div> </div> <div class="section" id="usage"> <h3>15.4.2.2. usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h3> <p>By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> calculates the usage message from the arguments it contains:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'bar help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h] [--foo [FOO]] bar [bar ...]</span> <span class="go">positional arguments:</span> <span class="go"> bar bar help</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo [FOO] foo help</span> </pre></div> </div> <p>The default message can be overridden with the <tt class="docutils literal"><span class="pre">usage=</span></tt> keyword argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">usage</span><span class="o">=</span><span class="s">'</span><span class="si">%(prog)s</span><span class="s"> [options]'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'bar help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [options]</span> <span class="go">positional arguments:</span> <span class="go"> bar bar help</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo [FOO] foo help</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">%(prog)s</span></tt> format specifier is available to fill in the program name in your usage messages.</p> </div> <div class="section" id="description"> <h3>15.4.2.3. description<a class="headerlink" href="#description" title="Permalink to this headline">¶</a></h3> <p>Most calls to the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> constructor will use the <tt class="docutils literal"><span class="pre">description=</span></tt> keyword argument. This argument gives a brief description of what the program does and how it works. In help messages, the description is displayed between the command-line usage string and the help messages for the various arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s">'A foo that bars'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: argparse.py [-h]</span> <span class="go">A foo that bars</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> <p>By default, the description will be line-wrapped so that it fits within the given space. To change this behavior, see the <a class="reference internal" href="#formatter-class">formatter_class</a> argument.</p> </div> <div class="section" id="epilog"> <h3>15.4.2.4. epilog<a class="headerlink" href="#epilog" title="Permalink to this headline">¶</a></h3> <p>Some programs like to display additional description of the program after the description of the arguments. Such text can be specified using the <tt class="docutils literal"><span class="pre">epilog=</span></tt> argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span> <span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="s">'A foo that bars'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">epilog</span><span class="o">=</span><span class="s">"And that's how you'd foo a bar"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: argparse.py [-h]</span> <span class="go">A foo that bars</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go">And that's how you'd foo a bar</span> </pre></div> </div> <p>As with the <a class="reference internal" href="#description">description</a> argument, the <tt class="docutils literal"><span class="pre">epilog=</span></tt> text is by default line-wrapped, but this behavior can be adjusted with the <a class="reference internal" href="#formatter-class">formatter_class</a> argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>.</p> </div> <div class="section" id="parents"> <h3>15.4.2.5. parents<a class="headerlink" href="#parents" title="Permalink to this headline">¶</a></h3> <p>Sometimes, several parsers share a common set of arguments. Rather than repeating the definitions of these arguments, a single parser with all the shared arguments and passed to <tt class="docutils literal"><span class="pre">parents=</span></tt> argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> can be used. The <tt class="docutils literal"><span class="pre">parents=</span></tt> argument takes a list of <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects, collects all the positional and optional actions from them, and adds these actions to the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> object being constructed:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parent_parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">add_help</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parent_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--parent'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">foo_parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="p">[</span><span class="n">parent_parser</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">foo_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">foo_parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--parent'</span><span class="p">,</span> <span class="s">'2'</span><span class="p">,</span> <span class="s">'XXX'</span><span class="p">])</span> <span class="go">Namespace(foo='XXX', parent=2)</span> <span class="gp">>>> </span><span class="n">bar_parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="p">[</span><span class="n">parent_parser</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">bar_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">bar_parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--bar'</span><span class="p">,</span> <span class="s">'YYY'</span><span class="p">])</span> <span class="go">Namespace(bar='YYY', parent=None)</span> </pre></div> </div> <p>Note that most parent parsers will specify <tt class="docutils literal"><span class="pre">add_help=False</span></tt>. Otherwise, the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> will see two <tt class="docutils literal"><span class="pre">-h/--help</span></tt> options (one in the parent and one in the child) and raise an error.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">You must fully initialize the parsers before passing them via <tt class="docutils literal"><span class="pre">parents=</span></tt>. If you change the parent parsers after the child parser, those changes will not be reflected in the child.</p> </div> </div> <div class="section" id="formatter-class"> <h3>15.4.2.6. formatter_class<a class="headerlink" href="#formatter-class" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects allow the help formatting to be customized by specifying an alternate formatting class. Currently, there are three such classes:</p> <dl class="class"> <dt id="argparse.RawDescriptionHelpFormatter"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">RawDescriptionHelpFormatter</tt><a class="headerlink" href="#argparse.RawDescriptionHelpFormatter" title="Permalink to this definition">¶</a></dt> <dt id="argparse.RawTextHelpFormatter"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">RawTextHelpFormatter</tt><a class="headerlink" href="#argparse.RawTextHelpFormatter" title="Permalink to this definition">¶</a></dt> <dt id="argparse.ArgumentDefaultsHelpFormatter"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">ArgumentDefaultsHelpFormatter</tt><a class="headerlink" href="#argparse.ArgumentDefaultsHelpFormatter" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>The first two allow more control over how textual descriptions are displayed, while the last automatically adds information about argument default values.</p> <p>By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects line-wrap the <a class="reference internal" href="#description">description</a> and <a class="reference internal" href="#epilog">epilog</a> texts in command-line help messages:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span> <span class="gp">... </span> <span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="s">'''this description</span> <span class="gp">... </span><span class="s"> was indented weird</span> <span class="gp">... </span><span class="s"> but that is okay'''</span><span class="p">,</span> <span class="gp">... </span> <span class="n">epilog</span><span class="o">=</span><span class="s">'''</span> <span class="gp">... </span><span class="s"> likewise for this epilog whose whitespace will</span> <span class="gp">... </span><span class="s"> be cleaned up and whose words will be wrapped</span> <span class="gp">... </span><span class="s"> across a couple lines'''</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h]</span> <span class="go">this description was indented weird but that is okay</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go">likewise for this epilog whose whitespace will be cleaned up and whose words</span> <span class="go">will be wrapped across a couple lines</span> </pre></div> </div> <p>Passing <a class="reference internal" href="#argparse.RawDescriptionHelpFormatter" title="argparse.RawDescriptionHelpFormatter"><tt class="xref py py-class docutils literal"><span class="pre">RawDescriptionHelpFormatter</span></tt></a> as <tt class="docutils literal"><span class="pre">formatter_class=</span></tt> indicates that <a class="reference internal" href="#description">description</a> and <a class="reference internal" href="#epilog">epilog</a> are already correctly formatted and should not be line-wrapped:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span> <span class="gp">... </span> <span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">formatter_class</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">RawDescriptionHelpFormatter</span><span class="p">,</span> <span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="n">textwrap</span><span class="o">.</span><span class="n">dedent</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span> <span class="gp">... </span><span class="s"> Please do not mess up this text!</span> <span class="gp">... </span><span class="s"> --------------------------------</span> <span class="gp">... </span><span class="s"> I have indented it</span> <span class="gp">... </span><span class="s"> exactly the way</span> <span class="gp">... </span><span class="s"> I want it</span> <span class="gp">... </span><span class="s"> '''</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h]</span> <span class="go">Please do not mess up this text!</span> <span class="go">--------------------------------</span> <span class="go"> I have indented it</span> <span class="go"> exactly the way</span> <span class="go"> I want it</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> <p><a class="reference internal" href="#argparse.RawTextHelpFormatter" title="argparse.RawTextHelpFormatter"><tt class="xref py py-class docutils literal"><span class="pre">RawTextHelpFormatter</span></tt></a> maintains whitespace for all sorts of help text, including argument descriptions.</p> <p>The other formatter class available, <a class="reference internal" href="#argparse.ArgumentDefaultsHelpFormatter" title="argparse.ArgumentDefaultsHelpFormatter"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentDefaultsHelpFormatter</span></tt></a>, will add information about the default value of each of the arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span> <span class="gp">... </span> <span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">formatter_class</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentDefaultsHelpFormatter</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'FOO!'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'*'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">help</span><span class="o">=</span><span class="s">'BAR!'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h] [--foo FOO] [bar [bar ...]]</span> <span class="go">positional arguments:</span> <span class="go"> bar BAR! (default: [1, 2, 3])</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo FOO FOO! (default: 42)</span> </pre></div> </div> </div> <div class="section" id="prefix-chars"> <h3>15.4.2.7. prefix_chars<a class="headerlink" href="#prefix-chars" title="Permalink to this headline">¶</a></h3> <p>Most command-line options will use <tt class="docutils literal"><span class="pre">-</span></tt> as the prefix, e.g. <tt class="docutils literal"><span class="pre">-f/--foo</span></tt>. Parsers that need to support different or additional prefix characters, e.g. for options like <tt class="docutils literal"><span class="pre">+f</span></tt> or <tt class="docutils literal"><span class="pre">/foo</span></tt>, may specify them using the <tt class="docutils literal"><span class="pre">prefix_chars=</span></tt> argument to the ArgumentParser constructor:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">prefix_chars</span><span class="o">=</span><span class="s">'-+'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'+f'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'++bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'+f X ++bar Y'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='Y', f='X')</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">prefix_chars=</span></tt> argument defaults to <tt class="docutils literal"><span class="pre">'-'</span></tt>. Supplying a set of characters that does not include <tt class="docutils literal"><span class="pre">-</span></tt> will cause <tt class="docutils literal"><span class="pre">-f/--foo</span></tt> options to be disallowed.</p> </div> <div class="section" id="fromfile-prefix-chars"> <h3>15.4.2.8. fromfile_prefix_chars<a class="headerlink" href="#fromfile-prefix-chars" title="Permalink to this headline">¶</a></h3> <p>Sometimes, for example when dealing with a particularly long argument lists, it may make sense to keep the list of arguments in a file rather than typing it out at the command line. If the <tt class="docutils literal"><span class="pre">fromfile_prefix_chars=</span></tt> argument is given to the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> constructor, then arguments that start with any of the specified characters will be treated as files, and will be replaced by the arguments they contain. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'args.txt'</span><span class="p">,</span> <span class="s">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span> <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'-f</span><span class="se">\n</span><span class="s">bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">fromfile_prefix_chars</span><span class="o">=</span><span class="s">'@'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'foo'</span><span class="p">,</span> <span class="s">'@args.txt'</span><span class="p">])</span> <span class="go">Namespace(f='bar')</span> </pre></div> </div> <p>Arguments read from a file must by default be one per line (but see also <a class="reference internal" href="#argparse.ArgumentParser.convert_arg_line_to_args" title="argparse.ArgumentParser.convert_arg_line_to_args"><tt class="xref py py-meth docutils literal"><span class="pre">convert_arg_line_to_args()</span></tt></a>) and are treated as if they were in the same place as the original file referencing argument on the command line. So in the example above, the expression <tt class="docutils literal"><span class="pre">['-f',</span> <span class="pre">'foo',</span> <span class="pre">'@args.txt']</span></tt> is considered equivalent to the expression <tt class="docutils literal"><span class="pre">['-f',</span> <span class="pre">'foo',</span> <span class="pre">'-f',</span> <span class="pre">'bar']</span></tt>.</p> <p>The <tt class="docutils literal"><span class="pre">fromfile_prefix_chars=</span></tt> argument defaults to <tt class="docutils literal"><span class="pre">None</span></tt>, meaning that arguments will never be treated as file references.</p> </div> <div class="section" id="argument-default"> <h3>15.4.2.9. argument_default<a class="headerlink" href="#argument-default" title="Permalink to this headline">¶</a></h3> <p>Generally, argument defaults are specified either by passing a default to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> or by calling the <a class="reference internal" href="#argparse.ArgumentParser.set_defaults" title="argparse.ArgumentParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a> methods with a specific set of name-value pairs. Sometimes however, it may be useful to specify a single parser-wide default for arguments. This can be accomplished by passing the <tt class="docutils literal"><span class="pre">argument_default=</span></tt> keyword argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>. For example, to globally suppress attribute creation on <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> calls, we supply <tt class="docutils literal"><span class="pre">argument_default=SUPPRESS</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">argument_default</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">SUPPRESS</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'1'</span><span class="p">,</span> <span class="s">'BAR'</span><span class="p">])</span> <span class="go">Namespace(bar='BAR', foo='1')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">Namespace()</span> </pre></div> </div> </div> <div class="section" id="conflict-handler"> <h3>15.4.2.10. conflict_handler<a class="headerlink" href="#conflict-handler" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects do not allow two actions with the same option string. By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects raises an exception if an attempt is made to create an argument with an option string that is already in use:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'old foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'new foo help'</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="c"> ..</span> <span class="gr">ArgumentError: argument --foo: conflicting option string(s)</span>: <span class="n">--foo</span> </pre></div> </div> <p>Sometimes (e.g. when using <a class="reference internal" href="#parents">parents</a>) it may be useful to simply override any older arguments with the same option string. To get this behavior, the value <tt class="docutils literal"><span class="pre">'resolve'</span></tt> can be supplied to the <tt class="docutils literal"><span class="pre">conflict_handler=</span></tt> argument of <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">conflict_handler</span><span class="o">=</span><span class="s">'resolve'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'old foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'new foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h] [-f FOO] [--foo FOO]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> -f FOO old foo help</span> <span class="go"> --foo FOO new foo help</span> </pre></div> </div> <p>Note that <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects only remove an action if all of its option strings are overridden. So, in the example above, the old <tt class="docutils literal"><span class="pre">-f/--foo</span></tt> action is retained as the <tt class="docutils literal"><span class="pre">-f</span></tt> action, because only the <tt class="docutils literal"><span class="pre">--foo</span></tt> option string was overridden.</p> </div> <div class="section" id="add-help"> <h3>15.4.2.11. add_help<a class="headerlink" href="#add-help" title="Permalink to this headline">¶</a></h3> <p>By default, ArgumentParser objects add an option which simply displays the parser’s help message. For example, consider a file named <tt class="docutils literal"><span class="pre">myprogram.py</span></tt> containing the following code:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">argparse</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> is supplied at the command line, the ArgumentParser help will be printed:</p> <div class="highlight-python"><pre>$ python myprogram.py --help usage: myprogram.py [-h] [--foo FOO] optional arguments: -h, --help show this help message and exit --foo FOO foo help</pre> </div> <p>Occasionally, it may be useful to disable the addition of this help option. This can be achieved by passing <tt class="docutils literal"><span class="pre">False</span></tt> as the <tt class="docutils literal"><span class="pre">add_help=</span></tt> argument to <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">add_help</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [--foo FOO]</span> <span class="go">optional arguments:</span> <span class="go"> --foo FOO foo help</span> </pre></div> </div> <p>The help option is typically <tt class="docutils literal"><span class="pre">-h/--help</span></tt>. The exception to this is if the <tt class="docutils literal"><span class="pre">prefix_chars=</span></tt> is specified and does not include <tt class="docutils literal"><span class="pre">-</span></tt>, in which case <tt class="docutils literal"><span class="pre">-h</span></tt> and <tt class="docutils literal"><span class="pre">--help</span></tt> are not valid options. In this case, the first character in <tt class="docutils literal"><span class="pre">prefix_chars</span></tt> is used to prefix the help options:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">prefix_chars</span><span class="o">=</span><span class="s">'+/'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> </div> </div> <div class="section" id="the-add-argument-method"> <h2>15.4.3. The add_argument() method<a class="headerlink" href="#the-add-argument-method" title="Permalink to this headline">¶</a></h2> <dl class="method"> <dt id="argparse.ArgumentParser.add_argument"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">add_argument</tt><big>(</big><em>name or flags...</em><span class="optional">[</span>, <em>action</em><span class="optional">]</span><span class="optional">[</span>, <em>nargs</em><span class="optional">]</span><span class="optional">[</span>, <em>const</em><span class="optional">]</span><span class="optional">[</span>, <em>default</em><span class="optional">]</span><span class="optional">[</span>, <em>type</em><span class="optional">]</span><span class="optional">[</span>, <em>choices</em><span class="optional">]</span><span class="optional">[</span>, <em>required</em><span class="optional">]</span><span class="optional">[</span>, <em>help</em><span class="optional">]</span><span class="optional">[</span>, <em>metavar</em><span class="optional">]</span><span class="optional">[</span>, <em>dest</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.add_argument" title="Permalink to this definition">¶</a></dt> <dd><p>Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:</p> <ul class="simple"> <li><a class="reference internal" href="#name-or-flags">name or flags</a> - Either a name or a list of option strings, e.g. <tt class="docutils literal"><span class="pre">foo</span></tt> or <tt class="docutils literal"><span class="pre">-f,</span> <span class="pre">--foo</span></tt>.</li> <li><a class="reference internal" href="#action">action</a> - The basic type of action to be taken when this argument is encountered at the command line.</li> <li><a class="reference internal" href="#nargs">nargs</a> - The number of command-line arguments that should be consumed.</li> <li><a class="reference internal" href="#const">const</a> - A constant value required by some <a class="reference internal" href="#action">action</a> and <a class="reference internal" href="#nargs">nargs</a> selections.</li> <li><a class="reference internal" href="#default">default</a> - The value produced if the argument is absent from the command line.</li> <li><a class="reference internal" href="#type">type</a> - The type to which the command-line argument should be converted.</li> <li><a class="reference internal" href="#choices">choices</a> - A container of the allowable values for the argument.</li> <li><a class="reference internal" href="#required">required</a> - Whether or not the command-line option may be omitted (optionals only).</li> <li><a class="reference internal" href="#help">help</a> - A brief description of what the argument does.</li> <li><a class="reference internal" href="#metavar">metavar</a> - A name for the argument in usage messages.</li> <li><a class="reference internal" href="#dest">dest</a> - The name of the attribute to be added to the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>.</li> </ul> </dd></dl> <p>The following sections describe how each of these are used.</p> <div class="section" id="name-or-flags"> <h3>15.4.3.1. name or flags<a class="headerlink" href="#name-or-flags" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> method must know whether an optional argument, like <tt class="docutils literal"><span class="pre">-f</span></tt> or <tt class="docutils literal"><span class="pre">--foo</span></tt>, or a positional argument, like a list of filenames, is expected. The first arguments passed to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> must therefore be either a series of flags, or a simple argument name. For example, an optional argument could be created like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">)</span> </pre></div> </div> <p>while a positional argument could be created like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> </pre></div> </div> <p>When <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> is called, optional arguments will be identified by the <tt class="docutils literal"><span class="pre">-</span></tt> prefix, and the remaining arguments will be assumed to be positional:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'BAR'</span><span class="p">])</span> <span class="go">Namespace(bar='BAR', foo=None)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'BAR'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">,</span> <span class="s">'FOO'</span><span class="p">])</span> <span class="go">Namespace(bar='BAR', foo='FOO')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'FOO'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [-f FOO] bar</span> <span class="go">PROG: error: too few arguments</span> </pre></div> </div> </div> <div class="section" id="action"> <h3>15.4.3.2. action<a class="headerlink" href="#action" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects associate command-line arguments with actions. These actions can do just about anything with the command-line arguments associated with them, though most actions simply add an attribute to the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>. The <tt class="docutils literal"><span class="pre">action</span></tt> keyword argument specifies how the command-line arguments should be handled. The supported actions are:</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">'store'</span></tt> - This just stores the argument’s value. This is the default action. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo 1'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo='1')</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'store_const'</span></tt> - This stores the value specified by the <a class="reference internal" href="#const">const</a> keyword argument. (Note that the <a class="reference internal" href="#const">const</a> keyword argument defaults to the rather unhelpful <tt class="docutils literal"><span class="pre">None</span></tt>.) The <tt class="docutils literal"><span class="pre">'store_const'</span></tt> action is most commonly used with optional arguments that specify some sort of flag. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_const'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=42)</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'store_true'</span></tt> and <tt class="docutils literal"><span class="pre">'store_false'</span></tt> - These are special cases of <tt class="docutils literal"><span class="pre">'store_const'</span></tt> using for storing the values <tt class="docutils literal"><span class="pre">True</span></tt> and <tt class="docutils literal"><span class="pre">False</span></tt> respectively. In addition, they create default values of <em>False</em> and <em>True</em> respectively. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_false'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--baz'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_false'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo --bar'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar=False, baz=True, foo=True)</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'append'</span></tt> - This stores a list, and appends each argument value to the list. This is useful to allow an option to be specified multiple times. Example usage:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'append'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo 1 --foo 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=['1', '2'])</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'append_const'</span></tt> - This stores a list, and appends the value specified by the <a class="reference internal" href="#const">const</a> keyword argument to the list. (Note that the <a class="reference internal" href="#const">const</a> keyword argument defaults to <tt class="docutils literal"><span class="pre">None</span></tt>.) The <tt class="docutils literal"><span class="pre">'append_const'</span></tt> action is typically useful when multiple arguments need to store constants to the same list. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--str'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'types'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'append_const'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--int'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'types'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'append_const'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--str --int'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(types=[<type 'str'>, <type 'int'>])</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'count'</span></tt> - This counts the number of times a keyword argument occurs. For example, this is useful for increasing verbosity levels:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--verbose'</span><span class="p">,</span> <span class="s">'-v'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'count'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-vvv'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(verbose=3)</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'help'</span></tt> - This prints a complete help message for all the options in the current parser and then exits. By default a help action is automatically added to the parser. See <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> for details of how the output is created.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'version'</span></tt> - This expects a <tt class="docutils literal"><span class="pre">version=</span></tt> keyword argument in the <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> call, and prints version information and exits when invoked:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">argparse</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--version'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'version'</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">'</span><span class="si">%(prog)s</span><span class="s"> 2.0'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--version'</span><span class="p">])</span> <span class="go">PROG 2.0</span> </pre></div> </div> </li> </ul> <p>You can also specify an arbitrary action by passing an object that implements the Action API. The easiest way to do this is to extend <tt class="xref py py-class docutils literal"><span class="pre">argparse.Action</span></tt>, supplying an appropriate <tt class="docutils literal"><span class="pre">__call__</span></tt> method. The <tt class="docutils literal"><span class="pre">__call__</span></tt> method should accept four parameters:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">parser</span></tt> - The ArgumentParser object which contains this action.</li> <li><tt class="docutils literal"><span class="pre">namespace</span></tt> - The <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object that will be returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>. Most actions add an attribute to this object.</li> <li><tt class="docutils literal"><span class="pre">values</span></tt> - The associated command-line arguments, with any type conversions applied. (Type conversions are specified with the <a class="reference internal" href="#type">type</a> keyword argument to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>.)</li> <li><tt class="docutils literal"><span class="pre">option_string</span></tt> - The option string that was used to invoke this action. The <tt class="docutils literal"><span class="pre">option_string</span></tt> argument is optional, and will be absent if the action is associated with a positional argument.</li> </ul> <p>An example of a custom action:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">FooAction</span><span class="p">(</span><span class="n">argparse</span><span class="o">.</span><span class="n">Action</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">option_string</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="gp">... </span> <span class="k">print</span> <span class="s">'</span><span class="si">%r</span><span class="s"> </span><span class="si">%r</span><span class="s"> </span><span class="si">%r</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">option_string</span><span class="p">)</span> <span class="gp">... </span> <span class="nb">setattr</span><span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">FooAction</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">FooAction</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'1 --foo 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar=None, foo=None) '1' None</span> <span class="go">Namespace(bar='1', foo=None) '2' '--foo'</span> <span class="gp">>>> </span><span class="n">args</span> <span class="go">Namespace(bar='1', foo='2')</span> </pre></div> </div> </div> <div class="section" id="nargs"> <h3>15.4.3.3. nargs<a class="headerlink" href="#nargs" title="Permalink to this headline">¶</a></h3> <p>ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The <tt class="docutils literal"><span class="pre">nargs</span></tt> keyword argument associates a different number of command-line arguments with a single action. The supported values are:</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">N</span></tt> (an integer). <tt class="docutils literal"><span class="pre">N</span></tt> arguments from the command line will be gathered together into a list. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'c --foo a b'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar=['c'], foo=['a', 'b'])</span> </pre></div> </div> <p>Note that <tt class="docutils literal"><span class="pre">nargs=1</span></tt> produces a list of one item. This is different from the default, in which the item is produced by itself.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'?'</span></tt>. One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from <a class="reference internal" href="#default">default</a> will be produced. Note that for optional arguments, there is an additional case - the option string is present but not followed by a command-line argument. In this case the value from <a class="reference internal" href="#const">const</a> will be produced. Some examples to illustrate this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">'c'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">'d'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">'d'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'XX --foo YY'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='XX', foo='YY')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'XX --foo'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='XX', foo='c')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">''</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='d', foo='d')</span> </pre></div> </div> <p>One of the more common uses of <tt class="docutils literal"><span class="pre">nargs='?'</span></tt> is to allow optional input and output files:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'infile'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'r'</span><span class="p">),</span> <span class="gp">... </span> <span class="n">default</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'outfile'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'w'</span><span class="p">),</span> <span class="gp">... </span> <span class="n">default</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'input.txt'</span><span class="p">,</span> <span class="s">'output.txt'</span><span class="p">])</span> <span class="go">Namespace(infile=<open file 'input.txt', mode 'r' at 0x...>,</span> <span class="go"> outfile=<open file 'output.txt', mode 'w' at 0x...>)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">Namespace(infile=<open file '<stdin>', mode 'r' at 0x...>,</span> <span class="go"> outfile=<open file '<stdout>', mode 'w' at 0x...>)</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'*'</span></tt>. All command-line arguments present are gathered into a list. Note that it generally doesn’t make much sense to have more than one positional argument with <tt class="docutils literal"><span class="pre">nargs='*'</span></tt>, but multiple optional arguments with <tt class="docutils literal"><span class="pre">nargs='*'</span></tt> is possible. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'*'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'*'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'baz'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'*'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'a b --foo x y --bar 1 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">'+'</span></tt>. Just like <tt class="docutils literal"><span class="pre">'*'</span></tt>, all command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn’t at least one command-line argument present. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'a b'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=['a', 'b'])</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">''</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">usage: PROG [-h] foo [foo ...]</span> <span class="go">PROG: error: too few arguments</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">argparse.REMAINDER</span></tt>. All the remaining command-line arguments are gathered into a list. This is commonly useful for command line utilities that dispatch to other command line utilities:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'command'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'args'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">REMAINDER</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo B cmd --arg1 XX ZZ'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(args=['--arg1', 'XX', 'ZZ'], command='cmd', foo='B')</span> </pre></div> </div> </li> </ul> <p>If the <tt class="docutils literal"><span class="pre">nargs</span></tt> keyword argument is not provided, the number of arguments consumed is determined by the <a class="reference internal" href="#action">action</a>. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced.</p> </div> <div class="section" id="const"> <h3>15.4.3.4. const<a class="headerlink" href="#const" title="Permalink to this headline">¶</a></h3> <p>The <tt class="docutils literal"><span class="pre">const</span></tt> argument of <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> is used to hold constant values that are not read from the command line but are required for the various <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> actions. The two most common uses of it are:</p> <ul class="simple"> <li>When <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> is called with <tt class="docutils literal"><span class="pre">action='store_const'</span></tt> or <tt class="docutils literal"><span class="pre">action='append_const'</span></tt>. These actions add the <tt class="docutils literal"><span class="pre">const</span></tt> value to one of the attributes of the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>. See the <a class="reference internal" href="#action">action</a> description for examples.</li> <li>When <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> is called with option strings (like <tt class="docutils literal"><span class="pre">-f</span></tt> or <tt class="docutils literal"><span class="pre">--foo</span></tt>) and <tt class="docutils literal"><span class="pre">nargs='?'</span></tt>. This creates an optional argument that can be followed by zero or one command-line arguments. When parsing the command line, if the option string is encountered with no command-line argument following it, the value of <tt class="docutils literal"><span class="pre">const</span></tt> will be assumed instead. See the <a class="reference internal" href="#nargs">nargs</a> description for examples.</li> </ul> <p>The <tt class="docutils literal"><span class="pre">const</span></tt> keyword argument defaults to <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </div> <div class="section" id="default"> <h3>15.4.3.5. default<a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3> <p>All optional arguments and some positional arguments may be omitted at the command line. The <tt class="docutils literal"><span class="pre">default</span></tt> keyword argument of <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>, whose value defaults to <tt class="docutils literal"><span class="pre">None</span></tt>, specifies what value should be used if the command-line argument is not present. For optional arguments, the <tt class="docutils literal"><span class="pre">default</span></tt> value is used when the option string was not present at the command line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo='2')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">''</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=42)</span> </pre></div> </div> <p>If the <tt class="docutils literal"><span class="pre">default</span></tt> value is a string, the parser parses the value as if it were a command-line argument. In particular, the parser applies any <a class="reference internal" href="#type">type</a> conversion argument, if provided, before setting the attribute on the <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> return value. Otherwise, the parser uses the value as is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--length'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">'10'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--width'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">10.5</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> <span class="go">Namespace(length=10, width=10.5)</span> </pre></div> </div> <p>For positional arguments with <a class="reference internal" href="#nargs">nargs</a> equal to <tt class="docutils literal"><span class="pre">?</span></tt> or <tt class="docutils literal"><span class="pre">*</span></tt>, the <tt class="docutils literal"><span class="pre">default</span></tt> value is used when no command-line argument was present:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'a'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo='a')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">''</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=42)</span> </pre></div> </div> <p>Providing <tt class="docutils literal"><span class="pre">default=argparse.SUPPRESS</span></tt> causes no attribute to be added if the command-line argument was not present.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">SUPPRESS</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">Namespace()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'1'</span><span class="p">])</span> <span class="go">Namespace(foo='1')</span> </pre></div> </div> </div> <div class="section" id="type"> <h3>15.4.3.6. type<a class="headerlink" href="#type" title="Permalink to this headline">¶</a></h3> <p>By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> objects read command-line arguments in as simple strings. However, quite often the command-line string should instead be interpreted as another type, like a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> or <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>. The <tt class="docutils literal"><span class="pre">type</span></tt> keyword argument of <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> allows any necessary type-checking and type conversions to be performed. Common built-in types and functions can be used directly as the value of the <tt class="docutils literal"><span class="pre">type</span></tt> argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">file</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'2 temp.txt'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar=<open file 'temp.txt', mode 'r' at 0x...>, foo=2)</span> </pre></div> </div> <p>See the section on the <a class="reference internal" href="#default">default</a> keyword argument for information on when the <tt class="docutils literal"><span class="pre">type</span></tt> argument is applied to default arguments.</p> <p>To ease the use of various types of files, the argparse module provides the factory FileType which takes the <tt class="docutils literal"><span class="pre">mode=</span></tt> and <tt class="docutils literal"><span class="pre">bufsize=</span></tt> arguments of the <tt class="docutils literal"><span class="pre">file</span></tt> object. For example, <tt class="docutils literal"><span class="pre">FileType('w')</span></tt> can be used to create a writable file:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'w'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'out.txt'</span><span class="p">])</span> <span class="go">Namespace(bar=<open file 'out.txt', mode 'w' at 0x...>)</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">type=</span></tt> can take any callable that takes a single string argument and returns the converted value:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">perfect_square</span><span class="p">(</span><span class="n">string</span><span class="p">):</span> <span class="gp">... </span> <span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">string</span><span class="p">)</span> <span class="gp">... </span> <span class="n">sqrt</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="n">sqrt</span> <span class="o">!=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sqrt</span><span class="p">):</span> <span class="gp">... </span> <span class="n">msg</span> <span class="o">=</span> <span class="s">"</span><span class="si">%r</span><span class="s"> is not a perfect square"</span> <span class="o">%</span> <span class="n">string</span> <span class="gp">... </span> <span class="k">raise</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentTypeError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">value</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">perfect_square</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'9'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=9)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'7'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">usage: PROG [-h] foo</span> <span class="go">PROG: error: argument foo: '7' is not a perfect square</span> </pre></div> </div> <p>The <a class="reference internal" href="#choices">choices</a> keyword argument may be more convenient for type checkers that simply check against a range of values:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="nb">xrange</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'7'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=7)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'11'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">usage: PROG [-h] {5,6,7,8,9}</span> <span class="go">PROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9)</span> </pre></div> </div> <p>See the <a class="reference internal" href="#choices">choices</a> section for more details.</p> </div> <div class="section" id="choices"> <h3>15.4.3.7. choices<a class="headerlink" href="#choices" title="Permalink to this headline">¶</a></h3> <p>Some command-line arguments should be selected from a restricted set of values. These can be handled by passing a container object as the <em>choices</em> keyword argument to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>. When the command line is parsed, argument values will be checked, and an error message will be displayed if the argument was not one of the acceptable values:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'game.py'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'move'</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s">'rock'</span><span class="p">,</span> <span class="s">'paper'</span><span class="p">,</span> <span class="s">'scissors'</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'rock'</span><span class="p">])</span> <span class="go">Namespace(move='rock')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'fire'</span><span class="p">])</span> <span class="go">usage: game.py [-h] {rock,paper,scissors}</span> <span class="go">game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',</span> <span class="go">'paper', 'scissors')</span> </pre></div> </div> <p>Note that inclusion in the <em>choices</em> container is checked after any <a class="reference internal" href="#type">type</a> conversions have been performed, so the type of the objects in the <em>choices</em> container should match the <a class="reference internal" href="#type">type</a> specified:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'doors.py'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'door'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'3'</span><span class="p">]))</span> <span class="go">Namespace(door=3)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'4'</span><span class="p">])</span> <span class="go">usage: doors.py [-h] {1,2,3}</span> <span class="go">doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)</span> </pre></div> </div> <p>Any object that supports the <tt class="docutils literal"><span class="pre">in</span></tt> operator can be passed as the <em>choices</em> value, so <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> objects, <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> objects, custom containers, etc. are all supported.</p> </div> <div class="section" id="required"> <h3>15.4.3.8. required<a class="headerlink" href="#required" title="Permalink to this headline">¶</a></h3> <p>In general, the <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> module assumes that flags like <tt class="docutils literal"><span class="pre">-f</span></tt> and <tt class="docutils literal"><span class="pre">--bar</span></tt> indicate <em>optional</em> arguments, which can always be omitted at the command line. To make an option <em>required</em>, <tt class="docutils literal"><span class="pre">True</span></tt> can be specified for the <tt class="docutils literal"><span class="pre">required=</span></tt> keyword argument to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'BAR'</span><span class="p">])</span> <span class="go">Namespace(foo='BAR')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">usage: argparse.py [-h] [--foo FOO]</span> <span class="go">argparse.py: error: option --foo is required</span> </pre></div> </div> <p>As the example shows, if an option is marked as <tt class="docutils literal"><span class="pre">required</span></tt>, <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will report an error if that option is not present at the command line.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Required options are generally considered bad form because users expect <em>options</em> to be <em>optional</em>, and thus they should be avoided when possible.</p> </div> </div> <div class="section" id="help"> <h3>15.4.3.9. help<a class="headerlink" href="#help" title="Permalink to this headline">¶</a></h3> <p>The <tt class="docutils literal"><span class="pre">help</span></tt> value is a string containing a brief description of the argument. When a user requests help (usually by using <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> at the command line), these <tt class="docutils literal"><span class="pre">help</span></tt> descriptions will be displayed with each argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'frobble'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'foo the bars before frobbling'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'one of the bars to be frobbled'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-h'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">usage: frobble [-h] [--foo] bar [bar ...]</span> <span class="go">positional arguments:</span> <span class="go"> bar one of the bars to be frobbled</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo foo the bars before frobbling</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">help</span></tt> strings can include various format specifiers to avoid repetition of things like the program name or the argument <a class="reference internal" href="#default">default</a>. The available specifiers include the program name, <tt class="docutils literal"><span class="pre">%(prog)s</span></tt> and most keyword arguments to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>, e.g. <tt class="docutils literal"><span class="pre">%(default)s</span></tt>, <tt class="docutils literal"><span class="pre">%(type)s</span></tt>, etc.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'frobble'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'the bar to </span><span class="si">%(prog)s</span><span class="s"> (default: </span><span class="si">%(default)s</span><span class="s">)'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: frobble [-h] [bar]</span> <span class="go">positional arguments:</span> <span class="go"> bar the bar to frobble (default: 42)</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> <p><a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> supports silencing the help entry for certain options, by setting the <tt class="docutils literal"><span class="pre">help</span></tt> value to <tt class="docutils literal"><span class="pre">argparse.SUPPRESS</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'frobble'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">SUPPRESS</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: frobble [-h]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> </pre></div> </div> </div> <div class="section" id="metavar"> <h3>15.4.3.10. metavar<a class="headerlink" href="#metavar" title="Permalink to this headline">¶</a></h3> <p>When <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> generates help messages, it needs some way to refer to each expected argument. By default, ArgumentParser objects use the <a class="reference internal" href="#dest">dest</a> value as the “name” of each object. By default, for positional argument actions, the <a class="reference internal" href="#dest">dest</a> value is used directly, and for optional argument actions, the <a class="reference internal" href="#dest">dest</a> value is uppercased. So, a single positional argument with <tt class="docutils literal"><span class="pre">dest='bar'</span></tt> will be referred to as <tt class="docutils literal"><span class="pre">bar</span></tt>. A single optional argument <tt class="docutils literal"><span class="pre">--foo</span></tt> that should be followed by a single command-line argument will be referred to as <tt class="docutils literal"><span class="pre">FOO</span></tt>. An example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'X --foo Y'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='X', foo='Y')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: [-h] [--foo FOO] bar</span> <span class="go">positional arguments:</span> <span class="go"> bar</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo FOO</span> </pre></div> </div> <p>An alternative name can be specified with <tt class="docutils literal"><span class="pre">metavar</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'YYY'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'XXX'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'X --foo Y'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='X', foo='Y')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: [-h] [--foo YYY] XXX</span> <span class="go">positional arguments:</span> <span class="go"> XXX</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo YYY</span> </pre></div> </div> <p>Note that <tt class="docutils literal"><span class="pre">metavar</span></tt> only changes the <em>displayed</em> name - the name of the attribute on the <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> object is still determined by the <a class="reference internal" href="#dest">dest</a> value.</p> <p>Different values of <tt class="docutils literal"><span class="pre">nargs</span></tt> may cause the metavar to be used multiple times. Providing a tuple to <tt class="docutils literal"><span class="pre">metavar</span></tt> specifies a different display for each of the arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="s">'baz'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [-h] [-x X X] [--foo bar baz]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> -x X X</span> <span class="go"> --foo bar baz</span> </pre></div> </div> </div> <div class="section" id="dest"> <h3>15.4.3.11. dest<a class="headerlink" href="#dest" title="Permalink to this headline">¶</a></h3> <p>Most <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> actions add some value as an attribute of the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>. The name of this attribute is determined by the <tt class="docutils literal"><span class="pre">dest</span></tt> keyword argument of <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>. For positional argument actions, <tt class="docutils literal"><span class="pre">dest</span></tt> is normally supplied as the first argument to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'XXX'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='XXX')</span> </pre></div> </div> <p>For optional argument actions, the value of <tt class="docutils literal"><span class="pre">dest</span></tt> is normally inferred from the option strings. <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> generates the value of <tt class="docutils literal"><span class="pre">dest</span></tt> by taking the first long option string and stripping away the initial <tt class="docutils literal"><span class="pre">--</span></tt> string. If no long option strings were supplied, <tt class="docutils literal"><span class="pre">dest</span></tt> will be derived from the first short option string by stripping the initial <tt class="docutils literal"><span class="pre">-</span></tt> character. Any internal <tt class="docutils literal"><span class="pre">-</span></tt> characters will be converted to <tt class="docutils literal"><span class="pre">_</span></tt> characters to make sure the string is a valid attribute name. The examples below illustrate this behavior:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-f'</span><span class="p">,</span> <span class="s">'--foo-bar'</span><span class="p">,</span> <span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">,</span> <span class="s">'-y'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-f 1 -x 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo_bar='1', x='2')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo 1 -y 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo_bar='1', x='2')</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">dest</span></tt> allows a custom attribute name to be provided:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo XXX'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bar='XXX')</span> </pre></div> </div> </div> </div> <div class="section" id="the-parse-args-method"> <h2>15.4.4. The parse_args() method<a class="headerlink" href="#the-parse-args-method" title="Permalink to this headline">¶</a></h2> <dl class="method"> <dt id="argparse.ArgumentParser.parse_args"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">parse_args</tt><big>(</big><em>args=None</em>, <em>namespace=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.parse_args" title="Permalink to this definition">¶</a></dt> <dd><p>Convert argument strings to objects and assign them as attributes of the namespace. Return the populated namespace.</p> <p>Previous calls to <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> determine exactly what objects are created and how they are assigned. See the documentation for <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> for details.</p> <p>By default, the argument strings are taken from <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><tt class="xref py py-data docutils literal"><span class="pre">sys.argv</span></tt></a>, and a new empty <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object is created for the attributes.</p> </dd></dl> <div class="section" id="option-value-syntax"> <h3>15.4.4.1. Option value syntax<a class="headerlink" href="#option-value-syntax" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> method supports several ways of specifying the value of an option (if it takes one). In the simplest case, the option and its value are passed as two separate arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-x X'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=None, x='X')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo FOO'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo='FOO', x=None)</span> </pre></div> </div> <p>For long options (options with names longer than a single character), the option and value can also be passed as a single command-line argument, using <tt class="docutils literal"><span class="pre">=</span></tt> to separate them:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'--foo=FOO'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo='FOO', x=None)</span> </pre></div> </div> <p>For short options (options only one character long), the option and its value can be concatenated:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-xX'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(foo=None, x='X')</span> </pre></div> </div> <p>Several short options can be joined together, using only a single <tt class="docutils literal"><span class="pre">-</span></tt> prefix, as long as only the last option (or none of them) requires a value:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-y'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-z'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-xyzZ'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(x=True, y=True, z='Z')</span> </pre></div> </div> </div> <div class="section" id="invalid-arguments"> <h3>15.4.4.2. Invalid arguments<a class="headerlink" href="#invalid-arguments" title="Permalink to this headline">¶</a></h3> <p>While parsing the command line, <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> checks for a variety of errors, including ambiguous options, invalid types, invalid options, wrong number of positional arguments, etc. When it encounters such an error, it exits and prints the error along with a usage message:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">)</span> <span class="gp">>>> </span><span class="c"># invalid type</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'spam'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [--foo FOO] [bar]</span> <span class="go">PROG: error: argument --foo: invalid int value: 'spam'</span> <span class="gp">>>> </span><span class="c"># invalid option</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--bar'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [--foo FOO] [bar]</span> <span class="go">PROG: error: no such option: --bar</span> <span class="gp">>>> </span><span class="c"># wrong number of arguments</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'spam'</span><span class="p">,</span> <span class="s">'badger'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [--foo FOO] [bar]</span> <span class="go">PROG: error: extra arguments found: badger</span> </pre></div> </div> </div> <div class="section" id="arguments-containing"> <h3>15.4.4.3. Arguments containing <tt class="docutils literal"><span class="pre">-</span></tt><a class="headerlink" href="#arguments-containing" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> method attempts to give errors whenever the user has clearly made a mistake, but some situations are inherently ambiguous. For example, the command-line argument <tt class="docutils literal"><span class="pre">-1</span></tt> could either be an attempt to specify an option or an attempt to provide a positional argument. The <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> method is cautious here: positional arguments may only begin with <tt class="docutils literal"><span class="pre">-</span></tt> if they look like negative numbers and there are no options in the parser that look like negative numbers:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">)</span> <span class="gp">>>> </span><span class="c"># no negative number options, so -1 is a positional argument</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-x'</span><span class="p">,</span> <span class="s">'-1'</span><span class="p">])</span> <span class="go">Namespace(foo=None, x='-1')</span> <span class="gp">>>> </span><span class="c"># no negative number options, so -1 and -5 are positional arguments</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-x'</span><span class="p">,</span> <span class="s">'-1'</span><span class="p">,</span> <span class="s">'-5'</span><span class="p">])</span> <span class="go">Namespace(foo='-5', x='-1')</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-1'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'one'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">)</span> <span class="gp">>>> </span><span class="c"># negative number options present, so -1 is an option</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-1'</span><span class="p">,</span> <span class="s">'X'</span><span class="p">])</span> <span class="go">Namespace(foo=None, one='X')</span> <span class="gp">>>> </span><span class="c"># negative number options present, so -2 is an option</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-2'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [-1 ONE] [foo]</span> <span class="go">PROG: error: no such option: -2</span> <span class="gp">>>> </span><span class="c"># negative number options present, so both -1s are options</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-1'</span><span class="p">,</span> <span class="s">'-1'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [-1 ONE] [foo]</span> <span class="go">PROG: error: argument -1: expected one argument</span> </pre></div> </div> <p>If you have positional arguments that must begin with <tt class="docutils literal"><span class="pre">-</span></tt> and don’t look like negative numbers, you can insert the pseudo-argument <tt class="docutils literal"><span class="pre">'--'</span></tt> which tells <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> that everything after that is a positional argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--'</span><span class="p">,</span> <span class="s">'-f'</span><span class="p">])</span> <span class="go">Namespace(foo='-f', one=None)</span> </pre></div> </div> </div> <div class="section" id="argument-abbreviations"> <h3>15.4.4.4. Argument abbreviations<a class="headerlink" href="#argument-abbreviations" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> method allows long options to be abbreviated if the abbreviation is unambiguous:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-bacon'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-badger'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-bac MMM'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bacon='MMM', badger=None)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-bad WOOD'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(bacon=None, badger='WOOD')</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'-ba BA'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">usage: PROG [-h] [-bacon BACON] [-badger BADGER]</span> <span class="go">PROG: error: ambiguous option: -ba could match -badger, -bacon</span> </pre></div> </div> <p>An error is produced for arguments that could produce more than one options.</p> </div> <div class="section" id="beyond-sys-argv"> <h3>15.4.4.5. Beyond <tt class="docutils literal"><span class="pre">sys.argv</span></tt><a class="headerlink" href="#beyond-sys-argv" title="Permalink to this headline">¶</a></h3> <p>Sometimes it may be useful to have an ArgumentParser parse arguments other than those of <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><tt class="xref py py-data docutils literal"><span class="pre">sys.argv</span></tt></a>. This can be accomplished by passing a list of strings to <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a>. This is useful for testing at the interactive prompt:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span> <span class="gp">... </span> <span class="s">'integers'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'int'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="nb">xrange</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="gp">... </span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'an integer in the range 0..9'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span> <span class="gp">... </span> <span class="s">'--sum'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'accumulate'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_const'</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="nb">sum</span><span class="p">,</span> <span class="gp">... </span> <span class="n">default</span><span class="o">=</span><span class="nb">max</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'sum the integers (default: find the max)'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'1'</span><span class="p">,</span> <span class="s">'2'</span><span class="p">,</span> <span class="s">'3'</span><span class="p">,</span> <span class="s">'4'</span><span class="p">])</span> <span class="go">Namespace(accumulate=<built-in function max>, integers=[1, 2, 3, 4])</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'1 2 3 4 --sum'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="go">Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])</span> </pre></div> </div> </div> <div class="section" id="the-namespace-object"> <h3>15.4.4.6. The Namespace object<a class="headerlink" href="#the-namespace-object" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="argparse.Namespace"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">Namespace</tt><a class="headerlink" href="#argparse.Namespace" title="Permalink to this definition">¶</a></dt> <dd><p>Simple class used by default by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> to create an object holding attributes and return it.</p> </dd></dl> <p>This class is deliberately simple, just an <a class="reference internal" href="functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> subclass with a readable string representation. If you prefer to have dict-like view of the attributes, you can use the standard Python idiom, <a class="reference internal" href="functions.html#vars" title="vars"><tt class="xref py py-func docutils literal"><span class="pre">vars()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'BAR'</span><span class="p">])</span> <span class="gp">>>> </span><span class="nb">vars</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="go">{'foo': 'BAR'}</span> </pre></div> </div> <p>It may also be useful to have an <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> assign attributes to an already existing object, rather than a new <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object. This can be achieved by specifying the <tt class="docutils literal"><span class="pre">namespace=</span></tt> keyword argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'BAR'</span><span class="p">],</span> <span class="n">namespace</span><span class="o">=</span><span class="n">c</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">foo</span> <span class="go">'BAR'</span> </pre></div> </div> </div> </div> <div class="section" id="other-utilities"> <h2>15.4.5. Other utilities<a class="headerlink" href="#other-utilities" title="Permalink to this headline">¶</a></h2> <div class="section" id="sub-commands"> <h3>15.4.5.1. Sub-commands<a class="headerlink" href="#sub-commands" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.add_subparsers"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">add_subparsers</tt><big>(</big><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.add_subparsers" title="Permalink to this definition">¶</a></dt> <dd><p>Many programs split up their functionality into a number of sub-commands, for example, the <tt class="docutils literal"><span class="pre">svn</span></tt> program can invoke sub-commands like <tt class="docutils literal"><span class="pre">svn</span> <span class="pre">checkout</span></tt>, <tt class="docutils literal"><span class="pre">svn</span> <span class="pre">update</span></tt>, and <tt class="docutils literal"><span class="pre">svn</span> <span class="pre">commit</span></tt>. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments. <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> supports the creation of such sub-commands with the <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> method. The <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> method is normally called with no arguments and returns a special action object. This object has a single method, <tt class="xref py py-meth docutils literal"><span class="pre">add_parser()</span></tt>, which takes a command name and any <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> constructor arguments, and returns an <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> object that can be modified as usual.</p> <p>Some example usage:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># create the top-level parser</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">(</span><span class="n">help</span><span class="o">=</span><span class="s">'sub-command help'</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># create the parser for the "a" command</span> <span class="gp">>>> </span><span class="n">parser_a</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'a'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'a help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_a</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'bar help'</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># create the parser for the "b" command</span> <span class="gp">>>> </span><span class="n">parser_b</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'b'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'b help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_b</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--baz'</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="s">'XYZ'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'baz help'</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># parse some argument lists</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'12'</span><span class="p">])</span> <span class="go">Namespace(bar=12, foo=False)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'--baz'</span><span class="p">,</span> <span class="s">'Z'</span><span class="p">])</span> <span class="go">Namespace(baz='Z', foo=True)</span> </pre></div> </div> <p>Note that the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will only contain attributes for the main parser and the subparser that was selected by the command line (and not any other subparsers). So in the example above, when the <tt class="docutils literal"><span class="pre">a</span></tt> command is specified, only the <tt class="docutils literal"><span class="pre">foo</span></tt> and <tt class="docutils literal"><span class="pre">bar</span></tt> attributes are present, and when the <tt class="docutils literal"><span class="pre">b</span></tt> command is specified, only the <tt class="docutils literal"><span class="pre">foo</span></tt> and <tt class="docutils literal"><span class="pre">baz</span></tt> attributes are present.</p> <p>Similarly, when a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages. (A help message for each subparser command, however, can be given by supplying the <tt class="docutils literal"><span class="pre">help=</span></tt> argument to <tt class="xref py py-meth docutils literal"><span class="pre">add_parser()</span></tt> as above.)</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--help'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [--foo] {a,b} ...</span> <span class="go">positional arguments:</span> <span class="go"> {a,b} sub-command help</span> <span class="go"> a a help</span> <span class="go"> b b help</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --foo foo help</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'--help'</span><span class="p">])</span> <span class="go">usage: PROG a [-h] bar</span> <span class="go">positional arguments:</span> <span class="go"> bar bar help</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'b'</span><span class="p">,</span> <span class="s">'--help'</span><span class="p">])</span> <span class="go">usage: PROG b [-h] [--baz {X,Y,Z}]</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go"> --baz {X,Y,Z} baz help</span> </pre></div> </div> <p>The <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> method also supports <tt class="docutils literal"><span class="pre">title</span></tt> and <tt class="docutils literal"><span class="pre">description</span></tt> keyword arguments. When either is present, the subparser’s commands will appear in their own group in the help output. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">'subcommands'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="s">'valid subcommands'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">help</span><span class="o">=</span><span class="s">'additional help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-h'</span><span class="p">])</span> <span class="go">usage: [-h] {foo,bar} ...</span> <span class="go">optional arguments:</span> <span class="go"> -h, --help show this help message and exit</span> <span class="go">subcommands:</span> <span class="go"> valid subcommands</span> <span class="go"> {foo,bar} additional help</span> </pre></div> </div> <p>One particularly effective way of handling sub-commands is to combine the use of the <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> method with calls to <a class="reference internal" href="#argparse.ArgumentParser.set_defaults" title="argparse.ArgumentParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a> so that each subparser knows which Python function it should execute. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># sub-command functions</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">args</span><span class="p">):</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">args</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">args</span><span class="o">.</span><span class="n">y</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">args</span><span class="p">):</span> <span class="gp">... </span> <span class="k">print</span> <span class="s">'((</span><span class="si">%s</span><span class="s">))'</span> <span class="o">%</span> <span class="n">args</span><span class="o">.</span><span class="n">z</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="c"># create the top-level parser</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">()</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># create the parser for the "foo" command</span> <span class="gp">>>> </span><span class="n">parser_foo</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'foo'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_foo</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_foo</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'y'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_foo</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">func</span><span class="o">=</span><span class="n">foo</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># create the parser for the "bar" command</span> <span class="gp">>>> </span><span class="n">parser_bar</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_bar</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'z'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser_bar</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">func</span><span class="o">=</span><span class="n">bar</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># parse the args and call whatever function was selected</span> <span class="gp">>>> </span><span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'foo 1 -x 2'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">args</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="go">2.0</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c"># parse the args and call whatever function was selected</span> <span class="gp">>>> </span><span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">'bar XYZYX'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">args</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="go">((XYZYX))</span> </pre></div> </div> <p>This way, you can let <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> do the job of calling the appropriate function after argument parsing is complete. Associating functions with actions like this is typically the easiest way to handle the different actions for each of your subparsers. However, if it is necessary to check the name of the subparser that was invoked, the <tt class="docutils literal"><span class="pre">dest</span></tt> keyword argument to the <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> call will work:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">(</span><span class="n">dest</span><span class="o">=</span><span class="s">'subparser_name'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparser1</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'1'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparser1</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-x'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparser2</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s">'2'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">subparser2</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'y'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'2'</span><span class="p">,</span> <span class="s">'frobble'</span><span class="p">])</span> <span class="go">Namespace(subparser_name='2', y='frobble')</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="filetype-objects"> <h3>15.4.5.2. FileType objects<a class="headerlink" href="#filetype-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="argparse.FileType"> <em class="property">class </em><tt class="descclassname">argparse.</tt><tt class="descname">FileType</tt><big>(</big><em>mode='r'</em>, <em>bufsize=None</em><big>)</big><a class="headerlink" href="#argparse.FileType" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#argparse.FileType" title="argparse.FileType"><tt class="xref py py-class docutils literal"><span class="pre">FileType</span></tt></a> factory creates objects that can be passed to the type argument of <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">ArgumentParser.add_argument()</span></tt></a>. Arguments that have <a class="reference internal" href="#argparse.FileType" title="argparse.FileType"><tt class="xref py py-class docutils literal"><span class="pre">FileType</span></tt></a> objects as their type will open command-line arguments as files with the requested modes and buffer sizes:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--output'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'wb'</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--output'</span><span class="p">,</span> <span class="s">'out'</span><span class="p">])</span> <span class="go">Namespace(output=<open file 'out', mode 'wb' at 0x...>)</span> </pre></div> </div> <p>FileType objects understand the pseudo-argument <tt class="docutils literal"><span class="pre">'-'</span></tt> and automatically convert this into <tt class="docutils literal"><span class="pre">sys.stdin</span></tt> for readable <a class="reference internal" href="#argparse.FileType" title="argparse.FileType"><tt class="xref py py-class docutils literal"><span class="pre">FileType</span></tt></a> objects and <tt class="docutils literal"><span class="pre">sys.stdout</span></tt> for writable <a class="reference internal" href="#argparse.FileType" title="argparse.FileType"><tt class="xref py py-class docutils literal"><span class="pre">FileType</span></tt></a> objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'infile'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'r'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'-'</span><span class="p">])</span> <span class="go">Namespace(infile=<open file '<stdin>', mode 'r' at 0x...>)</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="argument-groups"> <h3>15.4.5.3. Argument groups<a class="headerlink" href="#argument-groups" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.add_argument_group"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">add_argument_group</tt><big>(</big><em>title=None</em>, <em>description=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.add_argument_group" title="Permalink to this definition">¶</a></dt> <dd><p>By default, <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> groups command-line arguments into “positional arguments” and “optional arguments” when displaying help messages. When there is a better conceptual grouping of arguments than this default one, appropriate groups can be created using the <a class="reference internal" href="#argparse.ArgumentParser.add_argument_group" title="argparse.ArgumentParser.add_argument_group"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument_group()</span></tt></a> method:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">add_help</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument_group</span><span class="p">(</span><span class="s">'group'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'bar help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [--foo FOO] bar</span> <span class="go">group:</span> <span class="go"> bar bar help</span> <span class="go"> --foo FOO foo help</span> </pre></div> </div> <p>The <a class="reference internal" href="#argparse.ArgumentParser.add_argument_group" title="argparse.ArgumentParser.add_argument_group"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument_group()</span></tt></a> method returns an argument group object which has an <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> method just like a regular <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>. When an argument is added to the group, the parser treats it just like a normal argument, but displays the argument in a separate group for help messages. The <a class="reference internal" href="#argparse.ArgumentParser.add_argument_group" title="argparse.ArgumentParser.add_argument_group"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument_group()</span></tt></a> method accepts <em>title</em> and <em>description</em> arguments which can be used to customize this display:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">,</span> <span class="n">add_help</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group1</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument_group</span><span class="p">(</span><span class="s">'group1'</span><span class="p">,</span> <span class="s">'group1 description'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group1</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'foo help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group2</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_argument_group</span><span class="p">(</span><span class="s">'group2'</span><span class="p">,</span> <span class="s">'group2 description'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group2</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'bar help'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="go">usage: PROG [--bar BAR] foo</span> <span class="go">group1:</span> <span class="go"> group1 description</span> <span class="go"> foo foo help</span> <span class="go">group2:</span> <span class="go"> group2 description</span> <span class="go"> --bar BAR bar help</span> </pre></div> </div> <p>Note that any arguments not in your user-defined groups will end up back in the usual “positional arguments” and “optional arguments” sections.</p> </dd></dl> </div> <div class="section" id="mutual-exclusion"> <h3>15.4.5.4. Mutual exclusion<a class="headerlink" href="#mutual-exclusion" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.add_mutually_exclusive_group"> <tt class="descclassname">argparse.</tt><tt class="descname">add_mutually_exclusive_group</tt><big>(</big><em>required=False</em><big>)</big><a class="headerlink" href="#argparse.add_mutually_exclusive_group" title="Permalink to this definition">¶</a></dt> <dd><p>Create a mutually exclusive group. <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> will make sure that only one of the arguments in the mutually exclusive group was present on the command line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_mutually_exclusive_group</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_false'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">])</span> <span class="go">Namespace(bar=True, foo=True)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--bar'</span><span class="p">])</span> <span class="go">Namespace(bar=False, foo=False)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'--bar'</span><span class="p">])</span> <span class="go">usage: PROG [-h] [--foo | --bar]</span> <span class="go">PROG: error: argument --bar: not allowed with argument --foo</span> </pre></div> </div> <p>The <a class="reference internal" href="#argparse.add_mutually_exclusive_group" title="argparse.add_mutually_exclusive_group"><tt class="xref py py-meth docutils literal"><span class="pre">add_mutually_exclusive_group()</span></tt></a> method also accepts a <em>required</em> argument, to indicate that at least one of the mutually exclusive arguments is required:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s">'PROG'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_mutually_exclusive_group</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">group</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--bar'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_false'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">usage: PROG [-h] (--foo | --bar)</span> <span class="go">PROG: error: one of the arguments --foo --bar is required</span> </pre></div> </div> <p>Note that currently mutually exclusive argument groups do not support the <em>title</em> and <em>description</em> arguments of <a class="reference internal" href="#argparse.ArgumentParser.add_argument_group" title="argparse.ArgumentParser.add_argument_group"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument_group()</span></tt></a>.</p> </dd></dl> </div> <div class="section" id="parser-defaults"> <h3>15.4.5.5. Parser defaults<a class="headerlink" href="#parser-defaults" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.set_defaults"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">set_defaults</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.set_defaults" title="Permalink to this definition">¶</a></dt> <dd><p>Most of the time, the attributes of the object returned by <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will be fully determined by inspecting the command-line arguments and the argument actions. <a class="reference internal" href="#argparse.ArgumentParser.set_defaults" title="argparse.ArgumentParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a> allows some additional attributes that are determined without any inspection of the command line to be added:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">bar</span><span class="o">=</span><span class="mi">42</span><span class="p">,</span> <span class="n">baz</span><span class="o">=</span><span class="s">'badger'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'736'</span><span class="p">])</span> <span class="go">Namespace(bar=42, baz='badger', foo=736)</span> </pre></div> </div> <p>Note that parser-level defaults always override argument-level defaults:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">foo</span><span class="o">=</span><span class="s">'spam'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([])</span> <span class="go">Namespace(foo='spam')</span> </pre></div> </div> <p>Parser-level defaults can be particularly useful when working with multiple parsers. See the <a class="reference internal" href="#argparse.ArgumentParser.add_subparsers" title="argparse.ArgumentParser.add_subparsers"><tt class="xref py py-meth docutils literal"><span class="pre">add_subparsers()</span></tt></a> method for an example of this type.</p> </dd></dl> <dl class="method"> <dt id="argparse.ArgumentParser.get_default"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">get_default</tt><big>(</big><em>dest</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.get_default" title="Permalink to this definition">¶</a></dt> <dd><p>Get the default value for a namespace attribute, as set by either <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">add_argument()</span></tt></a> or by <a class="reference internal" href="#argparse.ArgumentParser.set_defaults" title="argparse.ArgumentParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">'badger'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">get_default</span><span class="p">(</span><span class="s">'foo'</span><span class="p">)</span> <span class="go">'badger'</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="printing-help"> <h3>15.4.5.6. Printing help<a class="headerlink" href="#printing-help" title="Permalink to this headline">¶</a></h3> <p>In most typical applications, <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> will take care of formatting and printing any usage or error messages. However, several formatting methods are available:</p> <dl class="method"> <dt id="argparse.ArgumentParser.print_usage"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">print_usage</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.print_usage" title="Permalink to this definition">¶</a></dt> <dd><p>Print a brief description of how the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> should be invoked on the command line. If <em>file</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><tt class="xref py py-data docutils literal"><span class="pre">sys.stdout</span></tt></a> is assumed.</p> </dd></dl> <dl class="method"> <dt id="argparse.ArgumentParser.print_help"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">print_help</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.print_help" title="Permalink to this definition">¶</a></dt> <dd><p>Print a help message, including the program usage and information about the arguments registered with the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>. If <em>file</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><tt class="xref py py-data docutils literal"><span class="pre">sys.stdout</span></tt></a> is assumed.</p> </dd></dl> <p>There are also variants of these methods that simply return a string instead of printing it:</p> <dl class="method"> <dt id="argparse.ArgumentParser.format_usage"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">format_usage</tt><big>(</big><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.format_usage" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string containing a brief description of how the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> should be invoked on the command line.</p> </dd></dl> <dl class="method"> <dt id="argparse.ArgumentParser.format_help"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">format_help</tt><big>(</big><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.format_help" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string containing a help message, including the program usage and information about the arguments registered with the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a>.</p> </dd></dl> </div> <div class="section" id="partial-parsing"> <h3>15.4.5.7. Partial parsing<a class="headerlink" href="#partial-parsing" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.parse_known_args"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">parse_known_args</tt><big>(</big><em>args=None</em>, <em>namespace=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.parse_known_args" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Sometimes a script may only parse a few of the command-line arguments, passing the remaining arguments on to another script or program. In these cases, the <a class="reference internal" href="#argparse.ArgumentParser.parse_known_args" title="argparse.ArgumentParser.parse_known_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_known_args()</span></tt></a> method can be useful. It works much like <a class="reference internal" href="#argparse.ArgumentParser.parse_args" title="argparse.ArgumentParser.parse_args"><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt></a> except that it does not produce an error when extra arguments are present. Instead, it returns a two item tuple containing the populated namespace and the list of remaining argument strings.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--foo'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">'store_true'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_known_args</span><span class="p">([</span><span class="s">'--foo'</span><span class="p">,</span> <span class="s">'--badger'</span><span class="p">,</span> <span class="s">'BAR'</span><span class="p">,</span> <span class="s">'spam'</span><span class="p">])</span> <span class="go">(Namespace(bar='BAR', foo=True), ['--badger', 'spam'])</span> </pre></div> </div> </div> <div class="section" id="customizing-file-parsing"> <h3>15.4.5.8. Customizing file parsing<a class="headerlink" href="#customizing-file-parsing" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.convert_arg_line_to_args"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">convert_arg_line_to_args</tt><big>(</big><em>arg_line</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.convert_arg_line_to_args" title="Permalink to this definition">¶</a></dt> <dd><p>Arguments that are read from a file (see the <em>fromfile_prefix_chars</em> keyword argument to the <a class="reference internal" href="#argparse.ArgumentParser" title="argparse.ArgumentParser"><tt class="xref py py-class docutils literal"><span class="pre">ArgumentParser</span></tt></a> constructor) are read one argument per line. <a class="reference internal" href="#argparse.ArgumentParser.convert_arg_line_to_args" title="argparse.ArgumentParser.convert_arg_line_to_args"><tt class="xref py py-meth docutils literal"><span class="pre">convert_arg_line_to_args()</span></tt></a> can be overriden for fancier reading.</p> <p>This method takes a single argument <em>arg_line</em> which is a string read from the argument file. It returns a list of arguments parsed from this string. The method is called once per line read from the argument file, in order.</p> <p>A useful override of this method is one that treats each space-separated word as an argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">convert_arg_line_to_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg_line</span><span class="p">):</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">arg_line</span><span class="o">.</span><span class="n">split</span><span class="p">():</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">arg</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span> <span class="k">continue</span> <span class="k">yield</span> <span class="n">arg</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="exiting-methods"> <h3>15.4.5.9. Exiting methods<a class="headerlink" href="#exiting-methods" title="Permalink to this headline">¶</a></h3> <dl class="method"> <dt id="argparse.ArgumentParser.exit"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">exit</tt><big>(</big><em>status=0</em>, <em>message=None</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.exit" title="Permalink to this definition">¶</a></dt> <dd><p>This method terminates the program, exiting with the specified <em>status</em> and, if given, it prints a <em>message</em> before that.</p> </dd></dl> <dl class="method"> <dt id="argparse.ArgumentParser.error"> <tt class="descclassname">ArgumentParser.</tt><tt class="descname">error</tt><big>(</big><em>message</em><big>)</big><a class="headerlink" href="#argparse.ArgumentParser.error" title="Permalink to this definition">¶</a></dt> <dd><p>This method prints a usage message including the <em>message</em> to the standard error and terminates the program with a status code of 2.</p> </dd></dl> </div> </div> <div class="section" id="upgrading-optparse-code"> <span id="argparse-from-optparse"></span><h2>15.4.6. Upgrading optparse code<a class="headerlink" href="#upgrading-optparse-code" title="Permalink to this headline">¶</a></h2> <p>Originally, the <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> module had attempted to maintain compatibility with <a class="reference internal" href="optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>. However, <a class="reference internal" href="optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> was difficult to extend transparently, particularly with the changes required to support the new <tt class="docutils literal"><span class="pre">nargs=</span></tt> specifiers and better usage messages. When most everything in <a class="reference internal" href="optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> had either been copy-pasted over or monkey-patched, it no longer seemed practical to try to maintain the backwards compatibility.</p> <p>A partial upgrade path from <a class="reference internal" href="optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a>:</p> <ul class="simple"> <li>Replace all <a class="reference internal" href="optparse.html#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">optparse.OptionParser.add_option()</span></tt></a> calls with <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">ArgumentParser.add_argument()</span></tt></a> calls.</li> <li>Replace <tt class="docutils literal"><span class="pre">(options,</span> <span class="pre">args)</span> <span class="pre">=</span> <span class="pre">parser.parse_args()</span></tt> with <tt class="docutils literal"><span class="pre">args</span> <span class="pre">=</span> <span class="pre">parser.parse_args()</span></tt> and add additional <a class="reference internal" href="#argparse.ArgumentParser.add_argument" title="argparse.ArgumentParser.add_argument"><tt class="xref py py-meth docutils literal"><span class="pre">ArgumentParser.add_argument()</span></tt></a> calls for the positional arguments. Keep in mind that what was previously called <tt class="docutils literal"><span class="pre">options</span></tt>, now in <a class="reference internal" href="#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> context is called <tt class="docutils literal"><span class="pre">args</span></tt>.</li> <li>Replace callback actions and the <tt class="docutils literal"><span class="pre">callback_*</span></tt> keyword arguments with <tt class="docutils literal"><span class="pre">type</span></tt> or <tt class="docutils literal"><span class="pre">action</span></tt> arguments.</li> <li>Replace string names for <tt class="docutils literal"><span class="pre">type</span></tt> keyword arguments with the corresponding type objects (e.g. int, float, complex, etc).</li> <li>Replace <tt class="xref py py-class docutils literal"><span class="pre">optparse.Values</span></tt> with <a class="reference internal" href="#argparse.Namespace" title="argparse.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> and <tt class="xref py py-exc docutils literal"><span class="pre">optparse.OptionError</span></tt> and <tt class="xref py py-exc docutils literal"><span class="pre">optparse.OptionValueError</span></tt> with <tt class="xref py py-exc docutils literal"><span class="pre">ArgumentError</span></tt>.</li> <li>Replace strings with implicit arguments such as <tt class="docutils literal"><span class="pre">%default</span></tt> or <tt class="docutils literal"><span class="pre">%prog</span></tt> with the standard Python syntax to use dictionaries to format strings, that is, <tt class="docutils literal"><span class="pre">%(default)s</span></tt> and <tt class="docutils literal"><span class="pre">%(prog)s</span></tt>.</li> <li>Replace the OptionParser constructor <tt class="docutils literal"><span class="pre">version</span></tt> argument with a call to <tt class="docutils literal"><span class="pre">parser.add_argument('--version',</span> <span class="pre">action='version',</span> <span class="pre">version='<the</span> <span class="pre">version>')</span></tt></li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">15.4. <tt class="docutils literal"><span class="pre">argparse</span></tt> — Parser for command-line options, arguments and sub-commands</a><ul> <li><a class="reference internal" href="#example">15.4.1. Example</a><ul> <li><a class="reference internal" href="#creating-a-parser">15.4.1.1. Creating a parser</a></li> <li><a class="reference internal" href="#adding-arguments">15.4.1.2. Adding arguments</a></li> <li><a class="reference internal" href="#parsing-arguments">15.4.1.3. Parsing arguments</a></li> </ul> </li> <li><a class="reference internal" href="#argumentparser-objects">15.4.2. ArgumentParser objects</a><ul> <li><a class="reference internal" href="#prog">15.4.2.1. prog</a></li> <li><a class="reference internal" href="#usage">15.4.2.2. usage</a></li> <li><a class="reference internal" href="#description">15.4.2.3. description</a></li> <li><a class="reference internal" href="#epilog">15.4.2.4. epilog</a></li> <li><a class="reference internal" href="#parents">15.4.2.5. parents</a></li> <li><a class="reference internal" href="#formatter-class">15.4.2.6. formatter_class</a></li> <li><a class="reference internal" href="#prefix-chars">15.4.2.7. prefix_chars</a></li> <li><a class="reference internal" href="#fromfile-prefix-chars">15.4.2.8. fromfile_prefix_chars</a></li> <li><a class="reference internal" href="#argument-default">15.4.2.9. argument_default</a></li> <li><a class="reference internal" href="#conflict-handler">15.4.2.10. conflict_handler</a></li> <li><a class="reference internal" href="#add-help">15.4.2.11. add_help</a></li> </ul> </li> <li><a class="reference internal" href="#the-add-argument-method">15.4.3. The add_argument() method</a><ul> <li><a class="reference internal" href="#name-or-flags">15.4.3.1. name or flags</a></li> <li><a class="reference internal" href="#action">15.4.3.2. action</a></li> <li><a class="reference internal" href="#nargs">15.4.3.3. nargs</a></li> <li><a class="reference internal" href="#const">15.4.3.4. const</a></li> <li><a class="reference internal" href="#default">15.4.3.5. default</a></li> <li><a class="reference internal" href="#type">15.4.3.6. type</a></li> <li><a class="reference internal" href="#choices">15.4.3.7. choices</a></li> <li><a class="reference internal" href="#required">15.4.3.8. required</a></li> <li><a class="reference internal" href="#help">15.4.3.9. help</a></li> <li><a class="reference internal" href="#metavar">15.4.3.10. metavar</a></li> <li><a class="reference internal" href="#dest">15.4.3.11. dest</a></li> </ul> </li> <li><a class="reference internal" href="#the-parse-args-method">15.4.4. The parse_args() method</a><ul> <li><a class="reference internal" href="#option-value-syntax">15.4.4.1. Option value syntax</a></li> <li><a class="reference internal" href="#invalid-arguments">15.4.4.2. Invalid arguments</a></li> <li><a class="reference internal" href="#arguments-containing">15.4.4.3. Arguments containing <tt class="docutils literal"><span class="pre">-</span></tt></a></li> <li><a class="reference internal" href="#argument-abbreviations">15.4.4.4. Argument abbreviations</a></li> <li><a class="reference internal" href="#beyond-sys-argv">15.4.4.5. Beyond <tt class="docutils literal"><span class="pre">sys.argv</span></tt></a></li> <li><a class="reference internal" href="#the-namespace-object">15.4.4.6. The Namespace object</a></li> </ul> </li> <li><a class="reference internal" href="#other-utilities">15.4.5. Other utilities</a><ul> <li><a class="reference internal" href="#sub-commands">15.4.5.1. Sub-commands</a></li> <li><a class="reference internal" href="#filetype-objects">15.4.5.2. FileType objects</a></li> <li><a class="reference internal" href="#argument-groups">15.4.5.3. Argument groups</a></li> <li><a class="reference internal" href="#mutual-exclusion">15.4.5.4. Mutual exclusion</a></li> <li><a class="reference internal" href="#parser-defaults">15.4.5.5. Parser defaults</a></li> <li><a class="reference internal" href="#printing-help">15.4.5.6. Printing help</a></li> <li><a class="reference internal" href="#partial-parsing">15.4.5.7. Partial parsing</a></li> <li><a class="reference internal" href="#customizing-file-parsing">15.4.5.8. Customizing file parsing</a></li> <li><a class="reference internal" href="#exiting-methods">15.4.5.9. Exiting methods</a></li> </ul> </li> <li><a class="reference internal" href="#upgrading-optparse-code">15.4.6. Upgrading optparse code</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="time.html" title="previous chapter">15.3. <tt class="docutils literal"><span class="pre">time</span></tt> — Time access and conversions</a></p> <h4>Next topic</h4> <p class="topless"><a href="optparse.html" title="next chapter">15.5. <tt class="docutils literal"><span class="pre">optparse</span></tt> — Parser for command line options</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/argparse.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="optparse.html" title="15.5. optparse — Parser for command line options" >next</a> |</li> <li class="right" > <a href="time.html" title="15.3. time — Time access and conversions" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="allos.html" >15. Generic Operating System Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>