Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > 35f15dfef6a5aceaac7285fbf44291b7 > files > 22

python-argparse-1.1-1.fc13.noarch.rpm

<!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>Documentation &mdash; argparse v1.1 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:     '1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="argparse v1.1 documentation" href="" />
    <link rel="next" title="Introduction to argparse" href="overview.html" /> 
  </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="overview.html" title="Introduction to argparse"
             accesskey="N">next</a> |</li>
        <li><a href="">argparse v1.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="documentation">
<h1>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline">¶</a></h1>
<ul>
<li class="toctree-l1"><a class="reference external" href="overview.html">Introduction to argparse</a><ul>
<li class="toctree-l2"><a class="reference external" href="overview.html#adding-arguments">Adding arguments</a></li>
<li class="toctree-l2"><a class="reference external" href="overview.html#parsing-arguments">Parsing arguments</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="argparse-vs-optparse.html">argparse vs. optparse</a><ul>
<li class="toctree-l2"><a class="reference external" href="argparse-vs-optparse.html#advantages-of-argparse">Advantages of argparse</a></li>
<li class="toctree-l2"><a class="reference external" href="argparse-vs-optparse.html#upgrading-optparse-code">Upgrading optparse code</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="api-docs.html">API documentation</a><ul>
<li class="toctree-l2"><a class="reference external" href="ArgumentParser.html">ArgumentParser objects</a></li>
<li class="toctree-l2"><a class="reference external" href="add_argument.html">The add_argument() method</a></li>
<li class="toctree-l2"><a class="reference external" href="parse_args.html">The parse_args() method</a></li>
<li class="toctree-l2"><a class="reference external" href="other-methods.html">Other methods</a></li>
<li class="toctree-l2"><a class="reference external" href="other-utilities.html">Other utilities</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="example-usage">
<h1>Example usage<a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h1>
<p>The following simple example uses the argparse module to generate the command-line interface for a Python program that sums its command-line arguments and writes them to a log file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="c"># create the parser</span>
    <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span>
        <span class="n">description</span><span class="o">=</span><span class="s">&#39;Sum the integers on the command line.&#39;</span><span class="p">)</span>

    <span class="c"># add the arguments</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s">&#39;integers&#39;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">&#39;int&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">&#39;+&#39;</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s">&#39;one of the integers to be summed&#39;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s">&#39;--log&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">&#39;w&#39;</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s">&#39;the file where the sum should be written &#39;</span>
             <span class="s">&#39;(default: write the sum to stdout)&#39;</span><span class="p">)</span>

    <span class="c"># parse the command line</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="c"># write out the sum</span>
    <span class="n">args</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="nb">sum</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">integers</span><span class="p">))</span>
    <span class="n">args</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Assuming the Python code above is saved into a file called <tt class="docutils literal"><span class="pre">scriptname.py</span></tt>, it can be run at the command line and provides useful help messages:</p>
<div class="highlight-python"><pre>$ scriptname.py -h
usage: scriptname.py [-h] [--log LOG] int [int ...]

Sum the integers on the command line.

positional arguments:
  int         one of the integers to be summed

optional arguments:
  -h, --help  show this help message and exit
  --log LOG   the file where the sum should be written (default: write the sum
              to stdout)</pre>
</div>
<p>When run with the appropriate arguments, it writes the sum of the command-line integers to the specified log file:</p>
<div class="highlight-python"><pre>$ scriptname.py --log=log.txt 1 1 2 3 5 8
$ more log.txt
20</pre>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Documentation</a><ul>
</ul>
</li>
<li><a class="reference external" href="#example-usage">Example usage</a></li>
</ul>

            <h4>Next topic</h4>
            <p class="topless"><a href="overview.html"
                                  title="next chapter">Introduction to argparse</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/index.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" size="18" />
                <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="overview.html" title="Introduction to argparse"
             >next</a> |</li>
        <li><a href="">argparse v1.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2006-2009, Steven Bethard.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>