Your IP : 216.73.217.64
<!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 — 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>
«  <a href="index.html">Welcome to Maarch CLITools documentation!</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="changelog.html">Changelog</a>  »
</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 <<a class="reference external" href="mailto:bruno.carlin%40maarch.org">bruno<span>.</span>carlin<span>@</span>maarch<span>.</span>org</a>></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>) – The name of the argument</li>
<li><strong>$params</strong> – <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: “q” for “-q”) (default: null)</li>
<li><tt class="docutils literal"><span class="pre">long</span></tt>: the long form of the argument
(ex: “quiet” for “–quiet”) (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: “”)</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"><?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">-></span><span class="na">add_arg</span><span class="p">(</span><span class="s2">"working directory"</span><span class="p">,</span>
<span class="k">Array</span><span class="p">(</span>
<span class="s2">"short"</span> <span class="o">=></span> <span class="s2">"w"</span><span class="p">,</span>
<span class="s2">"long"</span> <span class="o">=></span> <span class="s2">"working-directory"</span><span class="p">,</span>
<span class="s2">"default"</span> <span class="o">=></span> <span class="nb">getcwd</span><span class="p">(),</span>
<span class="s2">"help"</span> <span class="o">=></span> <span class="s2">"Sets working directory"</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>) – The name of the switch</li>
<li><strong>$params</strong> – <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: “q” for “-q”) (default: null)</li>
<li><tt class="docutils literal"><span class="pre">long</span></tt>: the long form of the argument
(ex: “quiet” for “–quiet”) (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: “”)</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"><?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">-></span><span class="na">add_switch</span><span class="p">(</span><span class="s2">"verbose"</span><span class="p">,</span>
<span class="k">Array</span><span class="p">(</span>
<span class="s2">"short"</span> <span class="o">=></span> <span class="s2">"v"</span><span class="p">,</span>
<span class="s2">"long"</span> <span class="o">=></span> <span class="s2">"verbose"</span><span class="p">,</span>
<span class="s2">"help"</span> <span class="o">=></span> <span class="s2">"Makes output more verbose"</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>) – The name of the argument</li>
<li><strong>$params</strong> – <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: “”)</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"><?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">-></span><span class="na">add_positional</span><span class="p">(</span><span class="s2">"working directory"</span><span class="p">,</span>
<span class="k">Array</span><span class="p">(</span>
<span class="s2">"default"</span> <span class="o">=></span> <span class="nb">getcwd</span><span class="p">(),</span>
<span class="s2">"help"</span> <span class="o">=></span> <span class="s2">"Sets working directory"</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>&$parser</em><span class="optional">[</span>, <em>$help=""</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>) – Main name of the command</li>
<li><strong>$aliases</strong> (<em>array</em>) – 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>) – An instance of ArgsParser that declares the specific arguments of the subcommand</li>
<li><strong>$help</strong> (<em>string</em>) – 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’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"><?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">-></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">-></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">-></span><span class="na">add_command</span><span class="p">(</span><span class="s2">"checkout"</span><span class="p">,</span> <span class="k">Array</span><span class="p">(</span><span class="s2">"co"</span><span class="p">,</span> <span class="s2">"get"</span><span class="p">),</span> <span class="nv">$checkout_parser</span><span class="p">,</span> <span class="s2">"Checkouts the given SVN repository"</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>) – An array of arguments</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">Raises MissingArgumentError:</th></tr>
<tr><td> </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["argv"]</span></tt> is what you want.</p>
<div class="highlight-bash"><div class="highlight"><pre>program.php --arg1 <span class="s2">"value of arg1"</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"><?php</span>
<span class="k">Array</span><span class="p">(</span><span class="s2">"program.php"</span><span class="p">,</span> <span class="s2">"--arg1"</span><span class="p">,</span> <span class="s2">"value of arg1"</span><span class="p">,</span> <span class="s2">"-k=v"</span><span class="p">,</span> <span class="s2">"path/to/file"</span><span class="p">,</span> <span class="s2">"--switch"</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"><?php</span>
<span class="k">Array</span><span class="p">(</span>
<span class="s2">"executable"</span> <span class="o">=></span> <span class="s2">"program.php"</span><span class="p">,</span>
<span class="s2">"positional"</span> <span class="o">=></span> <span class="k">Array</span><span class="p">(</span><span class="s2">"list of"</span><span class="p">,</span> <span class="s2">"non-declared"</span><span class="p">,</span> <span class="s2">"positional arguments"</span><span class="p">),</span>
<span class="s2">"name of arg1"</span> <span class="o">=></span> <span class="s2">"value of arg1"</span><span class="p">,</span>
<span class="s2">"name of k"</span> <span class="o">=></span> <span class="s2">"v"</span><span class="p">,</span>
<span class="s2">"non-given arg"</span> <span class="o">=></span> <span class="s2">"default value"</span><span class="p">,</span>
<span class="s2">"path as positional arg"</span> <span class="o">=></span> <span class="s2">"path/to/file"</span><span class="p">,</span>
<span class="s2">"switch"</span> <span class="o">=></span> <span class="k">True</span>
<span class="p">);</span>
</pre></div>
</div>
<p>If a subcommand is given on the command line, a “subcommand” 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"><?php</span>
<span class="k">Array</span><span class="p">(</span>
<span class="s2">"opts"</span> <span class="o">=></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">"name"</span> <span class="o">=></span> <span class="s2">"name of the command"</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 “help” command is given.</li>
</ul>
<p>It can also be invoked directly :</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?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">-></span><span class="na">usage</span><span class="p">();</span>
</pre></div>
</div>
</dd></dl>
</dd></dl>
</div>
</div>
<div class="bottomnav">
<p>
«  <a href="index.html">Welcome to Maarch CLITools documentation!</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="changelog.html">Changelog</a>  »
</p>
</div>
<div class="footer">
© Copyright 2010, Bruno Carlin <bruno.carlin@maarch.org>.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.4.
</div>
</body>
</html>