Horde_Argv Extending Horde_Argv Since the two major controlling factors in how Horde_Argv interprets command-line options are the action and type of each option, the most likely direction of extension is to add new actions and new types. Adding new types To add new types, you need to define your own subclass of the Horde_Argv_Option class. This class has a couple of properties that define Horde_Argv's types: $TYPES and $TYPE_CHECKER. $TYPES is a tuple of type names; in your subclass, simply define a new tuple $TYPES that builds on the standard one. $TYPE_CHECKER is a dictionary mapping type names to type-checking functions. A type-checking function has the following signature:
foo check_foo(Horde_Argv_Option $option, string $opt, string $value)
You can name it whatever you like, and make it return any type you like (e.g. the hypothetical type foo). The value returned by a type-checking function will wind up in the Horde_Argv_Values instance returned by Horde_Argv_Parser->parseArgs(), or be passed to callbacks as the $value parameter.
Your type-checking function should throw Horde_Argv_OptionValueException if it encounters any problems. Horde_Argv_OptionValueException takes a single string argument, which is passed as-is to Horde_Argv_Parser's parserError() method, which in turn prepends the program name and the string "error:" and prints everything to stderr before terminating the process.
Here's a silly example that demonstrates adding an imaginary MyComplex option type to parse complex numbers on the command line.
You need to define your type-checker, since it's referred to in the $TYPE_CHECKER class attribute of your Horde_Argv_Option subclass:
class MyOption extends Horde_Argv_Option
{
public function __construct()
{
$this->TYPES[] = 'complex';
$this->TYPE_CHECKER['complex'] = 'checkComplex';
}
public function checkComplex($option, $opt, $value)
{
try {
return new MyComplex(value);
} catch (Exception $e) {
throw new Horde_Argv_OptionValueException(
sprintf('option %s: invalid complex value: %s', (opt, value))
);
}
}
}
That's it! Now you can write a script that uses the new option type just like any other Horde_Argv-based script, except you have to instruct your Horde_Argv_Parser to use MyOption instead of Horde_Argv_Option:
$parser = new Horde_Argv_Parser(array('optionClass' => 'MyOption'));
$parser->addOption('-c', array('type' => 'complex'));
Alternately, you can build your own option list and pass it to Horde_Argv_Parser; if you don't use addOption() in the above way, you don't need to tell Horde_Argv_Parser which option class to use:
$option_list = array(
new MyOption(
'-c',
array('action' => 'store', 'type' => 'complex', 'dest' => 'c')
)
);
parser = new Horde_Argv_Parser(array('optionList' => $option_list));
Adding new actions
Adding new actions is a bit trickier, because you have to understand that Horde_Argv has a couple of classifications for actions:
"store" actions: actions that result in Horde_Argv storing a value to a property of the current Horde_Argv_Values instance; these options require a dest attribute to be supplied to the Horde_Argv_Option constructor
--names=foo,bar --names blah --names ding,dong
would result in a list
array('foo', 'bar', 'blah', 'ding', 'dong')
Again we define a subclass of Horde_Argv_Option:
class MyOption extends Horde_Argv_Option
{
public function __construct()
{
$this->ACTIONS[] = 'extend';
$this->STORE_ACTIONS[] = 'extend';
$this->TYPED_ACTIONS[] = 'extend';
}
public function takeAction($action, $dest, $opt, $value, $values, $parser)
{
if ($action == 'extend') {
$lvalue = explode(',', $value);
$values->dest = array_merge($values->ensureValue('dest', array()),
$lvalue);
} else {
parent::takeAction($action, $dest, $opt, $value, $values, $parser);
}
}
}
Features of note:
- extend both expects a value on the command-line and stores that value somewhere, so it goes in both $STORE_ACTIONS and $TYPED_ACTIONS
- MyOption::takeAction() implements just this one new action, and passes control back to Horde_Argv_Option::takeAction() for the standard Horde_Argv actions
- $values is an instance of the Horde_Argv_Values class, which provides the very useful ensureValue() method. ensureValue() is essentially a getter with a safety valve; it is called as
$values->ensureValue($attr, $value)