diff options
Diffstat (limited to 'doc/index.html')
-rw-r--r-- | doc/index.html | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/doc/index.html b/doc/index.html new file mode 100644 index 0000000..26c599b --- /dev/null +++ b/doc/index.html @@ -0,0 +1,172 @@ +<!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 — argparse v0.9.0 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: '0.9.0', + 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 v0.9.0 documentation" href="" /> + <link rel="next" title="argparse vs. optparse" href="argparse-vs-optparse.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="argparse-vs-optparse.html" title="argparse vs. optparse" + accesskey="N">next</a> |</li> + <li><a href="">argparse v0.9.0 documentation</a> »</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="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="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="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> +</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">'__main__'</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">'Sum the integers on the command line.'</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">'integers'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">'int'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'+'</span><span class="p">,</span> + <span class="n">help</span><span class="o">=</span><span class="s">'one of the integers to be summed'</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">'--log'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">FileType</span><span class="p">(</span><span class="s">'w'</span><span class="p">),</span> <span class="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">'the file where the sum should be written '</span> + <span class="s">'(default: write the sum to stdout)'</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">'</span><span class="si">%s</span><span class="se">\n</span><span class="s">'</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="argparse-vs-optparse.html" + title="next chapter">argparse vs. optparse</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="argparse-vs-optparse.html" title="argparse vs. optparse" + >next</a> |</li> + <li><a href="">argparse v0.9.0 documentation</a> »</li> + </ul> + </div> + <div class="footer"> + © Copyright 2006-2009, Steven Bethard. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1. + </div> + </body> +</html>
\ No newline at end of file |