Your IP : 216.73.217.64


Current Path : /home3/gp97/php/CLITools/doc/build/html/
Upload File :
Current File : //home3/gp97/php/CLITools/doc/build/html/argsparser.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>ArgsParser A simple yet powerful command line argument parser &mdash; Maarch CLITools v0.3.1 documentation</title>
    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="_static/print.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.3.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/theme_extras.js"></script>
    <link rel="top" title="Maarch CLITools v0.3.1 documentation" href="index.html" />
    <link rel="next" title="Changelog" href="changelog.html" />
    <link rel="prev" title="Welcome to Maarch CLITools documentation!" href="index.html" /> 
  </head>
  <body>
      <div class="header"><h1 class="heading"><a href="index.html">
          <span>Maarch CLITools v0.3.1 documentation</span></a></h1>
        <h2 class="heading"><span>ArgsParser A simple yet powerful command line argument parser</span></h2>
      </div>
      <div class="topnav">
      
        <p>
        «&#160;&#160;<a href="index.html">Welcome to Maarch CLITools documentation!</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        &#160;&#160;::&#160;&#160;
        <a href="changelog.html">Changelog</a>&#160;&#160;»
        </p>

      </div>
      <div class="content">
        
        
  <div class="section" id="argsparser-a-simple-yet-powerful-command-line-argument-parser">
<h1><a class="reference internal" href="#ArgsParser" title="ArgsParser"><tt class="xref php php-class docutils literal"><span class="pre">ArgsParser</span></tt></a> A simple yet powerful command line argument parser<a class="headerlink" href="#argsparser-a-simple-yet-powerful-command-line-argument-parser" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">author:</th><td class="field-body">Bruno Carlin &lt;<a class="reference external" href="mailto:bruno&#46;carlin&#37;&#52;&#48;maarch&#46;org">bruno<span>&#46;</span>carlin<span>&#64;</span>maarch<span>&#46;</span>org</a>&gt;</td>
</tr>
</tbody>
</table>
<p><a class="reference internal" href="#ArgsParser" title="ArgsParser"><tt class="xref php php-class docutils literal"><span class="pre">ArgsParser</span></tt></a> makes it easy to define the command line syntax of
your program.</p>
<p>You can define arguments with short (ex: <tt class="docutils literal"><span class="pre">-v</span></tt>) and long
(ex: <tt class="docutils literal"><span class="pre">--verbose</span></tt>) identifiers.</p>
<p>It supports three types of arguments:</p>
<ul class="simple">
<li>Pure arguments, with short or long arguments. Supported syntaxes are:<ul>
<li><tt class="docutils literal"><span class="pre">--key</span> <span class="pre">value</span></tt> or <tt class="docutils literal"><span class="pre">-k</span> <span class="pre">value</span></tt></li>
<li><tt class="docutils literal"><span class="pre">--key=value</span></tt> or <tt class="docutils literal"><span class="pre">-k=value</span></tt></li>
</ul>
</li>
<li>Switches, which translate to boolean values. ex: <tt class="docutils literal"><span class="pre">--verbose</span></tt> or <tt class="docutils literal"><span class="pre">-v</span></tt>.</li>
<li>Positional arguments. Those can be named. All positional arguments
given that are not named will be stored in a special array.</li>
</ul>
<p>Additionally, subcommands are supported. Subcommands must be defined
as a new instance of ArgsParser that will be attached to the main one.
In consequence, subcommands can have their own sets of arguments.</p>
<dl class="class">
<dt id="ArgsParser">
<em class="property">class </em><tt class="descname">ArgsParser</tt><a class="headerlink" href="#ArgsParser" title="Permalink to this definition">¶</a></dt>
<dd><p>This class contains a dictionnary of every argument and generates a
hash containing all the options the users sets on the command line
merged in default values.</p>
<p>It has the following methods:</p>
<dl class="method">
<dt id="add_arg">
<em class="property">public method ArgsParser::</em><tt class="descname">add_arg</tt><big>(</big><em>$name</em>, <em>$params</em><big>)</big><a class="headerlink" href="#add_arg" title="Permalink to this definition">¶</a></dt>
<dd><p>Register an argument in the parser.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the argument</li>
<li><strong>$params</strong> &#8211; <p>The parameters of the argument. Can contain following
items:</p>
<ul>
<li><tt class="docutils literal"><span class="pre">short</span></tt>: the short form of the argument
(ex: &#8220;q&#8221; for &#8220;-q&#8221;) (default: null)</li>
<li><tt class="docutils literal"><span class="pre">long</span></tt>: the long form of the argument
(ex: &#8220;quiet&#8221; for &#8220;&#8211;quiet&#8221;)  (default: null)</li>
<li><tt class="docutils literal"><span class="pre">default</span></tt>: The default value for the argument if
not given by the user (default: null)</li>
<li><tt class="docutils literal"><span class="pre">mandatory</span></tt>: Wether the argument have a value
either given by the user or a default one)
(default: false)</li>
<li><tt class="docutils literal"><span class="pre">help</span></tt>: A short help phrase used to generate usage
(default: &#8220;&#8221;)</li>
</ul>
</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">null</p>
</td>
</tr>
</tbody>
</table>
<p class="versionchanged">
<span class="versionmodified">Changed in version 0.2.0: </span>Only add arguments. Other types have their own methods. Parameters are given as array</p>
<p>This is the only way to register an argument in the parser.
Example:</p>
<p>To add a optional argument that sets the working directory for the
script:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$argsparser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$argsparser</span><span class="o">-&gt;</span><span class="na">add_arg</span><span class="p">(</span><span class="s2">&quot;working directory&quot;</span><span class="p">,</span>
                     <span class="k">Array</span><span class="p">(</span>
                       <span class="s2">&quot;short&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span>
                       <span class="s2">&quot;long&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;working-directory&quot;</span><span class="p">,</span>
                       <span class="s2">&quot;default&quot;</span> <span class="o">=&gt;</span> <span class="nb">getcwd</span><span class="p">(),</span>
                       <span class="s2">&quot;help&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Sets working directory&quot;</span>
                     <span class="p">)</span>
                    <span class="p">);</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="add_switch">
<em class="property">public method ArgsParser::</em><tt class="descname">add_switch</tt><big>(</big><em>$name</em>, <em>$params</em><big>)</big><a class="headerlink" href="#add_switch" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a switch in the parser. Switches can only be optional</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the switch</li>
<li><strong>$params</strong> &#8211; <p>The parameters of the switch. Can contain following
items:</p>
<ul>
<li><tt class="docutils literal"><span class="pre">short</span></tt>: the short form of the argument
(ex: &#8220;q&#8221; for &#8220;-q&#8221;) (default: null)</li>
<li><tt class="docutils literal"><span class="pre">long</span></tt>: the long form of the argument
(ex: &#8220;quiet&#8221; for &#8220;&#8211;quiet&#8221;)  (default: null)</li>
<li><tt class="docutils literal"><span class="pre">default</span></tt>: The default value for the argument if
not given by the user (default: <tt class="docutils literal"><span class="pre">false</span></tt>)</li>
<li><tt class="docutils literal"><span class="pre">help</span></tt>: A short help phrase used to generate usage
(default: &#8220;&#8221;)</li>
</ul>
</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">null</p>
</td>
</tr>
</tbody>
</table>
<p class="versionadded">
<span class="versionmodified">New in version 0.2.0.</span></p>
<p>Example:</p>
<p>To add a optional argument that make output more verbose:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$argsparser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$argsparser</span><span class="o">-&gt;</span><span class="na">add_switch</span><span class="p">(</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span>
                        <span class="k">Array</span><span class="p">(</span>
                            <span class="s2">&quot;short&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;v&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;long&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;verbose&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;help&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Makes output more verbose&quot;</span>
                        <span class="p">)</span>
                       <span class="p">);</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="add_positional">
<em class="property">public method ArgsParser::</em><tt class="descname">add_positional</tt><big>(</big><em>$name</em>, <em>$params</em><big>)</big><a class="headerlink" href="#add_positional" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a positional argument in the parser.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the argument</li>
<li><strong>$params</strong> &#8211; <p>The parameters of the argument. Can contain following
items:</p>
<ul>
<li><tt class="docutils literal"><span class="pre">default</span></tt>: The default value for the argument if
not given by the user (default: null)</li>
<li><tt class="docutils literal"><span class="pre">mandatory</span></tt>: Wether the argument have a value
either given by the user or a default one)
(default: false)</li>
<li><tt class="docutils literal"><span class="pre">help</span></tt>: A short help phrase used to generate usage
(default: &#8220;&#8221;)</li>
</ul>
</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">null</p>
</td>
</tr>
</tbody>
</table>
<p class="versionadded">
<span class="versionmodified">New in version 0.2.0.</span></p>
<p>Example:</p>
<p>To add a mandatory positional argument that sets the working directory
for the script:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$argsparser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$argsparser</span><span class="o">-&gt;</span><span class="na">add_positional</span><span class="p">(</span><span class="s2">&quot;working directory&quot;</span><span class="p">,</span>
                            <span class="k">Array</span><span class="p">(</span>
                                <span class="s2">&quot;default&quot;</span> <span class="o">=&gt;</span> <span class="nb">getcwd</span><span class="p">(),</span>
                                <span class="s2">&quot;help&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Sets working directory&quot;</span>
                            <span class="p">)</span>
                           <span class="p">);</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="add_command">
<em class="property">public method ArgsParser::</em><tt class="descname">add_command</tt><big>(</big><em>$name</em>, <em>$aliases</em>, <em>&amp;$parser</em><span class="optional">[</span>, <em>$help=&quot;&quot;</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#add_command" title="Permalink to this definition">¶</a></dt>
<dd><p>Registers a subcommand to the parser</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; Main name of the command</li>
<li><strong>$aliases</strong> (<em>array</em>) &#8211; Aliases for the command</li>
<li><strong>$parser</strong> (<a class="reference internal" href="#ArgsParser" title="ArgsParser"><tt class="xref php php-class docutils literal"><span class="pre">ArgsParser</span></tt></a>) &#8211; An instance of ArgsParser that declares the specific arguments of the subcommand</li>
<li><strong>$help</strong> (<em>string</em>) &#8211; Description of the argument. Will be used to generate help</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">null</p>
</td>
</tr>
</tbody>
</table>
<p>This allows to define and use subcommands on the command line. For
example:</p>
<div class="highlight-bash"><div class="highlight"><pre>program.php <span class="nb">command</span> -o /path/to/output/file
</pre></div>
</div>
<p>Those commands can be invoked with both the command name and its aliases.
Let&#8217;s imagine a wrapper for the svn program. You can define the following:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$main_parser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$main_parser</span><span class="o">-&gt;</span> <span class="na">add_arg</span><span class="p">(</span> <span class="o">...</span> <span class="p">);</span>

<span class="nv">$checkout_parser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$checkout_parser</span><span class="o">-&gt;</span><span class="na">add_arg</span><span class="p">(</span> <span class="o">...</span> <span class="p">);</span>
<span class="nv">$main_parser</span><span class="o">-&gt;</span><span class="na">add_command</span><span class="p">(</span><span class="s2">&quot;checkout&quot;</span><span class="p">,</span> <span class="k">Array</span><span class="p">(</span><span class="s2">&quot;co&quot;</span><span class="p">,</span> <span class="s2">&quot;get&quot;</span><span class="p">),</span> <span class="nv">$checkout_parser</span><span class="p">,</span> <span class="s2">&quot;Checkouts the given SVN repository&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>You can then use this command the following ways:</p>
<div class="highlight-bash"><div class="highlight"><pre>svn.php checkout svn://url/to/repository
svn.php co svn://url/to/repository
svn.php get svn://url/to/repository
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="parse_args">
<em class="property">public method ArgsParser::</em><tt class="descname">parse_args</tt><big>(</big><em>$args</em><big>)</big><a class="headerlink" href="#parse_args" title="Permalink to this definition">¶</a></dt>
<dd><p>Gets the actual command line argument and parses them.</p>
<p>This is the core of the class.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$args</strong> (<em>Array</em>) &#8211; An array of arguments</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">Raises MissingArgumentError:</th></tr>
<tr><td>&nbsp;</td><td class="field-body"><p class="first last">When a mandatory argument is missing</p>
</td>
</tr>
</tbody>
</table>
<p class="versionchanged">
<span class="versionmodified">Changed in version 0.1.1: </span>Verifies that mandatory arguments have a value</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 0.1.3: </span>Throws MissingArgumentError instead of exception</p>
<p>It takes as input a list of token taken directly from command line. In
most cases, <tt class="docutils literal"><span class="pre">$GLOBALS[&quot;argv&quot;]</span></tt> is what you want.</p>
<div class="highlight-bash"><div class="highlight"><pre>program.php --arg1 <span class="s2">&quot;value of arg1&quot;</span> -k<span class="o">=</span>v path/to/file --switch
</pre></div>
</div>
<p>must be translated as</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">Array</span><span class="p">(</span><span class="s2">&quot;program.php&quot;</span><span class="p">,</span> <span class="s2">&quot;--arg1&quot;</span><span class="p">,</span> <span class="s2">&quot;value of arg1&quot;</span><span class="p">,</span> <span class="s2">&quot;-k=v&quot;</span><span class="p">,</span> <span class="s2">&quot;path/to/file&quot;</span><span class="p">,</span> <span class="s2">&quot;--switch&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>This list will be processed and an array containing the options will be return.
The returned array has the following structure:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">Array</span><span class="p">(</span>
  <span class="s2">&quot;executable&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;program.php&quot;</span><span class="p">,</span>
  <span class="s2">&quot;positional&quot;</span> <span class="o">=&gt;</span> <span class="k">Array</span><span class="p">(</span><span class="s2">&quot;list of&quot;</span><span class="p">,</span> <span class="s2">&quot;non-declared&quot;</span><span class="p">,</span> <span class="s2">&quot;positional arguments&quot;</span><span class="p">),</span>
  <span class="s2">&quot;name of arg1&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;value of arg1&quot;</span><span class="p">,</span>
  <span class="s2">&quot;name of k&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;v&quot;</span><span class="p">,</span>
  <span class="s2">&quot;non-given arg&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;default value&quot;</span><span class="p">,</span>
  <span class="s2">&quot;path as positional arg&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;path/to/file&quot;</span><span class="p">,</span>
  <span class="s2">&quot;switch&quot;</span> <span class="o">=&gt;</span> <span class="k">True</span>
<span class="p">);</span>
</pre></div>
</div>
<p>If a subcommand is given on the command line, a &#8220;subcommand&#8221; item
will be added in the output array. It will contain an array
defined as following:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">Array</span><span class="p">(</span>
  <span class="s2">&quot;opts&quot;</span> <span class="o">=&gt;</span> <span class="k">Array</span><span class="p">(</span> <span class="o">...</span> <span class="p">),</span> <span class="c1">// array returned by the sub-parser parse_args method.</span>
                          <span class="c1">// It contains the subcommand specific arguments</span>
  <span class="s2">&quot;name&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;name of the command&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="usage">
<em class="property">public method ArgsParser::</em><tt class="descname">usage</tt><big>(</big><big>)</big><a class="headerlink" href="#usage" title="Permalink to this definition">¶</a></dt>
<dd><p>Automatically generates usage for the program and exits</p>
<p>This method is automatically called in the following case :</p>
<ul class="simple">
<li>no value is given for an argument</li>
<li>the <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> switches are given on the command line</li>
<li>the &#8220;help&#8221; command is given.</li>
</ul>
<p>It can also be invoked directly :</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$argsparser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArgsParser</span><span class="p">();</span>
<span class="nv">$argsparser</span><span class="o">-&gt;</span><span class="na">usage</span><span class="p">();</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>


      </div>
      <div class="bottomnav">
      
        <p>
        «&#160;&#160;<a href="index.html">Welcome to Maarch CLITools documentation!</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        &#160;&#160;::&#160;&#160;
        <a href="changelog.html">Changelog</a>&#160;&#160;»
        </p>

      </div>

    <div class="footer">
        &copy; Copyright 2010, Bruno Carlin &lt;bruno.carlin@maarch.org&gt;.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.4.
    </div>
  </body>
</html>