Sh3ll
OdayForums


Server : Apache
System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64
User : nobody ( 99)
PHP Version : 8.1.23
Disable Function : NONE
Directory :  /usr/share/doc/python-docs-2.7.5/html/library/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/doc/python-docs-2.7.5/html/library/optparse.html

<!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.5. optparse — Parser for command line options &mdash; 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.6. getopt — C-style parser for command line options" href="getopt.html" />
    <link rel="prev" title="15.4. argparse — Parser for command-line options, arguments and sub-commands" href="argparse.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="getopt.html" title="15.6. getopt — C-style parser for command line options"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="argparse.html" title="15.4. argparse — Parser for command-line options, arguments and sub-commands"
             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> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" accesskey="U">15. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-optparse">
<span id="optparse-parser-for-command-line-options"></span><h1>15.5. <a class="reference internal" href="#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> &#8212; Parser for command line options<a class="headerlink" href="#module-optparse" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.3.</span></p>
<p class="deprecated">
<span class="versionmodified">Deprecated since version 2.7: </span>The <a class="reference internal" href="#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> module is deprecated and will not be developed further;
development will continue with the <a class="reference internal" href="argparse.html#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.</p>
<p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/optparse.py">Lib/optparse.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#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> is a more convenient, flexible, and powerful library for parsing
command-line options than the old <a class="reference internal" href="getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><tt class="xref py py-mod docutils literal"><span class="pre">getopt</span></tt></a> module.  <a class="reference internal" href="#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> uses a
more declarative style of command-line parsing: you create an instance of
<a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>, populate it with options, and parse the command
line. <a class="reference internal" href="#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> allows users to specify options in the conventional
GNU/POSIX syntax, and additionally generates usage and help messages for you.</p>
<p>Here&#8217;s an example of using <a class="reference internal" href="#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> in a simple script:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;write report to FILE&quot;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">&quot;FILE&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="s">&quot;--quiet&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;don&#39;t print status messages to stdout&quot;</span><span class="p">)</span>

<span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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>With these few lines of code, users of your script can now do the &#8220;usual thing&#8221;
on the command-line, for example:</p>
<div class="highlight-python"><pre>&lt;yourscript&gt; --file=outfile -q</pre>
</div>
<p>As it parses the command line, <a class="reference internal" href="#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> sets attributes of the
<tt class="docutils literal"><span class="pre">options</span></tt> object returned by <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> based on user-supplied
command-line values.  When <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> returns from parsing this command
line, <tt class="docutils literal"><span class="pre">options.filename</span></tt> will be <tt class="docutils literal"><span class="pre">&quot;outfile&quot;</span></tt> and <tt class="docutils literal"><span class="pre">options.verbose</span></tt> will be
<tt class="docutils literal"><span class="pre">False</span></tt>.  <a class="reference internal" href="#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> supports both long and short options, allows short
options to be merged together, and allows options to be associated with their
arguments in a variety of ways.  Thus, the following command lines are all
equivalent to the above example:</p>
<div class="highlight-python"><pre>&lt;yourscript&gt; -f outfile --quiet
&lt;yourscript&gt; --quiet --file outfile
&lt;yourscript&gt; -q -foutfile
&lt;yourscript&gt; -qfoutfile</pre>
</div>
<p>Additionally, users can run one of</p>
<div class="highlight-python"><pre>&lt;yourscript&gt; -h
&lt;yourscript&gt; --help</pre>
</div>
<p>and <a class="reference internal" href="#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> will print out a brief summary of your script&#8217;s options:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: &lt;yourscript&gt; [options]

Options:
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write report to FILE
  -q, --quiet           don&#39;t print status messages to stdout
</pre></div>
</div>
<p>where the value of <em>yourscript</em> is determined at runtime (normally from
<tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt>).</p>
<div class="section" id="background">
<span id="optparse-background"></span><h2>15.5.1. Background<a class="headerlink" href="#background" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#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 explicitly designed to encourage the creation of programs
with straightforward, conventional command-line interfaces.  To that end, it
supports only the most common command-line syntax and semantics conventionally
used under Unix.  If you are unfamiliar with these conventions, read this
section to acquaint yourself with them.</p>
<div class="section" id="terminology">
<span id="optparse-terminology"></span><h3>15.5.1.1. Terminology<a class="headerlink" href="#terminology" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt>argument</dt>
<dd><p class="first">a string entered on the command-line, and passed by the shell to <tt class="docutils literal"><span class="pre">execl()</span></tt>
or <tt class="docutils literal"><span class="pre">execv()</span></tt>.  In Python, arguments are elements of <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>
(<tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is the name of the program being executed).  Unix shells
also use the term &#8220;word&#8221;.</p>
<p class="last">It is occasionally desirable to substitute an argument list other than
<tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>, so you should read &#8220;argument&#8221; as &#8220;an element of
<tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>, or of some other list provided as a substitute for
<tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>&#8221;.</p>
</dd>
<dt>option</dt>
<dd><p class="first">an argument used to supply extra information to guide or customize the
execution of a program.  There are many different syntaxes for options; the
traditional Unix syntax is a hyphen (&#8220;-&#8221;) followed by a single letter,
e.g. <tt class="docutils literal"><span class="pre">-x</span></tt> or <tt class="docutils literal"><span class="pre">-F</span></tt>.  Also, traditional Unix syntax allows multiple
options to be merged into a single argument, e.g. <tt class="docutils literal"><span class="pre">-x</span> <span class="pre">-F</span></tt> is equivalent
to <tt class="docutils literal"><span class="pre">-xF</span></tt>.  The GNU project introduced <tt class="docutils literal"><span class="pre">--</span></tt> followed by a series of
hyphen-separated words, e.g. <tt class="docutils literal"><span class="pre">--file</span></tt> or <tt class="docutils literal"><span class="pre">--dry-run</span></tt>.  These are the
only two option syntaxes provided by <a class="reference internal" href="#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>.</p>
<p>Some other option syntaxes that the world has seen include:</p>
<ul class="simple">
<li>a hyphen followed by a few letters, e.g. <tt class="docutils literal"><span class="pre">-pf</span></tt> (this is <em>not</em> the same
as multiple options merged into a single argument)</li>
<li>a hyphen followed by a whole word, e.g. <tt class="docutils literal"><span class="pre">-file</span></tt> (this is technically
equivalent to the previous syntax, but they aren&#8217;t usually seen in the same
program)</li>
<li>a plus sign followed by a single letter, or a few letters, or a word, e.g.
<tt class="docutils literal"><span class="pre">+f</span></tt>, <tt class="docutils literal"><span class="pre">+rgb</span></tt></li>
<li>a slash followed by a letter, or a few letters, or a word, e.g. <tt class="docutils literal"><span class="pre">/f</span></tt>,
<tt class="docutils literal"><span class="pre">/file</span></tt></li>
</ul>
<p class="last">These option syntaxes are not supported by <a class="reference internal" href="#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>, and they never
will be.  This is deliberate: the first three are non-standard on any
environment, and the last only makes sense if you&#8217;re exclusively targeting
VMS, MS-DOS, and/or Windows.</p>
</dd>
<dt>option argument</dt>
<dd><p class="first">an argument that follows an option, is closely associated with that option,
and is consumed from the argument list when that option is. With
<a class="reference internal" href="#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>, option arguments may either be in a separate argument from
their option:</p>
<div class="highlight-text"><div class="highlight"><pre>-f foo
--file foo
</pre></div>
</div>
<p>or included in the same argument:</p>
<div class="highlight-text"><div class="highlight"><pre>-ffoo
--file=foo
</pre></div>
</div>
<p class="last">Typically, a given option either takes an argument or it doesn&#8217;t. Lots of
people want an &#8220;optional option arguments&#8221; feature, meaning that some options
will take an argument if they see it, and won&#8217;t if they don&#8217;t.  This is
somewhat controversial, because it makes parsing ambiguous: if <tt class="docutils literal"><span class="pre">-a</span></tt> takes
an optional argument and <tt class="docutils literal"><span class="pre">-b</span></tt> is another option entirely, how do we
interpret <tt class="docutils literal"><span class="pre">-ab</span></tt>?  Because of this ambiguity, <a class="reference internal" href="#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> does not
support this feature.</p>
</dd>
<dt>positional argument</dt>
<dd>something leftover in the argument list after options have been parsed, i.e.
after options and their arguments have been parsed and removed from the
argument list.</dd>
<dt>required option</dt>
<dd>an option that must be supplied on the command-line; note that the phrase
&#8220;required option&#8221; is self-contradictory in English.  <a class="reference internal" href="#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> doesn&#8217;t
prevent you from implementing required options, but doesn&#8217;t give you much
help at it either.</dd>
</dl>
<p>For example, consider this hypothetical command-line:</p>
<div class="highlight-python"><pre>prog -v --report report.txt foo bar</pre>
</div>
<p><tt class="docutils literal"><span class="pre">-v</span></tt> and <tt class="docutils literal"><span class="pre">--report</span></tt> are both options.  Assuming that <tt class="docutils literal"><span class="pre">--report</span></tt>
takes one argument, <tt class="docutils literal"><span class="pre">report.txt</span></tt> is an option argument.  <tt class="docutils literal"><span class="pre">foo</span></tt> and
<tt class="docutils literal"><span class="pre">bar</span></tt> are positional arguments.</p>
</div>
<div class="section" id="what-are-options-for">
<span id="optparse-what-options-for"></span><h3>15.5.1.2. What are options for?<a class="headerlink" href="#what-are-options-for" title="Permalink to this headline">¶</a></h3>
<p>Options are used to provide extra information to tune or customize the execution
of a program.  In case it wasn&#8217;t clear, options are usually <em>optional</em>.  A
program should be able to run just fine with no options whatsoever.  (Pick a
random program from the Unix or GNU toolsets.  Can it run without any options at
all and still make sense?  The main exceptions are <tt class="docutils literal"><span class="pre">find</span></tt>, <tt class="docutils literal"><span class="pre">tar</span></tt>, and
<tt class="docutils literal"><span class="pre">dd</span></tt>&#8212;all of which are mutant oddballs that have been rightly criticized
for their non-standard syntax and confusing interfaces.)</p>
<p>Lots of people want their programs to have &#8220;required options&#8221;.  Think about it.
If it&#8217;s required, then it&#8217;s <em>not optional</em>!  If there is a piece of information
that your program absolutely requires in order to run successfully, that&#8217;s what
positional arguments are for.</p>
<p>As an example of good command-line interface design, consider the humble <tt class="docutils literal"><span class="pre">cp</span></tt>
utility, for copying files.  It doesn&#8217;t make much sense to try to copy files
without supplying a destination and at least one source. Hence, <tt class="docutils literal"><span class="pre">cp</span></tt> fails if
you run it with no arguments.  However, it has a flexible, useful syntax that
does not require any options at all:</p>
<div class="highlight-python"><pre>cp SOURCE DEST
cp SOURCE ... DEST-DIR</pre>
</div>
<p>You can get pretty far with just that.  Most <tt class="docutils literal"><span class="pre">cp</span></tt> implementations provide a
bunch of options to tweak exactly how the files are copied: you can preserve
mode and modification time, avoid following symlinks, ask before clobbering
existing files, etc.  But none of this distracts from the core mission of
<tt class="docutils literal"><span class="pre">cp</span></tt>, which is to copy either one file to another, or several files to another
directory.</p>
</div>
<div class="section" id="what-are-positional-arguments-for">
<span id="optparse-what-positional-arguments-for"></span><h3>15.5.1.3. What are positional arguments for?<a class="headerlink" href="#what-are-positional-arguments-for" title="Permalink to this headline">¶</a></h3>
<p>Positional arguments are for those pieces of information that your program
absolutely, positively requires to run.</p>
<p>A good user interface should have as few absolute requirements as possible.  If
your program requires 17 distinct pieces of information in order to run
successfully, it doesn&#8217;t much matter <em>how</em> you get that information from the
user&#8212;most people will give up and walk away before they successfully run the
program.  This applies whether the user interface is a command-line, a
configuration file, or a GUI: if you make that many demands on your users, most
of them will simply give up.</p>
<p>In short, try to minimize the amount of information that users are absolutely
required to supply&#8212;use sensible defaults whenever possible.  Of course, you
also want to make your programs reasonably flexible.  That&#8217;s what options are
for.  Again, it doesn&#8217;t matter if they are entries in a config file, widgets in
the &#8220;Preferences&#8221; dialog of a GUI, or command-line options&#8212;the more options
you implement, the more flexible your program is, and the more complicated its
implementation becomes.  Too much flexibility has drawbacks as well, of course;
too many options can overwhelm users and make your code much harder to maintain.</p>
</div>
</div>
<div class="section" id="tutorial">
<span id="optparse-tutorial"></span><h2>15.5.2. Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
<p>While <a class="reference internal" href="#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> is quite flexible and powerful, it&#8217;s also straightforward
to use in most cases.  This section covers the code patterns that are common to
any <a class="reference internal" href="#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>-based program.</p>
<p>First, you need to import the OptionParser class; then, early in the main
program, create an OptionParser instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span>
</pre></div>
</div>
<p>Then you can start defining options.  The basic syntax is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="n">opt_str</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span>
                  <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>Each option has one or more option strings, such as <tt class="docutils literal"><span class="pre">-f</span></tt> or <tt class="docutils literal"><span class="pre">--file</span></tt>,
and several option attributes that tell <a class="reference internal" href="#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> what to expect and what
to do when it encounters that option on the command line.</p>
<p>Typically, each option will have one short option string and one long option
string, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>You&#8217;re free to define as many short option strings and as many long option
strings as you like (including zero), as long as there is at least one option
string overall.</p>
<p>The option strings passed to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a> are effectively
labels for the
option defined by that call.  For brevity, we will frequently refer to
<em>encountering an option</em> on the command line; in reality, <a class="reference internal" href="#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>
encounters <em>option strings</em> and looks up options from them.</p>
<p>Once all of your options are defined, instruct <a class="reference internal" href="#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 parse your
program&#8217;s command line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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 you like, you can pass a custom argument list to <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>, but
that&#8217;s rarely necessary: by default it uses <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>.)</p>
<p><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> returns two values:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">options</span></tt>, an object containing values for all of your options&#8212;e.g. if
<tt class="docutils literal"><span class="pre">--file</span></tt> takes a single string argument, then <tt class="docutils literal"><span class="pre">options.file</span></tt> will be the
filename supplied by the user, or <tt class="docutils literal"><span class="pre">None</span></tt> if the user did not supply that
option</li>
<li><tt class="docutils literal"><span class="pre">args</span></tt>, the list of positional arguments leftover after parsing options</li>
</ul>
<p>This tutorial section only covers the four most important option attributes:
<a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>
(destination), and <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a>. Of these, <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a> is the
most fundamental.</p>
<div class="section" id="understanding-option-actions">
<span id="optparse-understanding-option-actions"></span><h3>15.5.2.1. Understanding option actions<a class="headerlink" href="#understanding-option-actions" title="Permalink to this headline">¶</a></h3>
<p>Actions tell <a class="reference internal" href="#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> what to do when it encounters an option on the
command line.  There is a fixed set of actions hard-coded into <a class="reference internal" href="#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>;
adding new actions is an advanced topic covered in section
<a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>.  Most actions tell <a class="reference internal" href="#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 store
a value in some variable&#8212;for example, take a string from the command line and
store it in an attribute of <tt class="docutils literal"><span class="pre">options</span></tt>.</p>
<p>If you don&#8217;t specify an option action, <a class="reference internal" href="#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> defaults to <tt class="docutils literal"><span class="pre">store</span></tt>.</p>
</div>
<div class="section" id="the-store-action">
<span id="optparse-store-action"></span><h3>15.5.2.2. The store action<a class="headerlink" href="#the-store-action" title="Permalink to this headline">¶</a></h3>
<p>The most common option action is <tt class="docutils literal"><span class="pre">store</span></tt>, which tells <a class="reference internal" href="#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 take
the next argument (or the remainder of the current argument), ensure that it is
of the correct type, and store it to your chosen destination.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Now let&#8217;s make up a fake command line and ask <a class="reference internal" href="#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 parse it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;foo.txt&quot;</span><span class="p">]</span>
<span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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="n">args</span><span class="p">)</span>
</pre></div>
</div>
<p>When <a class="reference internal" href="#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> sees the option string <tt class="docutils literal"><span class="pre">-f</span></tt>, it consumes the next
argument, <tt class="docutils literal"><span class="pre">foo.txt</span></tt>, and stores it in <tt class="docutils literal"><span class="pre">options.filename</span></tt>.  So, after this
call to <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>, <tt class="docutils literal"><span class="pre">options.filename</span></tt> is <tt class="docutils literal"><span class="pre">&quot;foo.txt&quot;</span></tt>.</p>
<p>Some other option types supported by <a class="reference internal" href="#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> are <tt class="docutils literal"><span class="pre">int</span></tt> and <tt class="docutils literal"><span class="pre">float</span></tt>.
Here&#8217;s an option that expects an integer argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-n&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;int&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;num&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that this option has no long option string, which is perfectly acceptable.
Also, there&#8217;s no explicit action, since the default is <tt class="docutils literal"><span class="pre">store</span></tt>.</p>
<p>Let&#8217;s parse another fake command-line.  This time, we&#8217;ll jam the option argument
right up against the option: since <tt class="docutils literal"><span class="pre">-n42</span></tt> (one argument) is equivalent to
<tt class="docutils literal"><span class="pre">-n</span> <span class="pre">42</span></tt> (two arguments), the code</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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">&quot;-n42&quot;</span><span class="p">])</span>
<span class="k">print</span> <span class="n">options</span><span class="o">.</span><span class="n">num</span>
</pre></div>
</div>
<p>will print <tt class="docutils literal"><span class="pre">42</span></tt>.</p>
<p>If you don&#8217;t specify a type, <a class="reference internal" href="#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> assumes <tt class="docutils literal"><span class="pre">string</span></tt>.  Combined with
the fact that the default action is <tt class="docutils literal"><span class="pre">store</span></tt>, that means our first example can
be a lot shorter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you don&#8217;t supply a destination, <a class="reference internal" href="#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> figures out a sensible
default from the option strings: if the first long option string is
<tt class="docutils literal"><span class="pre">--foo-bar</span></tt>, then the default destination is <tt class="docutils literal"><span class="pre">foo_bar</span></tt>.  If there are no
long option strings, <a class="reference internal" href="#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> looks at the first short option string: the
default destination for <tt class="docutils literal"><span class="pre">-f</span></tt> is <tt class="docutils literal"><span class="pre">f</span></tt>.</p>
<p><a class="reference internal" href="#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> also includes built-in <tt class="docutils literal"><span class="pre">long</span></tt> and <tt class="docutils literal"><span class="pre">complex</span></tt> types.  Adding
types is covered in section <a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>.</p>
</div>
<div class="section" id="handling-boolean-flag-options">
<span id="optparse-handling-boolean-options"></span><h3>15.5.2.3. Handling boolean (flag) options<a class="headerlink" href="#handling-boolean-flag-options" title="Permalink to this headline">¶</a></h3>
<p>Flag options&#8212;set a variable to true or false when a particular option is seen
&#8212;are quite common.  <a class="reference internal" href="#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> supports them with two separate actions,
<tt class="docutils literal"><span class="pre">store_true</span></tt> and <tt class="docutils literal"><span class="pre">store_false</span></tt>.  For example, you might have a <tt class="docutils literal"><span class="pre">verbose</span></tt>
flag that is turned on with <tt class="docutils literal"><span class="pre">-v</span></tt> and off with <tt class="docutils literal"><span class="pre">-q</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Here we have two different options with the same destination, which is perfectly
OK.  (It just means you have to be a bit careful when setting default values&#8212;
see below.)</p>
<p>When <a class="reference internal" href="#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> encounters <tt class="docutils literal"><span class="pre">-v</span></tt> on the command line, it sets
<tt class="docutils literal"><span class="pre">options.verbose</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt>; when it encounters <tt class="docutils literal"><span class="pre">-q</span></tt>,
<tt class="docutils literal"><span class="pre">options.verbose</span></tt> is set to <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
</div>
<div class="section" id="other-actions">
<span id="optparse-other-actions"></span><h3>15.5.2.4. Other actions<a class="headerlink" href="#other-actions" title="Permalink to this headline">¶</a></h3>
<p>Some other actions supported by <a class="reference internal" href="#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> are:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt></dt>
<dd>store a constant value</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt></dt>
<dd>append this option&#8217;s argument to a list</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;count&quot;</span></tt></dt>
<dd>increment a counter by one</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt></dt>
<dd>call a specified function</dd>
</dl>
<p>These are covered in section <a class="reference internal" href="#optparse-reference-guide"><em>Reference Guide</em></a>, Reference Guide
and section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a>.</p>
</div>
<div class="section" id="default-values">
<span id="optparse-default-values"></span><h3>15.5.2.5. Default values<a class="headerlink" href="#default-values" title="Permalink to this headline">¶</a></h3>
<p>All of the above examples involve setting some variable (the &#8220;destination&#8221;) when
certain command-line options are seen.  What happens if those options are never
seen?  Since we didn&#8217;t supply any defaults, they are all set to <tt class="docutils literal"><span class="pre">None</span></tt>.  This
is usually fine, but sometimes you want more control.  <a class="reference internal" href="#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> lets you
supply a default value for each destination, which is assigned before the
command line is parsed.</p>
<p>First, consider the verbose/quiet example.  If we want <a class="reference internal" href="#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 set
<tt class="docutils literal"><span class="pre">verbose</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt> unless <tt class="docutils literal"><span class="pre">-q</span></tt> is seen, then we can do this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Since default values apply to the <em>destination</em> rather than to any particular
option, and these two options happen to have the same destination, this is
exactly equivalent:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Consider this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Again, the default value for <tt class="docutils literal"><span class="pre">verbose</span></tt> will be <tt class="docutils literal"><span class="pre">True</span></tt>: the last default
value supplied for any particular destination is the one that counts.</p>
<p>A clearer way to specify default values is the <tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt> method of
OptionParser, which you can call at any time before calling <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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>As before, the last value specified for a given option destination is the one
that counts.  For clarity, try to use one method or the other of setting default
values, not both.</p>
</div>
<div class="section" id="generating-help">
<span id="optparse-generating-help"></span><h3>15.5.2.6. Generating help<a class="headerlink" href="#generating-help" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#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>&#8216;s ability to generate help and usage text automatically is
useful for creating user-friendly command-line interfaces.  All you have to do
is supply a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> value for each option, and optionally a short
usage message for your whole program.  Here&#8217;s an OptionParser populated with
user-friendly (documented) options:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">usage</span> <span class="o">=</span> <span class="s">&quot;usage: %prog [options] arg1 arg2&quot;</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="n">usage</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="s">&quot;--verbose&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;make lots of noise [default]&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="s">&quot;--quiet&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;be vewwy quiet (I&#39;m hunting wabbits)&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--filename&quot;</span><span class="p">,</span>
                  <span class="n">metavar</span><span class="o">=</span><span class="s">&quot;FILE&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;write output to FILE&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-m&quot;</span><span class="p">,</span> <span class="s">&quot;--mode&quot;</span><span class="p">,</span>
                  <span class="n">default</span><span class="o">=</span><span class="s">&quot;intermediate&quot;</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;interaction mode: novice, intermediate, &quot;</span>
                       <span class="s">&quot;or expert [default: </span><span class="si">%d</span><span class="s">efault]&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If <a class="reference internal" href="#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> encounters either <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> on the
command-line, or if you just call <tt class="xref py py-meth docutils literal"><span class="pre">parser.print_help()</span></tt>, it prints the
following to standard output:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: &lt;yourscript&gt; [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I&#39;m hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
                        expert [default: intermediate]
</pre></div>
</div>
<p>(If the help output is triggered by a help option, <a class="reference internal" href="#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> exits after
printing the help text.)</p>
<p>There&#8217;s a lot going on here to help <a class="reference internal" href="#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> generate the best possible
help message:</p>
<ul>
<li><p class="first">the script defines its own usage message:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">usage</span> <span class="o">=</span> <span class="s">&quot;usage: %prog [options] arg1 arg2&quot;</span>
</pre></div>
</div>
<p><a class="reference internal" href="#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> expands <tt class="docutils literal"><span class="pre">%prog</span></tt> in the usage string to the name of the
current program, i.e. <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt>.  The expanded string
is then printed before the detailed option help.</p>
<p>If you don&#8217;t supply a usage string, <a class="reference internal" href="#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> uses a bland but sensible
default: <tt class="docutils literal"><span class="pre">&quot;Usage:</span> <span class="pre">%prog</span> <span class="pre">[options]&quot;</span></tt>, which is fine if your script doesn&#8217;t
take any positional arguments.</p>
</li>
<li><p class="first">every option defines a help string, and doesn&#8217;t worry about line-wrapping&#8212;
<a class="reference internal" href="#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> takes care of wrapping lines and making the help output look
good.</p>
</li>
<li><p class="first">options that take a value indicate this fact in their automatically-generated
help message, e.g. for the &#8220;mode&#8221; option:</p>
<div class="highlight-python"><pre>-m MODE, --mode=MODE</pre>
</div>
<p>Here, &#8220;MODE&#8221; is called the meta-variable: it stands for the argument that the
user is expected to supply to <tt class="docutils literal"><span class="pre">-m</span></tt>/<tt class="docutils literal"><span class="pre">--mode</span></tt>.  By default,
<a class="reference internal" href="#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> converts the destination variable name to uppercase and uses
that for the meta-variable.  Sometimes, that&#8217;s not what you want&#8212;for
example, the <tt class="docutils literal"><span class="pre">--filename</span></tt> option explicitly sets <tt class="docutils literal"><span class="pre">metavar=&quot;FILE&quot;</span></tt>,
resulting in this automatically-generated option description:</p>
<div class="highlight-python"><pre>-f FILE, --filename=FILE</pre>
</div>
<p>This is important for more than just saving space, though: the manually
written help text uses the meta-variable <tt class="docutils literal"><span class="pre">FILE</span></tt> to clue the user in that
there&#8217;s a connection between the semi-formal syntax <tt class="docutils literal"><span class="pre">-f</span> <span class="pre">FILE</span></tt> and the informal
semantic description &#8220;write output to FILE&#8221;. This is a simple but effective
way to make your help text a lot clearer and more useful for end users.</p>
</li>
</ul>
<p class="versionadded">
<span class="versionmodified">New in version 2.4: </span>Options that have a default value can include <tt class="docutils literal"><span class="pre">%default</span></tt> in the help
string&#8212;<a class="reference internal" href="#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> will replace it with <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> of the option&#8217;s
default value.  If an option has no default value (or the default value is
<tt class="docutils literal"><span class="pre">None</span></tt>), <tt class="docutils literal"><span class="pre">%default</span></tt> expands to <tt class="docutils literal"><span class="pre">none</span></tt>.</p>
<div class="section" id="grouping-options">
<h4>15.5.2.6.1. Grouping Options<a class="headerlink" href="#grouping-options" title="Permalink to this headline">¶</a></h4>
<p>When dealing with many options, it is convenient to group these options for
better help output.  An <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> can contain several option groups,
each of which can contain several options.</p>
<p>An option group is obtained using the class <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a>:</p>
<dl class="class">
<dt id="optparse.OptionGroup">
<em class="property">class </em><tt class="descclassname">optparse.</tt><tt class="descname">OptionGroup</tt><big>(</big><em>parser</em>, <em>title</em>, <em>description=None</em><big>)</big><a class="headerlink" href="#optparse.OptionGroup" title="Permalink to this definition">¶</a></dt>
<dd><p>where</p>
<ul class="simple">
<li>parser is the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> instance the group will be insterted in
to</li>
<li>title is the group title</li>
<li>description, optional, is a long description of the group</li>
</ul>
</dd></dl>

<p><a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> inherits from <tt class="xref py py-class docutils literal"><span class="pre">OptionContainer</span></tt> (like
<a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>) and so the <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> method can be used to add
an option to the group.</p>
<p>Once all the options are declared, using the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> method
<tt class="xref py py-meth docutils literal"><span class="pre">add_option_group()</span></tt> the group is added to the previously defined parser.</p>
<p>Continuing with the parser defined in the previous section, adding an
<a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> to a parser is easy:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">&quot;Dangerous Options&quot;</span><span class="p">,</span>
                    <span class="s">&quot;Caution: use these options at your own risk.  &quot;</span>
                    <span class="s">&quot;It is believed that some of them bite.&quot;</span><span class="p">)</span>
<span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-g&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;Group option.&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
</pre></div>
</div>
<p>This would result in the following help output:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: &lt;yourscript&gt; [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I&#39;m hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
                        expert [default: intermediate]

  Dangerous Options:
    Caution: use these options at your own risk.  It is believed that some
    of them bite.

    -g                  Group option.
</pre></div>
</div>
<p>A bit more complete example might involve using more than one group: still
extending the previous example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">&quot;Dangerous Options&quot;</span><span class="p">,</span>
                    <span class="s">&quot;Caution: use these options at your own risk.  &quot;</span>
                    <span class="s">&quot;It is believed that some of them bite.&quot;</span><span class="p">)</span>
<span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-g&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;Group option.&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>

<span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">&quot;Debug Options&quot;</span><span class="p">)</span>
<span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-d&quot;</span><span class="p">,</span> <span class="s">&quot;--debug&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span>
                 <span class="n">help</span><span class="o">=</span><span class="s">&quot;Print debug information&quot;</span><span class="p">)</span>
<span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-s&quot;</span><span class="p">,</span> <span class="s">&quot;--sql&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span>
                 <span class="n">help</span><span class="o">=</span><span class="s">&quot;Print all SQL statements executed&quot;</span><span class="p">)</span>
<span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-e&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;Print every action done&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
</pre></div>
</div>
<p>that results in the following output:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: &lt;yourscript&gt; [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I&#39;m hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or expert
                        [default: intermediate]

  Dangerous Options:
    Caution: use these options at your own risk.  It is believed that some
    of them bite.

    -g                  Group option.

  Debug Options:
    -d, --debug         Print debug information
    -s, --sql           Print all SQL statements executed
    -e                  Print every action done
</pre></div>
</div>
<p>Another interesting method, in particular when working programmatically with
option groups is:</p>
<dl class="method">
<dt id="optparse.OptionParser.get_option_group">
<tt class="descclassname">OptionParser.</tt><tt class="descname">get_option_group</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_option_group" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> to which the short or long option
string <em>opt_str</em> (e.g. <tt class="docutils literal"><span class="pre">'-o'</span></tt> or <tt class="docutils literal"><span class="pre">'--option'</span></tt>) belongs. If
there&#8217;s no such <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a>, return <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="printing-a-version-string">
<span id="optparse-printing-version-string"></span><h3>15.5.2.7. Printing a version string<a class="headerlink" href="#printing-a-version-string" title="Permalink to this headline">¶</a></h3>
<p>Similar to the brief usage string, <a class="reference internal" href="#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> can also print a version
string for your program.  You have to supply the string as the <tt class="docutils literal"><span class="pre">version</span></tt>
argument to OptionParser:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="s">&quot;%prog [-f] [-q]&quot;</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">&quot;%prog 1.0&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">%prog</span></tt> is expanded just like it is in <tt class="docutils literal"><span class="pre">usage</span></tt>.  Apart from that,
<tt class="docutils literal"><span class="pre">version</span></tt> can contain anything you like.  When you supply it, <a class="reference internal" href="#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>
automatically adds a <tt class="docutils literal"><span class="pre">--version</span></tt> option to your parser. If it encounters
this option on the command line, it expands your <tt class="docutils literal"><span class="pre">version</span></tt> string (by
replacing <tt class="docutils literal"><span class="pre">%prog</span></tt>), prints it to stdout, and exits.</p>
<p>For example, if your script is called <tt class="docutils literal"><span class="pre">/usr/bin/foo</span></tt>:</p>
<div class="highlight-python"><pre>$ /usr/bin/foo --version
foo 1.0</pre>
</div>
<p>The following two methods can be used to print and get the <tt class="docutils literal"><span class="pre">version</span></tt> string:</p>
<dl class="method">
<dt id="optparse.OptionParser.print_version">
<tt class="descclassname">OptionParser.</tt><tt class="descname">print_version</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.print_version" title="Permalink to this definition">¶</a></dt>
<dd><p>Print the version message for the current program (<tt class="docutils literal"><span class="pre">self.version</span></tt>) to
<em>file</em> (default stdout).  As with <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><tt class="xref py py-meth docutils literal"><span class="pre">print_usage()</span></tt></a>, any occurrence
of <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">self.version</span></tt> is replaced with the name of the current
program.  Does nothing if <tt class="docutils literal"><span class="pre">self.version</span></tt> is empty or undefined.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.get_version">
<tt class="descclassname">OptionParser.</tt><tt class="descname">get_version</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_version" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_version" title="optparse.OptionParser.print_version"><tt class="xref py py-meth docutils literal"><span class="pre">print_version()</span></tt></a> but returns the version string instead of
printing it.</p>
</dd></dl>

</div>
<div class="section" id="how-optparse-handles-errors">
<span id="optparse-how-optparse-handles-errors"></span><h3>15.5.2.8. How <a class="reference internal" href="#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> handles errors<a class="headerlink" href="#how-optparse-handles-errors" title="Permalink to this headline">¶</a></h3>
<p>There are two broad classes of errors that <a class="reference internal" href="#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> has to worry about:
programmer errors and user errors.  Programmer errors are usually erroneous
calls to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-func docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>, e.g. invalid option strings, unknown
option attributes, missing option attributes, etc.  These are dealt with in the
usual way: raise an exception (either <tt class="xref py py-exc docutils literal"><span class="pre">optparse.OptionError</span></tt> or
<a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>) and let the program crash.</p>
<p>Handling user errors is much more important, since they are guaranteed to happen
no matter how stable your code is.  <a class="reference internal" href="#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> can automatically detect
some user errors, such as bad option arguments (passing <tt class="docutils literal"><span class="pre">-n</span> <span class="pre">4x</span></tt> where
<tt class="docutils literal"><span class="pre">-n</span></tt> takes an integer argument), missing arguments (<tt class="docutils literal"><span class="pre">-n</span></tt> at the end
of the command line, where <tt class="docutils literal"><span class="pre">-n</span></tt> takes an argument of any type).  Also,
you can call <tt class="xref py py-func docutils literal"><span class="pre">OptionParser.error()</span></tt> to signal an application-defined error
condition:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">a</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;options -a and -b are mutually exclusive&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>In either case, <a class="reference internal" href="#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> handles the error the same way: it prints the
program&#8217;s usage message and an error message to standard error and exits with
error status 2.</p>
<p>Consider the first example above, where the user passes <tt class="docutils literal"><span class="pre">4x</span></tt> to an option
that takes an integer:</p>
<div class="highlight-python"><pre>$ /usr/bin/foo -n 4x
Usage: foo [options]

foo: error: option -n: invalid integer value: '4x'</pre>
</div>
<p>Or, where the user fails to pass a value at all:</p>
<div class="highlight-python"><pre>$ /usr/bin/foo -n
Usage: foo [options]

foo: error: -n option requires an argument</pre>
</div>
<p><a class="reference internal" href="#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>-generated error messages take care always to mention the
option involved in the error; be sure to do the same when calling
<tt class="xref py py-func docutils literal"><span class="pre">OptionParser.error()</span></tt> from your application code.</p>
<p>If <a class="reference internal" href="#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>&#8216;s default error-handling behaviour does not suit your needs,
you&#8217;ll need to subclass OptionParser and override its <tt class="xref py py-meth docutils literal"><span class="pre">exit()</span></tt>
and/or <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt> methods.</p>
</div>
<div class="section" id="putting-it-all-together">
<span id="optparse-putting-it-all-together"></span><h3>15.5.2.9. Putting it all together<a class="headerlink" href="#putting-it-all-together" title="Permalink to this headline">¶</a></h3>
<p>Here&#8217;s what <a class="reference internal" href="#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>-based scripts usually look like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">usage</span> <span class="o">=</span> <span class="s">&quot;usage: %prog [options] arg&quot;</span>
    <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">,</span>
                      <span class="n">help</span><span class="o">=</span><span class="s">&quot;read data from FILENAME&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="s">&quot;--verbose&quot;</span><span class="p">,</span>
                      <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="s">&quot;--quiet&quot;</span><span class="p">,</span>
                      <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
    <span class="p">[</span><span class="o">...</span><span class="p">]</span>
    <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;incorrect number of arguments&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;reading </span><span class="si">%s</span><span class="s">...&quot;</span> <span class="o">%</span> <span class="n">options</span><span class="o">.</span><span class="n">filename</span>
    <span class="p">[</span><span class="o">...</span><span class="p">]</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="reference-guide">
<span id="optparse-reference-guide"></span><h2>15.5.3. Reference Guide<a class="headerlink" href="#reference-guide" title="Permalink to this headline">¶</a></h2>
<div class="section" id="creating-the-parser">
<span id="optparse-creating-parser"></span><h3>15.5.3.1. Creating the parser<a class="headerlink" href="#creating-the-parser" title="Permalink to this headline">¶</a></h3>
<p>The first step in using <a class="reference internal" href="#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> is to create an OptionParser instance.</p>
<dl class="class">
<dt id="optparse.OptionParser">
<em class="property">class </em><tt class="descclassname">optparse.</tt><tt class="descname">OptionParser</tt><big>(</big><em>...</em><big>)</big><a class="headerlink" href="#optparse.OptionParser" title="Permalink to this definition">¶</a></dt>
<dd><p>The OptionParser constructor has no required arguments, but a number of
optional keyword arguments.  You should always pass them as keyword
arguments, i.e. do not rely on the order in which the arguments are declared.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">usage</span></tt> (default: <tt class="docutils literal"><span class="pre">&quot;%prog</span> <span class="pre">[options]&quot;</span></tt>)</dt>
<dd>The usage summary to print when your program is run incorrectly or with a
help option.  When <a class="reference internal" href="#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> prints the usage string, it expands
<tt class="docutils literal"><span class="pre">%prog</span></tt> to <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt> (or to <tt class="docutils literal"><span class="pre">prog</span></tt> if you
passed that keyword argument).  To suppress a usage message, pass the
special value <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_USAGE</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">option_list</span></tt> (default: <tt class="docutils literal"><span class="pre">[]</span></tt>)</dt>
<dd>A list of Option objects to populate the parser with.  The options in
<tt class="docutils literal"><span class="pre">option_list</span></tt> are added after any options in <tt class="docutils literal"><span class="pre">standard_option_list</span></tt> (a
class attribute that may be set by OptionParser subclasses), but before
any version or help options. Deprecated; use <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt></a> after
creating the parser instead.</dd>
<dt><tt class="docutils literal"><span class="pre">option_class</span></tt> (default: optparse.Option)</dt>
<dd>Class to use when adding options to the parser in <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt></a>.</dd>
<dt><tt class="docutils literal"><span class="pre">version</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt>
<dd>A version string to print when the user supplies a version option. If you
supply a true value for <tt class="docutils literal"><span class="pre">version</span></tt>, <a class="reference internal" href="#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> automatically adds a
version option with the single option string <tt class="docutils literal"><span class="pre">--version</span></tt>.  The
substring <tt class="docutils literal"><span class="pre">%prog</span></tt> is expanded the same as for <tt class="docutils literal"><span class="pre">usage</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">conflict_handler</span></tt> (default: <tt class="docutils literal"><span class="pre">&quot;error&quot;</span></tt>)</dt>
<dd>Specifies what to do when options with conflicting option strings are
added to the parser; see section
<a class="reference internal" href="#optparse-conflicts-between-options"><em>Conflicts between options</em></a>.</dd>
<dt><tt class="docutils literal"><span class="pre">description</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt>
<dd>A paragraph of text giving a brief overview of your program.
<a class="reference internal" href="#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> reformats this paragraph to fit the current terminal width
and prints it when the user requests help (after <tt class="docutils literal"><span class="pre">usage</span></tt>, but before the
list of options).</dd>
<dt><tt class="docutils literal"><span class="pre">formatter</span></tt> (default: a new <tt class="xref py py-class docutils literal"><span class="pre">IndentedHelpFormatter</span></tt>)</dt>
<dd>An instance of optparse.HelpFormatter that will be used for printing help
text.  <a class="reference internal" href="#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> provides two concrete classes for this purpose:
IndentedHelpFormatter and TitledHelpFormatter.</dd>
<dt><tt class="docutils literal"><span class="pre">add_help_option</span></tt> (default: <tt class="docutils literal"><span class="pre">True</span></tt>)</dt>
<dd>If true, <a class="reference internal" href="#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> will add a help option (with option strings <tt class="docutils literal"><span class="pre">-h</span></tt>
and <tt class="docutils literal"><span class="pre">--help</span></tt>) to the parser.</dd>
<dt><tt class="docutils literal"><span class="pre">prog</span></tt></dt>
<dd>The string to use when expanding <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">usage</span></tt> and <tt class="docutils literal"><span class="pre">version</span></tt>
instead of <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">epilog</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt>
<dd>A paragraph of help text to print after the option help.</dd>
</dl>
</dd></dl>

</div>
<div class="section" id="populating-the-parser">
<span id="optparse-populating-parser"></span><h3>15.5.3.2. Populating the parser<a class="headerlink" href="#populating-the-parser" title="Permalink to this headline">¶</a></h3>
<p>There are several ways to populate the parser with options.  The preferred way
is by using <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>, as shown in section
<a class="reference internal" href="#optparse-tutorial"><em>Tutorial</em></a>.  <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> can be called in one of two ways:</p>
<ul class="simple">
<li>pass it an Option instance (as returned by <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt>)</li>
<li>pass it any combination of positional and keyword arguments that are
acceptable to <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt> (i.e., to the Option constructor), and it
will create the Option instance for you</li>
</ul>
<p>The other alternative is to pass a list of pre-constructed Option instances to
the OptionParser constructor, as in:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">make_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--filename&quot;</span><span class="p">,</span>
                <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">),</span>
    <span class="n">make_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="s">&quot;--quiet&quot;</span><span class="p">,</span>
                <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">),</span>
    <span class="p">]</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span>
</pre></div>
</div>
<p>(<tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt> is a factory function for creating Option instances;
currently it is an alias for the Option constructor.  A future version of
<a class="reference internal" href="#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> may split Option into several classes, and <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt>
will pick the right class to instantiate.  Do not instantiate Option directly.)</p>
</div>
<div class="section" id="defining-options">
<span id="optparse-defining-options"></span><h3>15.5.3.3. Defining options<a class="headerlink" href="#defining-options" title="Permalink to this headline">¶</a></h3>
<p>Each Option instance represents a set of synonymous command-line option strings,
e.g. <tt class="docutils literal"><span class="pre">-f</span></tt> and <tt class="docutils literal"><span class="pre">--file</span></tt>.  You can specify any number of short or
long option strings, but you must specify at least one overall option string.</p>
<p>The canonical way to create an <tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> instance is with the
<tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> method of <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>.</p>
<dl class="method">
<dt id="optparse.OptionParser.add_option">
<tt class="descclassname">OptionParser.</tt><tt class="descname">add_option</tt><big>(</big><em>option</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.add_option" title="Permalink to this definition">¶</a></dt>
<dt>
<tt class="descclassname">OptionParser.</tt><tt class="descname">add_option</tt><big>(</big><em>*opt_str</em>, <em>attr=value</em>, <em>...</em><big>)</big></dt>
<dd><p>To define an option with only a short option string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>And to define an option with only a long option string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--foo&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>The keyword arguments define attributes of the new Option object.  The most
important option attribute is <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, and it largely
determines which other attributes are relevant or required.  If you pass
irrelevant option attributes, or fail to pass required ones, <a class="reference internal" href="#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>
raises an <tt class="xref py py-exc docutils literal"><span class="pre">OptionError</span></tt> exception explaining your mistake.</p>
<p>An option&#8217;s <em>action</em> determines what <a class="reference internal" href="#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> does when it encounters
this option on the command-line.  The standard option actions hard-coded into
<a class="reference internal" href="#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> are:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt></dt>
<dd>store this option&#8217;s argument (default)</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt></dt>
<dd>store a constant value</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;store_true&quot;</span></tt></dt>
<dd>store a true value</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;store_false&quot;</span></tt></dt>
<dd>store a false value</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt></dt>
<dd>append this option&#8217;s argument to a list</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;append_const&quot;</span></tt></dt>
<dd>append a constant value to a list</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;count&quot;</span></tt></dt>
<dd>increment a counter by one</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt></dt>
<dd>call a specified function</dd>
<dt><tt class="docutils literal"><span class="pre">&quot;help&quot;</span></tt></dt>
<dd>print a usage message including all options and the documentation for them</dd>
</dl>
<p>(If you don&#8217;t supply an action, the default is <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt>.  For this action,
you may also supply <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> option
attributes; see <a class="reference internal" href="#optparse-standard-option-actions"><em>Standard option actions</em></a>.)</p>
</dd></dl>

<p>As you can see, most actions involve storing or updating a value somewhere.
<a class="reference internal" href="#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> always creates a special object for this, conventionally called
<tt class="docutils literal"><span class="pre">options</span></tt> (it happens to be an instance of <tt class="xref py py-class docutils literal"><span class="pre">optparse.Values</span></tt>).  Option
arguments (and various other values) are stored as attributes of this object,
according to the <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> (destination) option attribute.</p>
<p>For example, when you call</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
</pre></div>
</div>
<p>one of the first things <a class="reference internal" href="#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> does is create the <tt class="docutils literal"><span class="pre">options</span></tt> object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span> <span class="o">=</span> <span class="n">Values</span><span class="p">()</span>
</pre></div>
</div>
<p>If one of the options in this parser is defined with</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">,</span> <span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>and the command-line being parsed includes any of the following:</p>
<div class="highlight-python"><pre>-ffoo
-f foo
--file=foo
--file foo</pre>
</div>
<p>then <a class="reference internal" href="#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>, on seeing this option, will do the equivalent of</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="s">&quot;foo&quot;</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> option attributes are almost
as important as <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, but <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a> is the only
one that makes sense for <em>all</em> options.</p>
</div>
<div class="section" id="option-attributes">
<span id="optparse-option-attributes"></span><h3>15.5.3.4. Option attributes<a class="headerlink" href="#option-attributes" title="Permalink to this headline">¶</a></h3>
<p>The following option attributes may be passed as keyword arguments to
<a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>.  If you pass an option attribute that is not
relevant to a particular option, or fail to pass a required option attribute,
<a class="reference internal" href="#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> raises <tt class="xref py py-exc docutils literal"><span class="pre">OptionError</span></tt>.</p>
<dl class="attribute">
<dt id="optparse.Option.action">
<tt class="descclassname">Option.</tt><tt class="descname">action</tt><a class="headerlink" href="#optparse.Option.action" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt>)</p>
<p>Determines <a class="reference internal" href="#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>&#8216;s behaviour when this option is seen on the
command line; the available options are documented <a class="reference internal" href="#optparse-standard-option-actions"><em>here</em></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.type">
<tt class="descclassname">Option.</tt><tt class="descname">type</tt><a class="headerlink" href="#optparse.Option.type" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt>)</p>
<p>The argument type expected by this option (e.g., <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;int&quot;</span></tt>);
the available option types are documented <a class="reference internal" href="#optparse-standard-option-types"><em>here</em></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.dest">
<tt class="descclassname">Option.</tt><tt class="descname">dest</tt><a class="headerlink" href="#optparse.Option.dest" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: derived from option strings)</p>
<p>If the option&#8217;s action implies writing or modifying a value somewhere, this
tells <a class="reference internal" href="#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> where to write it: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> names an
attribute of the <tt class="docutils literal"><span class="pre">options</span></tt> object that <a class="reference internal" href="#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> builds as it parses
the command line.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.default">
<tt class="descclassname">Option.</tt><tt class="descname">default</tt><a class="headerlink" href="#optparse.Option.default" title="Permalink to this definition">¶</a></dt>
<dd><p>The value to use for this option&#8217;s destination if the option is not seen on
the command line.  See also <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.set_defaults()</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.nargs">
<tt class="descclassname">Option.</tt><tt class="descname">nargs</tt><a class="headerlink" href="#optparse.Option.nargs" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: 1)</p>
<p>How many arguments of type <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> should be consumed when this
option is seen.  If &gt; 1, <a class="reference internal" href="#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> will store a tuple of values to
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.const">
<tt class="descclassname">Option.</tt><tt class="descname">const</tt><a class="headerlink" href="#optparse.Option.const" title="Permalink to this definition">¶</a></dt>
<dd><p>For actions that store a constant value, the constant value to store.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.choices">
<tt class="descclassname">Option.</tt><tt class="descname">choices</tt><a class="headerlink" href="#optparse.Option.choices" title="Permalink to this definition">¶</a></dt>
<dd><p>For options of type <tt class="docutils literal"><span class="pre">&quot;choice&quot;</span></tt>, the list of strings the user may choose
from.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.callback">
<tt class="descclassname">Option.</tt><tt class="descname">callback</tt><a class="headerlink" href="#optparse.Option.callback" title="Permalink to this definition">¶</a></dt>
<dd><p>For options with action <tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt>, the callable to call when this option
is seen.  See section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a> for detail on the
arguments passed to the callable.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.callback_args">
<tt class="descclassname">Option.</tt><tt class="descname">callback_args</tt><a class="headerlink" href="#optparse.Option.callback_args" title="Permalink to this definition">¶</a></dt>
<dt id="optparse.Option.callback_kwargs">
<tt class="descclassname">Option.</tt><tt class="descname">callback_kwargs</tt><a class="headerlink" href="#optparse.Option.callback_kwargs" title="Permalink to this definition">¶</a></dt>
<dd><p>Additional positional and keyword arguments to pass to <tt class="docutils literal"><span class="pre">callback</span></tt> after the
four standard callback arguments.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.help">
<tt class="descclassname">Option.</tt><tt class="descname">help</tt><a class="headerlink" href="#optparse.Option.help" title="Permalink to this definition">¶</a></dt>
<dd><p>Help text to print for this option when listing all available options after
the user supplies a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> option (such as <tt class="docutils literal"><span class="pre">--help</span></tt>).  If
no help text is supplied, the option will be listed without help text.  To
hide this option, use the special value <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_HELP</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.metavar">
<tt class="descclassname">Option.</tt><tt class="descname">metavar</tt><a class="headerlink" href="#optparse.Option.metavar" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: derived from option strings)</p>
<p>Stand-in for the option argument(s) to use when printing help text.  See
section <a class="reference internal" href="#optparse-tutorial"><em>Tutorial</em></a> for an example.</p>
</dd></dl>

</div>
<div class="section" id="standard-option-actions">
<span id="optparse-standard-option-actions"></span><h3>15.5.3.5. Standard option actions<a class="headerlink" href="#standard-option-actions" title="Permalink to this headline">¶</a></h3>
<p>The various option actions all have slightly different requirements and effects.
Most actions have several relevant option attributes which you may specify to
guide <a class="reference internal" href="#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>&#8216;s behaviour; a few have required attributes, which you
must specify for any option using that action.</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>,
<a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a>]</p>
<p>The option must be followed by an argument, which is converted to a value
according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.  If
<a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> &gt; 1, multiple arguments will be consumed from the
command line; all will be converted according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and
stored to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> as a tuple.  See the
<a class="reference internal" href="#optparse-standard-option-types"><em>Standard option types</em></a> section.</p>
<p>If <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> is supplied (a list or tuple of strings), the type
defaults to <tt class="docutils literal"><span class="pre">&quot;choice&quot;</span></tt>.</p>
<p>If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> is not supplied, it defaults to <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt>.</p>
<p>If <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is not supplied, <a class="reference internal" href="#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> derives a destination
from the first long option string (e.g., <tt class="docutils literal"><span class="pre">--foo-bar</span></tt> implies
<tt class="docutils literal"><span class="pre">foo_bar</span></tt>). If there are no long option strings, <a class="reference internal" href="#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> derives a
destination from the first short option string (e.g., <tt class="docutils literal"><span class="pre">-f</span></tt> implies <tt class="docutils literal"><span class="pre">f</span></tt>).</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-f&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-p&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;float&quot;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;point&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>As it parses the command line</p>
<div class="highlight-python"><pre>-f foo.txt -p 1 -3.5 4 -fbar.txt</pre>
</div>
<p><a class="reference internal" href="#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> will set</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s">&quot;foo.txt&quot;</span>
<span class="n">options</span><span class="o">.</span><span class="n">point</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.5</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">)</span>
<span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s">&quot;bar.txt&quot;</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a>; relevant:
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p>
<p>The value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a> is stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-q&quot;</span><span class="p">,</span> <span class="s">&quot;--quiet&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="s">&quot;--verbose&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--noisy&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">--noisy</span></tt> is seen, <a class="reference internal" href="#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> will set</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="mi">2</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;store_true&quot;</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p>
<p>A special case of <tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt> that stores a true value to
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;store_false&quot;</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p>
<p>Like <tt class="docutils literal"><span class="pre">&quot;store_true&quot;</span></tt>, but stores a false value.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--clobber&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;clobber&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--no-clobber&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;clobber&quot;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>,
<a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a>]</p>
<p>The option must be followed by an argument, which is appended to the list in
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.  If no default value for <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is
supplied, an empty list is automatically created when <a class="reference internal" href="#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> first
encounters this option on the command-line.  If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> &gt; 1,
multiple arguments are consumed, and a tuple of length <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>
is appended to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p>
<p>The defaults for <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> are the same as
for the <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt> action.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-t&quot;</span><span class="p">,</span> <span class="s">&quot;--tracks&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;append&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;int&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">-t3</span></tt> is seen on the command-line, <a class="reference internal" href="#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> does the equivalent
of:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">tracks</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s">&quot;3&quot;</span><span class="p">))</span>
</pre></div>
</div>
<p>If, a little later on, <tt class="docutils literal"><span class="pre">--tracks=4</span></tt> is seen, it does:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s">&quot;4&quot;</span><span class="p">))</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">append</span></tt> action calls the <tt class="docutils literal"><span class="pre">append</span></tt> method on the current value of the
option.  This means that any default value specified must have an <tt class="docutils literal"><span class="pre">append</span></tt>
method.  It also means that if the default value is non-empty, the default
elements will be present in the parsed value for the option, with any values
from the command line appended after those default values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--files&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;append&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;~/.mypkg/defaults&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opts</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="s">&#39;--files&#39;</span><span class="p">,</span> <span class="s">&#39;overrides.mypkg&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opts</span><span class="o">.</span><span class="n">files</span>
<span class="go">[&#39;~/.mypkg/defaults&#39;, &#39;overrides.mypkg&#39;]</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;append_const&quot;</span></tt> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a>; relevant:
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p>
<p>Like <tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt>, but the value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a> is appended to
<a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>; as with <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> defaults to
<tt class="docutils literal"><span class="pre">None</span></tt>, and an empty list is automatically created the first time the option
is encountered.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;count&quot;</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p>
<p>Increment the integer stored at <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.  If no default value is
supplied, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is set to zero before being incremented the
first time.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;count&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbosity&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The first time <tt class="docutils literal"><span class="pre">-v</span></tt> is seen on the command line, <a class="reference internal" href="#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> does the
equivalent of:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
<p>Every subsequent occurrence of <tt class="docutils literal"><span class="pre">-v</span></tt> results in</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt> [required: <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><tt class="xref py py-attr docutils literal"><span class="pre">callback</span></tt></a>; relevant:
<a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a>,
<a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>]</p>
<p>Call the function specified by <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><tt class="xref py py-attr docutils literal"><span class="pre">callback</span></tt></a>, which is called as</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>See section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a> for more detail.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;help&quot;</span></tt></p>
<p>Prints a complete help message for all the options in the current option
parser.  The help message is constructed from the <tt class="docutils literal"><span class="pre">usage</span></tt> string passed to
OptionParser&#8217;s constructor and the <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> string passed to every
option.</p>
<p>If no <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> string is supplied for an option, it will still be
listed in the help message.  To omit an option entirely, use the special value
<tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_HELP</span></tt>.</p>
<p><a class="reference internal" href="#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> automatically adds a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> option to all
OptionParsers, so you do not normally need to create one.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span><span class="p">,</span> <span class="n">SUPPRESS_HELP</span>

<span class="c"># usually, a help option is added automatically, but that can</span>
<span class="c"># be suppressed using the add_help_option argument</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">add_help_option</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-h&quot;</span><span class="p">,</span> <span class="s">&quot;--help&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;help&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;Be moderately verbose&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;filename&quot;</span><span class="p">,</span>
                  <span class="n">help</span><span class="o">=</span><span class="s">&quot;Input file to read data from&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--secret&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">SUPPRESS_HELP</span><span class="p">)</span>
</pre></div>
</div>
<p>If <a class="reference internal" href="#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> sees either <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> on the command line,
it will print something like the following help message to stdout (assuming
<tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is <tt class="docutils literal"><span class="pre">&quot;foo.py&quot;</span></tt>):</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: foo.py [options]

Options:
  -h, --help        Show this help message and exit
  -v                Be moderately verbose
  --file=FILENAME   Input file to read data from
</pre></div>
</div>
<p>After printing the help message, <a class="reference internal" href="#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> terminates your process with
<tt class="docutils literal"><span class="pre">sys.exit(0)</span></tt>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;version&quot;</span></tt></p>
<p>Prints the version number supplied to the OptionParser to stdout and exits.
The version number is actually formatted and printed by the
<tt class="docutils literal"><span class="pre">print_version()</span></tt> method of OptionParser.  Generally only relevant if the
<tt class="docutils literal"><span class="pre">version</span></tt> argument is supplied to the OptionParser constructor.  As with
<a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> options, you will rarely create <tt class="docutils literal"><span class="pre">version</span></tt> options,
since <a class="reference internal" href="#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> automatically adds them when needed.</p>
</li>
</ul>
</div>
<div class="section" id="standard-option-types">
<span id="optparse-standard-option-types"></span><h3>15.5.3.6. Standard option types<a class="headerlink" href="#standard-option-types" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#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> has six built-in option types: <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;int&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;long&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;choice&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;float&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;complex&quot;</span></tt>.  If you need to add new
option types, see section <a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>.</p>
<p>Arguments to string options are not checked or converted in any way: the text on
the command line is stored in the destination (or passed to the callback) as-is.</p>
<p>Integer arguments (type <tt class="docutils literal"><span class="pre">&quot;int&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;long&quot;</span></tt>) are parsed as follows:</p>
<ul class="simple">
<li>if the number starts with <tt class="docutils literal"><span class="pre">0x</span></tt>, it is parsed as a hexadecimal number</li>
<li>if the number starts with <tt class="docutils literal"><span class="pre">0</span></tt>, it is parsed as an octal number</li>
<li>if the number starts with <tt class="docutils literal"><span class="pre">0b</span></tt>, it is parsed as a binary number</li>
<li>otherwise, the number is parsed as a decimal number</li>
</ul>
<p>The conversion is done by calling either <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> or <a class="reference internal" href="functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> with the
appropriate base (2, 8, 10, or 16).  If this fails, so will <a class="reference internal" href="#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>,
although with a more useful error message.</p>
<p><tt class="docutils literal"><span class="pre">&quot;float&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;complex&quot;</span></tt> option arguments are converted directly with
<a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a> and <a class="reference internal" href="functions.html#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a>, with similar error-handling.</p>
<p><tt class="docutils literal"><span class="pre">&quot;choice&quot;</span></tt> options are a subtype of <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt> options.  The
<a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> option attribute (a sequence of strings) defines the
set of allowed option arguments.  <tt class="xref py py-func docutils literal"><span class="pre">optparse.check_choice()</span></tt> compares
user-supplied option arguments against this master list and raises
<tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if an invalid string is given.</p>
</div>
<div class="section" id="parsing-arguments">
<span id="optparse-parsing-arguments"></span><h3>15.5.3.7. Parsing arguments<a class="headerlink" href="#parsing-arguments" title="Permalink to this headline">¶</a></h3>
<p>The whole point of creating and populating an OptionParser is to call its
<tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</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="n">args</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>where the input parameters are</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">args</span></tt></dt>
<dd>the list of arguments to process (default: <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>)</dd>
<dt><tt class="docutils literal"><span class="pre">values</span></tt></dt>
<dd>a <tt class="xref py py-class docutils literal"><span class="pre">optparse.Values</span></tt> object to store option arguments in (default: a
new instance of <tt class="xref py py-class docutils literal"><span class="pre">Values</span></tt>) &#8211; if you give an existing object, the
option defaults will not be initialized on it</dd>
</dl>
<p>and the return values are</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">options</span></tt></dt>
<dd>the same object that was passed in as <tt class="docutils literal"><span class="pre">values</span></tt>, or the optparse.Values
instance created by <a class="reference internal" href="#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></dd>
<dt><tt class="docutils literal"><span class="pre">args</span></tt></dt>
<dd>the leftover positional arguments after all options have been processed</dd>
</dl>
<p>The most common usage is to supply neither keyword argument.  If you supply
<tt class="docutils literal"><span class="pre">values</span></tt>, it will be modified with repeated <a class="reference internal" href="functions.html#setattr" title="setattr"><tt class="xref py py-func docutils literal"><span class="pre">setattr()</span></tt></a> calls (roughly one
for every option argument stored to an option destination) and returned by
<tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>.</p>
<p>If <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> encounters any errors in the argument list, it calls the
OptionParser&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt> method with an appropriate end-user error message.
This ultimately terminates your process with an exit status of 2 (the
traditional Unix exit status for command-line errors).</p>
</div>
<div class="section" id="querying-and-manipulating-your-option-parser">
<span id="optparse-querying-manipulating-option-parser"></span><h3>15.5.3.8. Querying and manipulating your option parser<a class="headerlink" href="#querying-and-manipulating-your-option-parser" title="Permalink to this headline">¶</a></h3>
<p>The default behavior of the option parser can be customized slightly, and you
can also poke around your option parser and see what&#8217;s there.  OptionParser
provides several methods to help you out:</p>
<dl class="method">
<dt id="optparse.OptionParser.disable_interspersed_args">
<tt class="descclassname">OptionParser.</tt><tt class="descname">disable_interspersed_args</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.disable_interspersed_args" title="Permalink to this definition">¶</a></dt>
<dd><p>Set parsing to stop on the first non-option.  For example, if <tt class="docutils literal"><span class="pre">-a</span></tt> and
<tt class="docutils literal"><span class="pre">-b</span></tt> are both simple options that take no arguments, <a class="reference internal" href="#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>
normally accepts this syntax:</p>
<div class="highlight-python"><pre>prog -a arg1 -b arg2</pre>
</div>
<p>and treats it as equivalent to</p>
<div class="highlight-python"><pre>prog -a -b arg1 arg2</pre>
</div>
<p>To disable this feature, call <a class="reference internal" href="#optparse.OptionParser.disable_interspersed_args" title="optparse.OptionParser.disable_interspersed_args"><tt class="xref py py-meth docutils literal"><span class="pre">disable_interspersed_args()</span></tt></a>.  This
restores traditional Unix syntax, where option parsing stops with the first
non-option argument.</p>
<p>Use this if you have a command processor which runs another command which has
options of its own and you want to make sure these options don&#8217;t get
confused.  For example, each command might have a different set of options.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.enable_interspersed_args">
<tt class="descclassname">OptionParser.</tt><tt class="descname">enable_interspersed_args</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.enable_interspersed_args" title="Permalink to this definition">¶</a></dt>
<dd><p>Set parsing to not stop on the first non-option, allowing interspersing
switches with command arguments.  This is the default behavior.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.get_option">
<tt class="descclassname">OptionParser.</tt><tt class="descname">get_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_option" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the Option instance with the option string <em>opt_str</em>, or <tt class="docutils literal"><span class="pre">None</span></tt> if
no options have that option string.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.has_option">
<tt class="descclassname">OptionParser.</tt><tt class="descname">has_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.has_option" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the OptionParser has an option with option string <em>opt_str</em>
(e.g., <tt class="docutils literal"><span class="pre">-q</span></tt> or <tt class="docutils literal"><span class="pre">--verbose</span></tt>).</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.remove_option">
<tt class="descclassname">OptionParser.</tt><tt class="descname">remove_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.remove_option" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> has an option corresponding to <em>opt_str</em>, that
option is removed.  If that option provided any other option strings, all of
those option strings become invalid. If <em>opt_str</em> does not occur in any
option belonging to this <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>, raises <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
</dd></dl>

</div>
<div class="section" id="conflicts-between-options">
<span id="optparse-conflicts-between-options"></span><h3>15.5.3.9. Conflicts between options<a class="headerlink" href="#conflicts-between-options" title="Permalink to this headline">¶</a></h3>
<p>If you&#8217;re not careful, it&#8217;s easy to define options with conflicting option
strings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-n&quot;</span><span class="p">,</span> <span class="s">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-n&quot;</span><span class="p">,</span> <span class="s">&quot;--noisy&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>(This is particularly true if you&#8217;ve defined your own OptionParser subclass with
some standard options.)</p>
<p>Every time you add an option, <a class="reference internal" href="#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> checks for conflicts with existing
options.  If it finds any, it invokes the current conflict-handling mechanism.
You can set the conflict-handling mechanism either in the constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">conflict_handler</span><span class="o">=</span><span class="n">handler</span><span class="p">)</span>
</pre></div>
</div>
<p>or with a separate call:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">set_conflict_handler</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
</pre></div>
</div>
<p>The available conflict handlers are:</p>
<blockquote>
<div><dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">&quot;error&quot;</span></tt> (default)</dt>
<dd>assume option conflicts are a programming error and raise
<tt class="xref py py-exc docutils literal"><span class="pre">OptionConflictError</span></tt></dd>
<dt><tt class="docutils literal"><span class="pre">&quot;resolve&quot;</span></tt></dt>
<dd>resolve option conflicts intelligently (see below)</dd>
</dl>
</div></blockquote>
<p>As an example, let&#8217;s define an <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> that resolves conflicts
intelligently and add conflicting options to it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">conflict_handler</span><span class="o">=</span><span class="s">&quot;resolve&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-n&quot;</span><span class="p">,</span> <span class="s">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;do no harm&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-n&quot;</span><span class="p">,</span> <span class="s">&quot;--noisy&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;be noisy&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>At this point, <a class="reference internal" href="#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> detects that a previously-added option is already
using the <tt class="docutils literal"><span class="pre">-n</span></tt> option string.  Since <tt class="docutils literal"><span class="pre">conflict_handler</span></tt> is <tt class="docutils literal"><span class="pre">&quot;resolve&quot;</span></tt>,
it resolves the situation by removing <tt class="docutils literal"><span class="pre">-n</span></tt> from the earlier option&#8217;s list of
option strings.  Now <tt class="docutils literal"><span class="pre">--dry-run</span></tt> is the only way for the user to activate
that option.  If the user asks for help, the help message will reflect that:</p>
<div class="highlight-python"><pre>Options:
  --dry-run     do no harm
  [...]
  -n, --noisy   be noisy</pre>
</div>
<p>It&#8217;s possible to whittle away the option strings for a previously-added option
until there are none left, and the user has no way of invoking that option from
the command-line.  In that case, <a class="reference internal" href="#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> removes that option completely,
so it doesn&#8217;t show up in help text or anywhere else. Carrying on with our
existing OptionParser:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;new dry-run option&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>At this point, the original <tt class="docutils literal"><span class="pre">-n</span></tt>/<tt class="docutils literal"><span class="pre">--dry-run</span></tt> option is no longer
accessible, so <a class="reference internal" href="#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> removes it, leaving this help text:</p>
<div class="highlight-python"><pre>Options:
  [...]
  -n, --noisy   be noisy
  --dry-run     new dry-run option</pre>
</div>
</div>
<div class="section" id="cleanup">
<span id="optparse-cleanup"></span><h3>15.5.3.10. Cleanup<a class="headerlink" href="#cleanup" title="Permalink to this headline">¶</a></h3>
<p>OptionParser instances have several cyclic references.  This should not be a
problem for Python&#8217;s garbage collector, but you may wish to break the cyclic
references explicitly by calling <tt class="xref py py-meth docutils literal"><span class="pre">destroy()</span></tt> on your
OptionParser once you are done with it.  This is particularly useful in
long-running applications where large object graphs are reachable from your
OptionParser.</p>
</div>
<div class="section" id="other-methods">
<span id="optparse-other-methods"></span><h3>15.5.3.11. Other methods<a class="headerlink" href="#other-methods" title="Permalink to this headline">¶</a></h3>
<p>OptionParser supports several other public methods:</p>
<dl class="method">
<dt id="optparse.OptionParser.set_usage">
<tt class="descclassname">OptionParser.</tt><tt class="descname">set_usage</tt><big>(</big><em>usage</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.set_usage" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the usage string according to the rules described above for the <tt class="docutils literal"><span class="pre">usage</span></tt>
constructor keyword argument.  Passing <tt class="docutils literal"><span class="pre">None</span></tt> sets the default usage
string; use <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_USAGE</span></tt> to suppress a usage message.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.print_usage">
<tt class="descclassname">OptionParser.</tt><tt class="descname">print_usage</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.print_usage" title="Permalink to this definition">¶</a></dt>
<dd><p>Print the usage message for the current program (<tt class="docutils literal"><span class="pre">self.usage</span></tt>) to <em>file</em>
(default stdout).  Any occurrence of the string <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">self.usage</span></tt>
is replaced with the name of the current program.  Does nothing if
<tt class="docutils literal"><span class="pre">self.usage</span></tt> is empty or not defined.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.get_usage">
<tt class="descclassname">OptionParser.</tt><tt class="descname">get_usage</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_usage" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><tt class="xref py py-meth docutils literal"><span class="pre">print_usage()</span></tt></a> but returns the usage string instead of
printing it.</p>
</dd></dl>

<dl class="method">
<dt id="optparse.OptionParser.set_defaults">
<tt class="descclassname">OptionParser.</tt><tt class="descname">set_defaults</tt><big>(</big><em>dest=value</em>, <em>...</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.set_defaults" title="Permalink to this definition">¶</a></dt>
<dd><p>Set default values for several option destinations at once.  Using
<a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a> is the preferred way to set default values for options,
since multiple options can share the same destination.  For example, if
several &#8220;mode&#8221; options all set the same destination, any one of them can set
the default, and the last one wins:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--advanced&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span>
                  <span class="n">dest</span><span class="o">=</span><span class="s">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">&quot;advanced&quot;</span><span class="p">,</span>
                  <span class="n">default</span><span class="o">=</span><span class="s">&quot;novice&quot;</span><span class="p">)</span>    <span class="c"># overridden below</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--novice&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span>
                  <span class="n">dest</span><span class="o">=</span><span class="s">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">&quot;novice&quot;</span><span class="p">,</span>
                  <span class="n">default</span><span class="o">=</span><span class="s">&quot;advanced&quot;</span><span class="p">)</span>  <span class="c"># overrides above setting</span>
</pre></div>
</div>
<p>To avoid this confusion, use <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.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="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s">&quot;advanced&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--advanced&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span>
                  <span class="n">dest</span><span class="o">=</span><span class="s">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">&quot;advanced&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--novice&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_const&quot;</span><span class="p">,</span>
                  <span class="n">dest</span><span class="o">=</span><span class="s">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">&quot;novice&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="option-callbacks">
<span id="optparse-option-callbacks"></span><h2>15.5.4. Option Callbacks<a class="headerlink" href="#option-callbacks" title="Permalink to this headline">¶</a></h2>
<p>When <a class="reference internal" href="#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>&#8216;s built-in actions and types aren&#8217;t quite enough for your
needs, you have two choices: extend <a class="reference internal" href="#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> or define a callback option.
Extending <a class="reference internal" href="#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> is more general, but overkill for a lot of simple
cases.  Quite often a simple callback is all you need.</p>
<p>There are two steps to defining a callback option:</p>
<ul class="simple">
<li>define the option itself using the <tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt> action</li>
<li>write the callback; this is a function (or method) that takes at least four
arguments, as described below</li>
</ul>
<div class="section" id="defining-a-callback-option">
<span id="optparse-defining-callback-option"></span><h3>15.5.4.1. Defining a callback option<a class="headerlink" href="#defining-a-callback-option" title="Permalink to this headline">¶</a></h3>
<p>As always, the easiest way to define a callback option is by using the
<a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a> method.  Apart from <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, the
only option attribute you must specify is <tt class="docutils literal"><span class="pre">callback</span></tt>, the function to call:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">my_callback</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">callback</span></tt> is a function (or other callable object), so you must have already
defined <tt class="docutils literal"><span class="pre">my_callback()</span></tt> when you create this callback option. In this simple
case, <a class="reference internal" href="#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> doesn&#8217;t even know if <tt class="docutils literal"><span class="pre">-c</span></tt> takes any arguments,
which usually means that the option takes no arguments&#8212;the mere presence of
<tt class="docutils literal"><span class="pre">-c</span></tt> on the command-line is all it needs to know.  In some
circumstances, though, you might want your callback to consume an arbitrary
number of command-line arguments.  This is where writing callbacks gets tricky;
it&#8217;s covered later in this section.</p>
<p><a class="reference internal" href="#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> always passes four particular arguments to your callback, and it
will only pass additional arguments if you specify them via
<a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a> and <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>.  Thus, the
minimal callback function signature is:</p>
<div class="highlight-python"><pre>def my_callback(option, opt, value, parser):</pre>
</div>
<p>The four arguments to a callback are described below.</p>
<p>There are several other option attributes that you can supply when you define a
callback option:</p>
<dl class="docutils">
<dt><a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a></dt>
<dd>has its usual meaning: as with the <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt> actions, it
instructs <a class="reference internal" href="#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 consume one argument and convert it to
<a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>.  Rather than storing the converted value(s) anywhere,
though, <a class="reference internal" href="#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> passes it to your callback function.</dd>
<dt><a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a></dt>
<dd>also has its usual meaning: if it is supplied and &gt; 1, <a class="reference internal" href="#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> will
consume <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> arguments, each of which must be convertible to
<a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>.  It then passes a tuple of converted values to your
callback.</dd>
<dt><a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a></dt>
<dd>a tuple of extra positional arguments to pass to the callback</dd>
<dt><a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a></dt>
<dd>a dictionary of extra keyword arguments to pass to the callback</dd>
</dl>
</div>
<div class="section" id="how-callbacks-are-called">
<span id="optparse-how-callbacks-called"></span><h3>15.5.4.2. How callbacks are called<a class="headerlink" href="#how-callbacks-are-called" title="Permalink to this headline">¶</a></h3>
<p>All callbacks are called as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>where</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">option</span></tt></dt>
<dd>is the Option instance that&#8217;s calling the callback</dd>
<dt><tt class="docutils literal"><span class="pre">opt_str</span></tt></dt>
<dd>is the option string seen on the command-line that&#8217;s triggering the callback.
(If an abbreviated long option was used, <tt class="docutils literal"><span class="pre">opt_str</span></tt> will be the full,
canonical option string&#8212;e.g. if the user puts <tt class="docutils literal"><span class="pre">--foo</span></tt> on the
command-line as an abbreviation for <tt class="docutils literal"><span class="pre">--foobar</span></tt>, then <tt class="docutils literal"><span class="pre">opt_str</span></tt> will be
<tt class="docutils literal"><span class="pre">&quot;--foobar&quot;</span></tt>.)</dd>
<dt><tt class="docutils literal"><span class="pre">value</span></tt></dt>
<dd>is the argument to this option seen on the command-line.  <a class="reference internal" href="#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> will
only expect an argument if <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> is set; the type of <tt class="docutils literal"><span class="pre">value</span></tt> will be
the type implied by the option&#8217;s type.  If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> for this option is
<tt class="docutils literal"><span class="pre">None</span></tt> (no argument expected), then <tt class="docutils literal"><span class="pre">value</span></tt> will be <tt class="docutils literal"><span class="pre">None</span></tt>.  If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>
&gt; 1, <tt class="docutils literal"><span class="pre">value</span></tt> will be a tuple of values of the appropriate type.</dd>
<dt><tt class="docutils literal"><span class="pre">parser</span></tt></dt>
<dd><p class="first">is the OptionParser instance driving the whole thing, mainly useful because
you can access some other interesting data through its instance attributes:</p>
<dl class="last docutils">
<dt><tt class="docutils literal"><span class="pre">parser.largs</span></tt></dt>
<dd>the current list of leftover arguments, ie. arguments that have been
consumed but are neither options nor option arguments. Feel free to modify
<tt class="docutils literal"><span class="pre">parser.largs</span></tt>, e.g. by adding more arguments to it.  (This list will
become <tt class="docutils literal"><span class="pre">args</span></tt>, the second return value of <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>.)</dd>
<dt><tt class="docutils literal"><span class="pre">parser.rargs</span></tt></dt>
<dd>the current list of remaining arguments, ie. with <tt class="docutils literal"><span class="pre">opt_str</span></tt> and
<tt class="docutils literal"><span class="pre">value</span></tt> (if applicable) removed, and only the arguments following them
still there.  Feel free to modify <tt class="docutils literal"><span class="pre">parser.rargs</span></tt>, e.g. by consuming more
arguments.</dd>
<dt><tt class="docutils literal"><span class="pre">parser.values</span></tt></dt>
<dd>the object where option values are by default stored (an instance of
optparse.OptionValues).  This lets callbacks use the same mechanism as the
rest of <a class="reference internal" href="#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> for storing option values; you don&#8217;t need to mess
around with globals or closures.  You can also access or modify the
value(s) of any options already encountered on the command-line.</dd>
</dl>
</dd>
<dt><tt class="docutils literal"><span class="pre">args</span></tt></dt>
<dd>is a tuple of arbitrary positional arguments supplied via the
<a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a> option attribute.</dd>
<dt><tt class="docutils literal"><span class="pre">kwargs</span></tt></dt>
<dd>is a dictionary of arbitrary keyword arguments supplied via
<a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>.</dd>
</dl>
</div>
<div class="section" id="raising-errors-in-a-callback">
<span id="optparse-raising-errors-in-callback"></span><h3>15.5.4.3. Raising errors in a callback<a class="headerlink" href="#raising-errors-in-a-callback" title="Permalink to this headline">¶</a></h3>
<p>The callback function should raise <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if there are any
problems with the option or its argument(s).  <a class="reference internal" href="#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> catches this and
terminates the program, printing the error message you supply to stderr.  Your
message should be clear, concise, accurate, and mention the option at fault.
Otherwise, the user will have a hard time figuring out what he did wrong.</p>
</div>
<div class="section" id="callback-example-1-trivial-callback">
<span id="optparse-callback-example-1"></span><h3>15.5.4.4. Callback example 1: trivial callback<a class="headerlink" href="#callback-example-1-trivial-callback" title="Permalink to this headline">¶</a></h3>
<p>Here&#8217;s an example of a callback option that takes no arguments, and simply
records that the option was seen:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">record_foo_seen</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">saw_foo</span> <span class="o">=</span> <span class="bp">True</span>

<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--foo&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">record_foo_seen</span><span class="p">)</span>
</pre></div>
</div>
<p>Of course, you could do that with the <tt class="docutils literal"><span class="pre">&quot;store_true&quot;</span></tt> action.</p>
</div>
<div class="section" id="callback-example-2-check-option-order">
<span id="optparse-callback-example-2"></span><h3>15.5.4.5. Callback example 2: check option order<a class="headerlink" href="#callback-example-2-check-option-order" title="Permalink to this headline">¶</a></h3>
<p>Here&#8217;s a slightly more interesting example: record the fact that <tt class="docutils literal"><span class="pre">-a</span></tt> is
seen, but blow up if it comes after <tt class="docutils literal"><span class="pre">-b</span></tt> in the command-line.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">&quot;can&#39;t use -a after -b&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-a&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-b&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;b&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="callback-example-3-check-option-order-generalized">
<span id="optparse-callback-example-3"></span><h3>15.5.4.6. Callback example 3: check option order (generalized)<a class="headerlink" href="#callback-example-3-check-option-order-generalized" title="Permalink to this headline">¶</a></h3>
<p>If you want to re-use this callback for several similar options (set a flag, but
blow up if <tt class="docutils literal"><span class="pre">-b</span></tt> has already been seen), it needs a bit of work: the error
message and the flag that it sets must be generalized.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">&quot;can&#39;t use </span><span class="si">%s</span><span class="s"> after -b&quot;</span> <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-a&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;a&#39;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-b&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;b&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;c&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="callback-example-4-check-arbitrary-condition">
<span id="optparse-callback-example-4"></span><h3>15.5.4.7. Callback example 4: check arbitrary condition<a class="headerlink" href="#callback-example-4-check-arbitrary-condition" title="Permalink to this headline">¶</a></h3>
<p>Of course, you could put any condition in there&#8212;you&#8217;re not limited to checking
the values of already-defined options.  For example, if you have options that
should not be called when the moon is full, all you have to do is this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_moon</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">is_moon_full</span><span class="p">():</span>
        <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> option invalid when moon is full&quot;</span>
                               <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--foo&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_moon</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;foo&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>(The definition of <tt class="docutils literal"><span class="pre">is_moon_full()</span></tt> is left as an exercise for the reader.)</p>
</div>
<div class="section" id="callback-example-5-fixed-arguments">
<span id="optparse-callback-example-5"></span><h3>15.5.4.8. Callback example 5: fixed arguments<a class="headerlink" href="#callback-example-5-fixed-arguments" title="Permalink to this headline">¶</a></h3>
<p>Things get slightly more interesting when you define callback options that take
a fixed number of arguments.  Specifying that a callback option takes arguments
is similar to defining a <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt> option: if you define
<a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, then the option takes one argument that must be
convertible to that type; if you further define <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, then the
option takes <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> arguments.</p>
<p>Here&#8217;s an example that just emulates the standard <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt> action:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">store_value</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--foo&quot;</span><span class="p">,</span>
                  <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">store_value</span><span class="p">,</span>
                  <span class="nb">type</span><span class="o">=</span><span class="s">&quot;int&quot;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;foo&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that <a class="reference internal" href="#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> takes care of consuming 3 arguments and converting
them to integers for you; all you have to do is store them.  (Or whatever;
obviously you don&#8217;t need a callback for this example.)</p>
</div>
<div class="section" id="callback-example-6-variable-arguments">
<span id="optparse-callback-example-6"></span><h3>15.5.4.9. Callback example 6: variable arguments<a class="headerlink" href="#callback-example-6-variable-arguments" title="Permalink to this headline">¶</a></h3>
<p>Things get hairy when you want an option to take a variable number of arguments.
For this case, you must write a callback, as <a class="reference internal" href="#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> doesn&#8217;t provide any
built-in capabilities for it.  And you have to deal with certain intricacies of
conventional Unix command-line parsing that <a class="reference internal" href="#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> normally handles for
you.  In particular, callbacks should implement the conventional rules for bare
<tt class="docutils literal"><span class="pre">--</span></tt> and <tt class="docutils literal"><span class="pre">-</span></tt> arguments:</p>
<ul class="simple">
<li>either <tt class="docutils literal"><span class="pre">--</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt> can be option arguments</li>
<li>bare <tt class="docutils literal"><span class="pre">--</span></tt> (if not the argument to some option): halt command-line
processing and discard the <tt class="docutils literal"><span class="pre">--</span></tt></li>
<li>bare <tt class="docutils literal"><span class="pre">-</span></tt> (if not the argument to some option): halt command-line
processing but keep the <tt class="docutils literal"><span class="pre">-</span></tt> (append it to <tt class="docutils literal"><span class="pre">parser.largs</span></tt>)</li>
</ul>
<p>If you want an option that takes a variable number of arguments, there are
several subtle, tricky issues to worry about.  The exact implementation you
choose will be based on which trade-offs you&#8217;re willing to make for your
application (which is why <a class="reference internal" href="#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> doesn&#8217;t support this sort of thing
directly).</p>
<p>Nevertheless, here&#8217;s a stab at a callback for an option with variable
arguments:</p>
<div class="highlight-python"><pre> def vararg_callback(option, opt_str, value, parser):
     assert value is None
     value = []

     def floatable(str):
         try:
             float(str)
             return True
         except ValueError:
             return False

     for arg in parser.rargs:
         # stop on --foo like options
         if arg[:2] == "--" and len(arg) &gt; 2:
             break
         # stop on -a, but not on -3 or -3.0
         if arg[:1] == "-" and len(arg) &gt; 1 and not floatable(arg):
             break
         value.append(arg)

     del parser.rargs[:len(value)]
     setattr(parser.values, option.dest, value)

[...]
parser.add_option("-c", "--callback", dest="vararg_attr",
                  action="callback", callback=vararg_callback)</pre>
</div>
</div>
</div>
<div class="section" id="extending-optparse">
<span id="optparse-extending-optparse"></span><h2>15.5.5. Extending <a class="reference internal" href="#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><a class="headerlink" href="#extending-optparse" title="Permalink to this headline">¶</a></h2>
<p>Since the two major controlling factors in how <a class="reference internal" href="#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> interprets
command-line options are the action and type of each option, the most likely
direction of extension is to add new actions and new types.</p>
<div class="section" id="adding-new-types">
<span id="optparse-adding-new-types"></span><h3>15.5.5.1. Adding new types<a class="headerlink" href="#adding-new-types" title="Permalink to this headline">¶</a></h3>
<p>To add new types, you need to define your own subclass of <a class="reference internal" href="#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>&#8216;s
<tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> class.  This class has a couple of attributes that define
<a class="reference internal" href="#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>&#8216;s types: <a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><tt class="xref py py-attr docutils literal"><span class="pre">TYPES</span></tt></a> and <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a>.</p>
<dl class="attribute">
<dt id="optparse.Option.TYPES">
<tt class="descclassname">Option.</tt><tt class="descname">TYPES</tt><a class="headerlink" href="#optparse.Option.TYPES" title="Permalink to this definition">¶</a></dt>
<dd><p>A tuple of type names; in your subclass, simply define a new tuple
<a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><tt class="xref py py-attr docutils literal"><span class="pre">TYPES</span></tt></a> that builds on the standard one.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.TYPE_CHECKER">
<tt class="descclassname">Option.</tt><tt class="descname">TYPE_CHECKER</tt><a class="headerlink" href="#optparse.Option.TYPE_CHECKER" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary mapping type names to type-checking functions.  A type-checking
function has the following signature:</p>
<div class="highlight-python"><pre>def check_mytype(option, opt, value)</pre>
</div>
<p>where <tt class="docutils literal"><span class="pre">option</span></tt> is an <tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> instance, <tt class="docutils literal"><span class="pre">opt</span></tt> is an option string
(e.g., <tt class="docutils literal"><span class="pre">-f</span></tt>), and <tt class="docutils literal"><span class="pre">value</span></tt> is the string from the command line that must
be checked and converted to your desired type.  <tt class="docutils literal"><span class="pre">check_mytype()</span></tt> should
return an object of the hypothetical type <tt class="docutils literal"><span class="pre">mytype</span></tt>.  The value returned by
a type-checking function will wind up in the OptionValues instance returned
by <tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.parse_args()</span></tt>, or be passed to a callback as the
<tt class="docutils literal"><span class="pre">value</span></tt> parameter.</p>
<p>Your type-checking function should raise <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if it
encounters any problems.  <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> takes a single string
argument, which is passed as-is to <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>&#8216;s <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt>
method, which in turn prepends the program name and the string <tt class="docutils literal"><span class="pre">&quot;error:&quot;</span></tt>
and prints everything to stderr before terminating the process.</p>
</dd></dl>

<p>Here&#8217;s a silly example that demonstrates adding a <tt class="docutils literal"><span class="pre">&quot;complex&quot;</span></tt> option type to
parse Python-style complex numbers on the command line.  (This is even sillier
than it used to be, because <a class="reference internal" href="#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> 1.3 added built-in support for
complex numbers, but never mind.)</p>
<p>First, the necessary imports:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">copy</span>
<span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">Option</span><span class="p">,</span> <span class="n">OptionValueError</span>
</pre></div>
</div>
<p>You need to define your type-checker first, since it&#8217;s referred to later (in the
<a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a> class attribute of your Option subclass):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_complex</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">complex</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span>
            <span class="s">&quot;option </span><span class="si">%s</span><span class="s">: invalid complex value: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
</pre></div>
</div>
<p>Finally, the Option subclass:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyOption</span> <span class="p">(</span><span class="n">Option</span><span class="p">):</span>
    <span class="n">TYPES</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPES</span> <span class="o">+</span> <span class="p">(</span><span class="s">&quot;complex&quot;</span><span class="p">,)</span>
    <span class="n">TYPE_CHECKER</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">Option</span><span class="o">.</span><span class="n">TYPE_CHECKER</span><span class="p">)</span>
    <span class="n">TYPE_CHECKER</span><span class="p">[</span><span class="s">&quot;complex&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">check_complex</span>
</pre></div>
</div>
<p>(If we didn&#8217;t make a <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-func docutils literal"><span class="pre">copy()</span></tt></a> of <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">Option.TYPE_CHECKER</span></tt></a>, we would end
up modifying the <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a> attribute of <a class="reference internal" href="#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>&#8216;s
Option class.  This being Python, nothing stops you from doing that except good
manners and common sense.)</p>
<p>That&#8217;s it!  Now you can write a script that uses the new option type just like
any other <a class="reference internal" href="#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>-based script, except you have to instruct your
OptionParser to use MyOption instead of Option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_class</span><span class="o">=</span><span class="n">MyOption</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-c&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;complex&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Alternately, you can build your own option list and pass it to OptionParser; if
you don&#8217;t use <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> in the above way, you don&#8217;t need to tell
OptionParser which option class to use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyOption</span><span class="p">(</span><span class="s">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;complex&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;c&quot;</span><span class="p">)]</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="adding-new-actions">
<span id="optparse-adding-new-actions"></span><h3>15.5.5.2. Adding new actions<a class="headerlink" href="#adding-new-actions" title="Permalink to this headline">¶</a></h3>
<p>Adding new actions is a bit trickier, because you have to understand that
<a class="reference internal" href="#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> has a couple of classifications for actions:</p>
<dl class="docutils">
<dt>&#8220;store&#8221; actions</dt>
<dd>actions that result in <a class="reference internal" href="#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> storing a value to an attribute of the
current OptionValues instance; these options require a <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>
attribute to be supplied to the Option constructor.</dd>
<dt>&#8220;typed&#8221; actions</dt>
<dd>actions that take a value from the command line and expect it to be of a
certain type; or rather, a string that can be converted to a certain type.
These options require a <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> attribute to the Option
constructor.</dd>
</dl>
<p>These are overlapping sets: some default &#8220;store&#8221; actions are <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;store_const&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;count&quot;</span></tt>, while the default &#8220;typed&#8221;
actions are <tt class="docutils literal"><span class="pre">&quot;store&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;callback&quot;</span></tt>.</p>
<p>When you add an action, you need to categorize it by listing it in at least one
of the following class attributes of Option (all are lists of strings):</p>
<dl class="attribute">
<dt id="optparse.Option.ACTIONS">
<tt class="descclassname">Option.</tt><tt class="descname">ACTIONS</tt><a class="headerlink" href="#optparse.Option.ACTIONS" title="Permalink to this definition">¶</a></dt>
<dd><p>All actions must be listed in ACTIONS.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.STORE_ACTIONS">
<tt class="descclassname">Option.</tt><tt class="descname">STORE_ACTIONS</tt><a class="headerlink" href="#optparse.Option.STORE_ACTIONS" title="Permalink to this definition">¶</a></dt>
<dd><p>&#8220;store&#8221; actions are additionally listed here.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.TYPED_ACTIONS">
<tt class="descclassname">Option.</tt><tt class="descname">TYPED_ACTIONS</tt><a class="headerlink" href="#optparse.Option.TYPED_ACTIONS" title="Permalink to this definition">¶</a></dt>
<dd><p>&#8220;typed&#8221; actions are additionally listed here.</p>
</dd></dl>

<dl class="attribute">
<dt id="optparse.Option.ALWAYS_TYPED_ACTIONS">
<tt class="descclassname">Option.</tt><tt class="descname">ALWAYS_TYPED_ACTIONS</tt><a class="headerlink" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="Permalink to this definition">¶</a></dt>
<dd><p>Actions that always take a type (i.e. whose options always take a value) are
additionally listed here.  The only effect of this is that <a class="reference internal" href="#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>
assigns the default type, <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt>, to options with no explicit type
whose action is listed in <a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">ALWAYS_TYPED_ACTIONS</span></tt></a>.</p>
</dd></dl>

<p>In order to actually implement your new action, you must override Option&#8217;s
<tt class="xref py py-meth docutils literal"><span class="pre">take_action()</span></tt> method and add a case that recognizes your action.</p>
<p>For example, let&#8217;s add an <tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> action.  This is similar to the standard
<tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt> action, but instead of taking a single value from the command-line
and appending it to an existing list, <tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> will take multiple values in
a single comma-delimited string, and extend an existing list with them.  That
is, if <tt class="docutils literal"><span class="pre">--names</span></tt> is an <tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> option of type <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt>, the command
line</p>
<div class="highlight-python"><pre>--names=foo,bar --names blah --names ding,dong</pre>
</div>
<p>would result in a list</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;blah&quot;</span><span class="p">,</span> <span class="s">&quot;ding&quot;</span><span class="p">,</span> <span class="s">&quot;dong&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>Again we define a subclass of Option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyOption</span><span class="p">(</span><span class="n">Option</span><span class="p">):</span>

    <span class="n">ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">&quot;extend&quot;</span><span class="p">,)</span>
    <span class="n">STORE_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">STORE_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">&quot;extend&quot;</span><span class="p">,)</span>
    <span class="n">TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">&quot;extend&quot;</span><span class="p">,)</span>
    <span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">&quot;extend&quot;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s">&quot;extend&quot;</span><span class="p">:</span>
            <span class="n">lvalue</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)</span>
            <span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">lvalue</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">Option</span><span class="o">.</span><span class="n">take_action</span><span class="p">(</span>
                <span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
</pre></div>
</div>
<p>Features of note:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> both expects a value on the command-line and stores that value
somewhere, so it goes in both <a class="reference internal" href="#optparse.Option.STORE_ACTIONS" title="optparse.Option.STORE_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">STORE_ACTIONS</span></tt></a> and
<a class="reference internal" href="#optparse.Option.TYPED_ACTIONS" title="optparse.Option.TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">TYPED_ACTIONS</span></tt></a>.</p>
</li>
<li><p class="first">to ensure that <a class="reference internal" href="#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> assigns the default type of <tt class="docutils literal"><span class="pre">&quot;string&quot;</span></tt> to
<tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> actions, we put the <tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt> action in
<a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">ALWAYS_TYPED_ACTIONS</span></tt></a> as well.</p>
</li>
<li><p class="first"><tt class="xref py py-meth docutils literal"><span class="pre">MyOption.take_action()</span></tt> implements just this one new action, and passes
control back to <tt class="xref py py-meth docutils literal"><span class="pre">Option.take_action()</span></tt> for the standard <a class="reference internal" href="#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>
actions.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">values</span></tt> is an instance of the optparse_parser.Values class, which provides
the very useful <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> method. <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> is
essentially <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> with a safety valve; it is called as</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>If the <tt class="docutils literal"><span class="pre">attr</span></tt> attribute of <tt class="docutils literal"><span class="pre">values</span></tt> doesn&#8217;t exist or is None, then
ensure_value() first sets it to <tt class="docutils literal"><span class="pre">value</span></tt>, and then returns &#8216;value. This is
very handy for actions like <tt class="docutils literal"><span class="pre">&quot;extend&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;append&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;count&quot;</span></tt>, all
of which accumulate data in a variable and expect that variable to be of a
certain type (a list for the first two, an integer for the latter).  Using
<tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> means that scripts using your action don&#8217;t have to worry
about setting a default value for the option destinations in question; they
can just leave the default as None and <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> will take care of
getting it right when it&#8217;s needed.</p>
</li>
</ul>
</div>
</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.5. <tt class="docutils literal"><span class="pre">optparse</span></tt> &#8212; Parser for command line options</a><ul>
<li><a class="reference internal" href="#background">15.5.1. Background</a><ul>
<li><a class="reference internal" href="#terminology">15.5.1.1. Terminology</a></li>
<li><a class="reference internal" href="#what-are-options-for">15.5.1.2. What are options for?</a></li>
<li><a class="reference internal" href="#what-are-positional-arguments-for">15.5.1.3. What are positional arguments for?</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tutorial">15.5.2. Tutorial</a><ul>
<li><a class="reference internal" href="#understanding-option-actions">15.5.2.1. Understanding option actions</a></li>
<li><a class="reference internal" href="#the-store-action">15.5.2.2. The store action</a></li>
<li><a class="reference internal" href="#handling-boolean-flag-options">15.5.2.3. Handling boolean (flag) options</a></li>
<li><a class="reference internal" href="#other-actions">15.5.2.4. Other actions</a></li>
<li><a class="reference internal" href="#default-values">15.5.2.5. Default values</a></li>
<li><a class="reference internal" href="#generating-help">15.5.2.6. Generating help</a><ul>
<li><a class="reference internal" href="#grouping-options">15.5.2.6.1. Grouping Options</a></li>
</ul>
</li>
<li><a class="reference internal" href="#printing-a-version-string">15.5.2.7. Printing a version string</a></li>
<li><a class="reference internal" href="#how-optparse-handles-errors">15.5.2.8. How <tt class="docutils literal"><span class="pre">optparse</span></tt> handles errors</a></li>
<li><a class="reference internal" href="#putting-it-all-together">15.5.2.9. Putting it all together</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reference-guide">15.5.3. Reference Guide</a><ul>
<li><a class="reference internal" href="#creating-the-parser">15.5.3.1. Creating the parser</a></li>
<li><a class="reference internal" href="#populating-the-parser">15.5.3.2. Populating the parser</a></li>
<li><a class="reference internal" href="#defining-options">15.5.3.3. Defining options</a></li>
<li><a class="reference internal" href="#option-attributes">15.5.3.4. Option attributes</a></li>
<li><a class="reference internal" href="#standard-option-actions">15.5.3.5. Standard option actions</a></li>
<li><a class="reference internal" href="#standard-option-types">15.5.3.6. Standard option types</a></li>
<li><a class="reference internal" href="#parsing-arguments">15.5.3.7. Parsing arguments</a></li>
<li><a class="reference internal" href="#querying-and-manipulating-your-option-parser">15.5.3.8. Querying and manipulating your option parser</a></li>
<li><a class="reference internal" href="#conflicts-between-options">15.5.3.9. Conflicts between options</a></li>
<li><a class="reference internal" href="#cleanup">15.5.3.10. Cleanup</a></li>
<li><a class="reference internal" href="#other-methods">15.5.3.11. Other methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#option-callbacks">15.5.4. Option Callbacks</a><ul>
<li><a class="reference internal" href="#defining-a-callback-option">15.5.4.1. Defining a callback option</a></li>
<li><a class="reference internal" href="#how-callbacks-are-called">15.5.4.2. How callbacks are called</a></li>
<li><a class="reference internal" href="#raising-errors-in-a-callback">15.5.4.3. Raising errors in a callback</a></li>
<li><a class="reference internal" href="#callback-example-1-trivial-callback">15.5.4.4. Callback example 1: trivial callback</a></li>
<li><a class="reference internal" href="#callback-example-2-check-option-order">15.5.4.5. Callback example 2: check option order</a></li>
<li><a class="reference internal" href="#callback-example-3-check-option-order-generalized">15.5.4.6. Callback example 3: check option order (generalized)</a></li>
<li><a class="reference internal" href="#callback-example-4-check-arbitrary-condition">15.5.4.7. Callback example 4: check arbitrary condition</a></li>
<li><a class="reference internal" href="#callback-example-5-fixed-arguments">15.5.4.8. Callback example 5: fixed arguments</a></li>
<li><a class="reference internal" href="#callback-example-6-variable-arguments">15.5.4.9. Callback example 6: variable arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#extending-optparse">15.5.5. Extending <tt class="docutils literal"><span class="pre">optparse</span></tt></a><ul>
<li><a class="reference internal" href="#adding-new-types">15.5.5.1. Adding new types</a></li>
<li><a class="reference internal" href="#adding-new-actions">15.5.5.2. Adding new actions</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="argparse.html"
                        title="previous chapter">15.4. <tt class="docutils literal"><span class="pre">argparse</span></tt> &#8212; Parser for command-line options, arguments and sub-commands</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="getopt.html"
                        title="next chapter">15.6. <tt class="docutils literal"><span class="pre">getopt</span></tt> &#8212; C-style 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/optparse.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="getopt.html" title="15.6. getopt — C-style parser for command line options"
             >next</a> |</li>
        <li class="right" >
          <a href="argparse.html" title="15.4. argparse — Parser for command-line options, arguments and sub-commands"
             >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> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" >15. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2020, 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 Oct 13, 2020.
    <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>

ZeroDay Forums Mini