Files
argparse/doc/defaults.html
2015-08-08 23:34:50 +03:00

300 lines
13 KiB
HTML

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Default values &mdash; argparse 0.4.1 tutorial</title>
<link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="top" title="argparse 0.4.1 tutorial" href="index.html"/>
<link rel="next" title="Callbacks" href="callbacks.html"/>
<link rel="prev" title="Adding and configuring commands" href="commands.html"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="index.html" class="fa fa-home"> argparse</a>
<div role="search">
<form id ="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="parsers.html">Creating and using parsers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="parsers.html#parsing-command-line-arguments">Parsing command line arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="parsers.html#configuring-parsers">Configuring parsers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="arguments.html">Adding and configuring arguments</a><ul>
<li class="toctree-l2"><a class="reference internal" href="arguments.html#setting-number-of-consumed-arguments">Setting number of consumed arguments</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="options.html">Adding and configuring options</a><ul>
<li class="toctree-l2"><a class="reference internal" href="options.html#flags">Flags</a></li>
<li class="toctree-l2"><a class="reference internal" href="options.html#control-characters">Control characters</a></li>
<li class="toctree-l2"><a class="reference internal" href="options.html#setting-number-of-consumed-arguments">Setting number of consumed arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="options.html#setting-number-of-invocations">Setting number of invocations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mutexes.html">Mutually exclusive groups</a></li>
<li class="toctree-l1"><a class="reference internal" href="commands.html">Adding and configuring commands</a><ul>
<li class="toctree-l2"><a class="reference internal" href="commands.html#adding-elements-to-commands">Adding elements to commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="commands.html#making-a-command-optional">Making a command optional</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Default values</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#default-mode">Default mode</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="callbacks.html">Callbacks</a><ul>
<li class="toctree-l2"><a class="reference internal" href="callbacks.html#converters">Converters</a></li>
<li class="toctree-l2"><a class="reference internal" href="callbacks.html#actions">Actions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="misc.html">Miscellaneous</a><ul>
<li class="toctree-l2"><a class="reference internal" href="misc.html#generating-and-overwriting-help-and-usage-messages">Generating and overwriting help and usage messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#overwriting-default-help-option">Overwriting default help option</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#setting-argument-placeholder">Setting argument placeholder</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#disabling-option-handling">Disabling option handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#prohibiting-overuse-of-options">Prohibiting overuse of options</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#parsing-algorithm">Parsing algorithm</a></li>
<li class="toctree-l2"><a class="reference internal" href="misc.html#property-lists">Property lists</a></li>
</ul>
</li>
</ul>
</div>
&nbsp;
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">argparse</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li>Default values</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/defaults.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main">
<div class="section" id="default-values">
<h1>Default values<a class="headerlink" href="#default-values" title="Permalink to this headline"></a></h1>
<p>For elements such as arguments and options, if <tt class="docutils literal"><span class="pre">default</span></tt> property is set, its value is stored in case the element was not used.</p>
<div class="highlight-lua"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="n">parser</span><span class="p">:</span><span class="n">option</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">-o --output&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">Output file.&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span><span class="p">)</span>
<span class="c1">-- Equivalent:</span>
<span class="n">parser</span><span class="p">:</span><span class="n">option</span> <span class="s2">&quot;</span><span class="s">-o&quot;</span> <span class="s2">&quot;</span><span class="s">--output&quot;</span>
<span class="p">:</span><span class="n">description</span> <span class="s2">&quot;</span><span class="s">Output file.&quot;</span>
<span class="p">:</span><span class="n">default</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span>
</pre></div>
</td></tr></table></div>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua
</pre></div>
</div>
<div class="highlight-lua"><div class="highlight"><pre><span class="p">{</span>
<span class="n">output</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The existence of a default value is reflected in help message, unless <tt class="docutils literal"><span class="pre">show_default</span></tt> property is set to <tt class="docutils literal"><span class="pre">false</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua --help
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Usage: script.lua [-o &lt;output&gt;] [-h]
Options:
-o &lt;output&gt;, --output &lt;output&gt;
Output file. (default: a.out)
-h, --help Show this help message and exit.
</pre></div>
</div>
<p>Note that invocation without required arguments is still an error.</p>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua -o
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Usage: script.lua [-o &lt;output&gt;] [-h]
Error: too few arguments
</pre></div>
</div>
<div class="section" id="default-mode">
<h2>Default mode<a class="headerlink" href="#default-mode" title="Permalink to this headline"></a></h2>
<p><tt class="docutils literal"><span class="pre">defmode</span></tt> property regulates how argparse should use the default value of an element.</p>
<p>If <tt class="docutils literal"><span class="pre">defmode</span></tt> contains <tt class="docutils literal"><span class="pre">u</span></tt> (for unused), the default value will be automatically passed to the element if it was not invoked at all. This is the default behavior.</p>
<p>If <tt class="docutils literal"><span class="pre">defmode</span></tt> contains <tt class="docutils literal"><span class="pre">a</span></tt> (for argument), the default value will be automatically passed to the element if not enough arguments were passed, or not enough invocations were made.</p>
<p>Consider the difference:</p>
<div class="highlight-lua"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="n">parser</span><span class="p">:</span><span class="n">option</span> <span class="s2">&quot;</span><span class="s">-o&quot;</span>
<span class="p">:</span><span class="n">default</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span>
<span class="n">parser</span><span class="p">:</span><span class="n">option</span> <span class="s2">&quot;</span><span class="s">-p&quot;</span>
<span class="p">:</span><span class="n">default</span> <span class="s2">&quot;</span><span class="s">password&quot;</span>
<span class="p">:</span><span class="n">defmode</span> <span class="s2">&quot;</span><span class="s">arg&quot;</span>
</pre></div>
</td></tr></table></div>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua -h
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Usage: script.lua [-o &lt;o&gt;] [-p [&lt;p&gt;]] [-h]
Options:
-o &lt;o&gt; default: a.out
-p [&lt;p&gt;] default: password
-h, --help Show this help message and exit.
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua
</pre></div>
</div>
<div class="highlight-lua"><div class="highlight"><pre><span class="p">{</span>
<span class="n">o</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua -p
</pre></div>
</div>
<div class="highlight-lua"><div class="highlight"><pre><span class="p">{</span>
<span class="n">o</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">a.out&quot;</span><span class="p">,</span>
<span class="n">p</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">password&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>$ lua script.lua -o
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Usage: script.lua [-o &lt;o&gt;] [-p [&lt;p&gt;]] [-h]
Error: too few arguments
</pre></div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="callbacks.html" class="btn btn-neutral float-right" title="Callbacks"/>Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="commands.html" class="btn btn-neutral" title="Adding and configuring commands"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2013 - 2015, Peter Melnichenko.
</p>
</div>
<a href="https://github.com/snide/sphinx_rtd_theme">Sphinx theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.4.1',
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/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>