\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Horde\_Argv}
\section{Advanced Usage}
This is reference documentation. If you haven't read the <a href="https://wiki.horde.org/Doc/Dev/HordeArgv">Basic Usage</a> tutorial document yet, do so now.

\subsection{Creating and populating the parser}
There are several ways to populate the parser with options. One way is to pass a list of \texttt{Horde\_Argv\_Option}s to the \texttt{Horde\_Argv\_Parser} constructor:

<pre><code class="language-php">
\$parser = new Horde\_Argv\_Parser(array('optionList' => array(
    new Horde\_Argv\_Option(
        '-f', '--filename',
        array('action' => 'store', 'type' => 'string', 'dest' => 'filename')),
    new Horde\_Argv\_Option(
        '-q', '--quiet',
        array('action' => 'store\_false', 'dest' => 'verbose'))
)));
</code></pre>
For long option lists, it's often more convenient/readable to create the list separately:

<pre><code class="language-php">
\$option\_list = array(
    new Horde\_Argv\_Option(
        '-f', '--filename',
        array('action' => 'store', 'type' => 'string', 'dest' => 'filename')),
    // ... 17 other options ...
    new Horde\_Argv\_Option(
        '-q', '--quiet',
        array('action' => 'store\_false', 'dest' => 'verbose'))
);
\$parser = new Horde\_Argv\_Parser(array('optionList' => \$option\_list));
</code></pre>
Or, you can use the \texttt{addOption()} method of \texttt{Horde\_Argv\_Parser} to add options one at a time:

<pre><code class="language-php">
\$parser = new Horde\_Argv\_Parser();
\$parser->addOption(
    '-f', '--filename',
    array('action' => 'store', 'type' => 'string', 'dest' => 'filename')
);
\$parser->addOption(
    '-q', '--quiet',
    array('action' => 'store\_false', 'dest' => 'verbose')
);
</code></pre>
This method makes it easier to track down exceptions raised by the \texttt{Horde\_Argv\_Option} constructor, which are common because of the complicated interdependencies among the various keyword arguments -- if you get it wrong, \textit{Horde\_Argv} throws an \texttt{InvalidArgumentException}.

\texttt{addOption()} can be called in one of two ways:

\begin{itemize}
\item pass it an \texttt{Horde\_Argv\_Option} instance


\item pass it any combination of positional and keyword arguments that are acceptable to \texttt{new Horde\_Argv\_Option()} (ie., to the \texttt{Horde\_Argv\_Option} constructor), and it will create the \texttt{Horde\_Argv\_Option} instance for you (shown above)


\end{itemize}
\subsection{Defining options}
Each \texttt{Horde\_Argv\_Option} instance represents a set of synonymous command-line options, ie. options that have the same meaning and effect, but different spellings. You can specify any number of short or long option strings, but you must specify at least one option string.

To define an option with only a short option string:

<pre><code class="language-php">
new Horde\_Argv\_Option('-f', ...)
</code></pre>
And to define an option with only a long option string:

<pre><code class="language-php">
new Horde\_Argv\_Option('--foo', ...)
</code></pre>
The ... represents a set of keyword arguments that define option attributes, i.e. attributes of the \texttt{Horde\_Argv\_Option} object. Just which keyword args you must supply for a given \texttt{Horde\_Argv\_Option} is fairly complicated (see the various \texttt{\_check*()} methods in the \texttt{Horde\_Argv\_Option} class if you don't believe me). If you get it wrong, \textit{Horde\_Argv} throws an \texttt{InvalidArgumentException} explaining your mistake.

The most important attribute of an option is its action, ie. what to do when we encounter this option on the command-line. The possible actions are:

\textbf{\texttt{store}}: store this option's argument <a href="https://wiki.horde.org/default">default</a><br />
\textbf{\texttt{store\_const}}: store a constant value<br />
\textbf{\texttt{store\_true}}: store a \texttt{TRUE} value<br />
\textbf{\texttt{store\_false}}: store a \texttt{FALSE} value<br />
\textbf{\texttt{append}}: append this option's argument to a list<br />
\textbf{\texttt{count}}: increment a counter by one<br />
\textbf{\texttt{callback}}: call a specified function<br />
\textbf{\texttt{help}}: print a usage message including all options and the documentation for them

(If you don't supply an action, the default is \texttt{store}. For this action, you may also supply \texttt{type} and \texttt{dest} option attributes; see below.)

As you can see, most actions involve storing or updating a value somewhere. \textit{Horde\_Argv} always creates an instance of \texttt{Horde\_Argv\_Values} (referred to as options) specifically for this purpose. Option arguments (and various other values) are stored as attributes of this object, according to the \texttt{dest} (destination) option attribute.

For example, when you call

<pre><code class="language-php">
\$parser->parseArgs();
</code></pre>
one of the first things \textit{Horde\_Argv} does is create the options object:

<pre><code class="language-php">
\$options = new Horde\_Argv\_Values();
</code></pre>
If one of the options in this parser is defined with

<pre><code class="language-php">
new Horde\_Argv\_Option('-f', '--file', array('action' => 'store', 'type' => 'string', 'dest' => 'filename'))
</code></pre>
and the command-line being parsed includes any of the following:

<pre><code>
-ffoo
-f foo
--file=foo
--file foo
</code></pre>
then \textit{Horde\_Argv}, on seeing the "-f" or "--file" option, will do the equivalent of this:

<pre><code class="language-php">
\$options->filename = 'foo';
</code></pre>
Clearly, the \texttt{type} and \texttt{dest} arguments are almost as important as \texttt{action}. \texttt{action} is the only attribute that is meaningful for all options, though, so it is the most important.

\subsection{Option actions}
The various option actions all have slightly different requirements and effects. Most actions have several relevant option attributes which you may specify to guide \textit{Horde\_Argv}'s behaviour; a few have required attributes, which you must specify for any option using that action.

\begin{itemize}
\item \texttt{store} [relevant: \texttt{type}, \texttt{dest}, \texttt{nargs}, \texttt{choices}]


\end{itemize}
\begin{quote}
The option must be followed by an argument, which is converted to a value according to \texttt{type} and stored in \texttt{dest}. If \texttt{nargs} > 1, multiple arguments will be consumed from the command line; all will be converted according to \texttt{type} and stored to \texttt{dest} as an array. See the "Option types" section below.<br />
If \texttt{choices} is supplied (an array of strings), the \texttt{type} defaults to \texttt{choice}.<br />
If \texttt{type} is not supplied, it defaults to \texttt{string}.<br />
If \texttt{dest} is not supplied, \textit{Horde\_Argv} derives a destination from the first long option strings (e.g., "--foo-bar" implies "foo\_bar"). If there are no long option strings, \textit{Horde\_Argv} derives a destination from the first short option string (e.g., "-f" implies "f").<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser->addOption('-f');
\$parser->addOption('-p', array('type' => 'float', 'nargs' => 3, 'dest' => 'point'));
</code></pre>
\begin{quote}
Given the following command line:<br />
\texttt{-f foo.txt -p 1 -3.5 4 -fbar.txt}<br />
\textit{Horde\_Argv} will set


\end{quote}
<pre><code class="language-php">
\$options->f = 'foo.txt';
\$options->point = array(1.0, -3.5, 4.0);
\$options->f = 'bar.txt';
</code></pre>
\begin{itemize}
\item \texttt{store\_const} [required: \texttt{const}; relevant: \texttt{dest}]


\end{itemize}
\begin{quote}
The value \texttt{const} is stored in \texttt{dest}.<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser->addOption('-q', '--quiet', array('action' => 'store\_const', 'const' => 0, 'dest' => 'verbose'));
\$parser->addOption('-v', '--verbose', array('action' => 'store\_const', 'const' => 1, 'dest' => 'verbose'));
\$parser->addOption('--noisy', array('action' => 'store\_const', 'const' => 2, 'dest' => 'verbose'));
</code></pre>
\begin{quote}
If "--noisy" is seen, \textit{Horde\_Argv} will set


\end{quote}
<pre><code class="language-php">
\$options->verbose = 2;
</code></pre>
\begin{itemize}
\item \texttt{store\_true} [relevant: \texttt{dest}]


\end{itemize}
\begin{quote}
A special case of \texttt{store\_const} that stores a \texttt{TRUE} value to \texttt{dest}.


\end{quote}
\begin{itemize}
\item \texttt{store\_false} [relevant: \texttt{dest}]


\end{itemize}
\begin{quote}
Like \texttt{store\_true}, but stores a \texttt{FALSE} value.<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser->addOption(null, '--clobber', array('action' => 'store\_true', 'dest' => 'clobber'));
\$parser->addOption(null, '--no-clobber', array('action' => 'store\_false', 'dest' => 'clobber'));
</code></pre>
\begin{itemize}
\item \texttt{append} [relevant: \texttt{type}, \texttt{dest}, \texttt{nargs}, \texttt{choices}]


\end{itemize}
\begin{quote}
The option must be followed by an argument, which is appended to the array in \texttt{dest}. If no default value for \texttt{dest} is supplied, an empty array is automatically created when \textit{Horde\_Argv} first encounters this option on the command-line. If \texttt{nargs} > 1, multiple arguments are consumed, and an array of length \texttt{nargs} is appended to \texttt{dest}.<br />
The defaults for \texttt{type} and \texttt{dest} are the same as for the \texttt{store} action.<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser->addOption('-t', '--tracks', array('action' => 'append', 'type' => 'int'));
</code></pre>
\begin{quote}
If "-t3" is seen on the command-line, \textit{Horde\_Argv} does the equivalent of:


\end{quote}
<pre><code class="language-php">
\$options->tracks = array();
\$options->tracks[] = intval('3');
</code></pre>
\begin{quote}
If, a little later on, "--tracks=4" is seen, it does:


\end{quote}
<pre><code class="language-php">
\$options->tracks[] = intval('4');
</code></pre>
\begin{itemize}
\item \texttt{count} [relevant: \texttt{dest}]


\end{itemize}
\begin{quote}
Increment the integer stored at \texttt{dest}. \texttt{dest} is set to zero before being incremented the first time (unless you supply a default value).<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser->addOption('-v', array('action' => 'count', 'dest' => 'verbosity'));
</code></pre>
\begin{quote}
The first time "-v" is seen on the command line, \textit{Horde\_Argv} does the equivalent of:


\end{quote}
<pre><code class="language-php">
\$options->verbosity = 0;
\$options->verbosity += 1;
</code></pre>
\begin{quote}
Every subsequent occurrence of "-v" results in


\end{quote}
<pre><code class="language-php">
\$options->verbosity += 1;
</code></pre>
\begin{itemize}
\item \texttt{callback} [required: \texttt{callback}; relevant: \texttt{type}, \texttt{nargs}, \texttt{callback\_args}, \texttt{callback\_kwargs}]


\end{itemize}
\begin{quote}
Call the function specified by \texttt{callback}. The signature of this function should be


\end{quote}
<pre><code class="language-php">
func(Horde\_Argv\_Option \$option,
     string \$opt,
     mixed \$value,
     Horde\_Argv\_Parser \$parser,
     array \$args,
     array \$kwargs)
</code></pre>
\begin{quote}
See Option Callbacks for more detail.


\end{quote}
\begin{itemize}
\item \texttt{help} [relevant: none]


\end{itemize}
\begin{quote}
Prints a complete help message for all the options in the current option parser. The help message is constructed from the \texttt{usage} string passed to \texttt{Horde\_Argv\_Parser}'s constructor and the \texttt{help} string passed to every option.<br />
If no help string is supplied for an option, it will still be listed in the help message. To omit an option entirely, use the special value \texttt{Horde\_Argv\_Option::SUPPRESS\_HELP}.<br />
Example:


\end{quote}
<pre><code class="language-php">
\$parser = new Horde\_Argv\_Parser();
\$parser->addOption('-h', '--help',
                   array('action' => 'help'));
\$parser->addOption('-v',
                   array('action' => 'store\_true', 'dest' => 'verbose',
                         'help' => 'Be moderately verbose'));
\$parser->addOption('--file',
                   array('dest' => 'filename',
                         'help' => 'Input file to read data from'));
\$parser->addOption('--secret',
                   array('help' => Horde\_Argv\_Option::SUPPRESS\_HELP));
</code></pre>
\begin{quote}
If \textit{Horde\_Argv} sees either "-h" or "--help" on the command line, it will print something like the following help message to stdout (assuming \$\_SERVER['argv'][0] is "foo.php"):


\end{quote}
<pre><code>
usage: foo.py [options]

options:
  -h, --help        Show this help message and exit
  -v                Be moderately verbose
  --file=FILENAME   Input file to read data from
</code></pre>
\begin{quote}
After printing the help message, \textit{Horde\_Argv} terminates your process with \texttt{exit(0)}.


\end{quote}
\begin{itemize}
\item \texttt{version} [relevant: none]


\end{itemize}
\begin{quote}
Prints the version number supplied to the \texttt{Horde\_Argv\_Parser} to stdout and exits. The version number is actually formatted and printed by the \texttt{printVersion()} method of \texttt{Horde\_Argv\_Parser}. Generally only relevant if the version argument is supplied to the \texttt{Horde\_Argv\_Parser} constructor.


\end{quote}
\subsection{Option types}
\textit{Horde\_Argv} has six built-in option types: \texttt{string}, \texttt{int}, \texttt{long}, \texttt{choice}, \texttt{float} and \texttt{complex}. If you need to add new option types, see <a href="https://wiki.horde.org/Doc/Dev/HordeArgvExtend">Extending Horde\_Argv</a>.

Arguments to string options are not checked or converted in any way: the text on the command line is stored in the destination (or passed to the callback) as-is.

Integer arguments are passed to \texttt{intval()} to convert them to PHP integers. If \texttt{intval()} fails, so will \textit{Horde\_Argv}, although with a more useful error message. (Internally, \textit{Horde\_Argv} throws \texttt{Horde\_Argv\_OptionValueException} from \texttt{Horde\_Argv\_Option\#checkBuiltin()}; \texttt{Horde\_Argv\_Parser} catches this exception higher up and terminates your program with a useful error message.)

Likewise, float arguments are passed to \texttt{floatval()} for conversion, long arguments also to \texttt{intval()}, and complex arguments are not handled yet. Apart from that, they are handled identically to integer arguments.

\texttt{choice} options are a subtype of \texttt{string} options. The \texttt{choices} option attribute (an array of strings) defines the set of allowed option arguments. \texttt{Horde\_Argv\_Option\#checkChoice()} compares user-supplied option arguments against this master list and throws \texttt{Horde\_Argv\_OptionValueException} if an invalid string is given.

\subsection{Querying and manipulating your option parser}
Sometimes, it's useful to poke around your option parser and see what's there. \texttt{Horde\_Argv\_Parser} provides a couple of methods to help you out:

\textbf{\texttt{boolean hasOption(string \$opt\_str)}}: Given an option string such as "-q" or "--verbose", returns \texttt{true} if the \texttt{Horde\_Argv\_Parser} has an option with that option string.<br />
\textbf{\texttt{Horde\_Argv\_Option getOption(string \$opt\_str)}}: Returns the \texttt{Horde\_Argv\_Option} instance that implements the supplied option string, or \texttt{null} if no options implement it.<br />
\textbf{\texttt{removeOption(string \$opt\_str)}}: If the \texttt{Horde\_Argv\_Parser} has an option corresponding to \texttt{\$opt\_str}, that option is removed. If that option provided any other option strings, all of those option strings become invalid. If \texttt{\$opt\_str} does not occur in any option belonging to this \texttt{Horde\_Argv\_Parser}, throws \texttt{InvalidArgumentException}.

\subsection{Conflicts between options}
If you're not careful, it's easy to define conflicting options:

<pre><code class="language-php">
\$parser->addOption('-n', '--dry-run', ...);
[...]
\$parser->addOption('-n', '--noisy', ...);
</code></pre>
(This is even easier to do if you've defined your own \texttt{Horde\_Argv\_Parser} subclass with some standard options.)

Every time you add an option, \textit{Horde\_Argv} checks for conflicts with existing options. If it finds any, it invokes the current conflict-handling mechanism. You can set the conflict-handling mechanism either in the constructor:

<pre><code class="language-php">
\$parser = new Horde\_Argv\_Parser(..., array('conflictHandler' => '...'));
</code></pre>
or with a separate call:

<pre><code class="language-php">
\$parser->setConflictHandler('...');
</code></pre>
The available conflict-handling mechanisms are:

\textbf{\texttt{error} (default)}: assume option conflicts are a programming error and throws \texttt{Horde\_Argv\_OptionConflictException}<br />
\textbf{\texttt{resolve}}: resolve option conflicts intelligently (see below)

Here's an example: first, define an \texttt{Horde\_Argv\_Parser} that resolves conflicts intelligently:

<pre><code class="language-php">
\$parser = new Horde\_Argv\_Parser(array('conflictHandler' => 'resolve'));
</code></pre>
Now add all of our options:

<pre><code class="language-php">
\$parser->addOption('-n', '--dry-run', ..., array('help' => 'original dry-run option'));
[...]
\$parser->addOption('-n', '--noisy', ..., array('help' => 'be noisy'));
</code></pre>
At this point, \textit{Horde\_Argv} detects that a previously-added option is already using the "-n" option string. Since \texttt{conflictHandler} is "resolve", it resolves the situation by removing "-n" from the earlier option's list of option strings. Now, "--dry-run" is the only way for the user to activate that option. If the user asks for help, the help message will reflect that, e.g.:

<pre><code>
options:
  --dry-run     original dry-run option
  [...]
  -n, --noisy   be noisy
</code></pre>
Note that it's possible to whittle away the option strings for a previously-added option until there are none left, and the user has no way of invoking that option from the command-line. In that case, \textit{Horde\_Argv} removes that option completely, so it doesn't show up in help text or anywhere else. E.g. if we carry on with our existing \texttt{Horde\_Argv\_Parser}:

<pre><code class="language-php">
\$parser->addOption('--dry-run', ..., array('help' => 'new dry-run option'));
</code></pre>
At this point, the first "-n/--dry-run" option is no longer accessible, so \textit{Horde\_Argv} removes it. If the user asks for help, they'll get something like this:

<pre><code>
options:
  [...]
  -n, --noisy   be noisy
  --dry-run     new dry-run option
</code></pre>
\end{document}
