\documentclass{article}
\usepackage{ulem}
\pagestyle{headings}
\begin{document}
\part{Horde\_Cli\_Modular}
Command line interfaces can often be composed of different modules responsible for distinct actions. \texttt{Horde\_Cli\_Modular} allows each such module to influence the overall command line interface.

\section{Introduction}
The command line of the tool \textbf{\texttt{pear}} is a good example of the kind of interface \texttt{Horde\_Cli\_Modular} supports: There are a number of global options available but each command supported by \textbf{\texttt{pear}} may have its own subset of options. \textbf{\texttt{-c}} identifies the \textbf{\texttt{pear}} configuration file as a global option. In contrast the \textbf{\texttt{--register-only}} option is specific to the \textbf{\texttt{install}} command provided by \textbf{\texttt{pear}}.

Obviously not all command line interfaces match this scheme. Hence \texttt{Horde\_Cli\_Modular} shouldn't be considered to be a generic utility for all CLI tools. But for all CLI helpers that have such a structure the library should provide a decent skeleton that allows to quickly start adding modules.

\section{Structure}
\texttt{Horde\_Cli\_Modular} provides four classes as main structural elements:

\begin{description}
\item[\texttt{Horde\_Cli\_Modular}] {The entry point for generating the command line interface.}
\item[\texttt{Horde\_Cli\_Modular\_Modules}] {The handler for the list of modules.}
\item[\texttt{Horde\_Cli\_Modular\_ModuleProvide}] {A module factory.}
\item[\texttt{Horde\_Cli\_Modular\_Module}] {The interface definition for a module.}
\end{description}

\section{Horde\_Cli\_Modular}
The \texttt{Horde\_Cli\_Modular} class is the entry point for constructing a modular command line interface. The class provides methods to combine and access the different modules.

\subsection{Setup}
This class generates the basic setup and you will only have to provide a few basic parameters to setup the system. This introduction will focus on the central settings and you will need to lookup the API documentation for further details.

The following demonstrates how you could construct the \texttt{Horde\_Cli\_Modular} instance:

\begin{verbatim}

$modular = new Horde_Cli_Modular(
  array(
    'parser' => array('usage' => '[options] MODULE'),
    'modules' => array('directory' => dirname(__FILE__) . '/Module'),
    'provider' => array('prefix' => 'Horde_Something_Module_')
  )
);
\end{verbatim}

The \textbf{\texttt{parser}} parameter preparse the command line argument parser (usually \texttt{Horde\_Argv}). The snippet above only provides the \textbf{\texttt{usage}} setting that will be displayed as the condensed usage instruction when the user requests the help for the CLI script.

The \textbf{\texttt{modules}} part defines the \textbf{\texttt{directory}} that contains the various modules which are part of the command line interface.

And finally the \textbf{\texttt{provider}} setting indicates the common \textbf{\texttt{prefix}} that the module classes will have.

The file structure for the hypothetical \texttt{Horde\_Something} package would look like this in order to match the setup presented above:

\begin{verbatim}

lib/
  Horde/
    Something.php
    Something/
      Module/
        One.php
        Two.php        
\end{verbatim}

There are two modules: \texttt{Horde\_Something\_Module\_One} in \texttt{One.php} and \texttt{Horde\_Something\_Module\_Two} in \texttt{Two.php}.

\subsection{Usage}
After creating the \texttt{Horde\_Cli\_Modular} instance you should create the command line parser and read the arguments the user provided.

\begin{verbatim}

$parser = $modular->createParser();
list($options, $arguments) = $parser->parseArgs();
\end{verbatim}

This is the main action you will need.

Beyond that \texttt{Horde\_Cli\_Modular} will allow you to retrieve the list of modules with:

\begin{verbatim}

$modules = $modular->getModules();
\end{verbatim}

And you can retrieve a module instance using:

\begin{verbatim}

$module = $modular->getProvider()->getModule('one');
\end{verbatim}

\section{Writing modules}
First and foremost each module can add its own option group with a specific title, description and a set of module specific options (see Horde\_Argv\_OptionGroup for exact details). In addition \texttt{Horde\_Cli\_Modular} allows a module to modify the global usage string and add basic options valid for all modules.

For that to work all modules are required to implement the \texttt{Horde\_Cli\_Modular\_Module} interface:

\begin{verbatim}

interface Horde_Cli_Modular_Module
{
    public function getUsage();
    public function getBaseOptions();
    public function hasOptionGroup();
    public function getOptionGroupTitle();
    public function getOptionGroupDescription();
    public function getOptionGroupOptions();
}
\end{verbatim}

There are two functions that allow to influence the global setup of the command line parser: \texttt{getUsage()} and \texttt{getBaseOptions()}. The first one returns a string that will be added to the generic usage description displayed when the user requests the help screen. The second one returns an array of \texttt{Horde\_Argv\_Option} instances that each define an option valid for all modules.

If the module defines its own option group (that will be displayed as a separate section in the help text for the command) \texttt{hasOptionGroup()} needs to return \textbf{\texttt{true}}.

An option group needs a title (returned as a string by \texttt{getOptionGroupTitle()}) and a descriptions (returned as a string by \texttt{getOptionGroupDescription()}). And finally an option group needs a list of options valid for the specific module defining the option group. The corresponding list is returned as an array of \texttt{Horde\_Argv\_Option} instances by the method \texttt{getOptionGroupOptions()}.

\section{Examples}
Currently the package \texttt{Horde\_Kolab\_Cli} and the \texttt{components} application both use \texttt{Horde\_Cli\_Modular}. You are referred to these package in order to look at real world examples using the \texttt{Horde\_Cli\_Modular} library.

\end{document}
