Source for file Option.php
Documentation is available at Option.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* This file is part of the PEAR Console_CommandLine package.
* LICENSE: This source file is subject to the MIT license that is available
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
* @package Console_CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007 David JEAN LOUIS
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
* Required by this class.
require_once 'Console/CommandLine.php';
require_once 'Console/CommandLine/Element.php';
* Class that represent a commandline option.
* @package Console_CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007 David JEAN LOUIS
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: 1.2.0
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
* The option short name (ex: -v).
* @var string $short_name Short name of the option
* The option long name (ex: --verbose).
* @var string $long_name Long name of the option
* The option action, defaults to "StoreString".
* @var string $action Option action
* An array of possible values for the option. If this array is not empty
* and the value passed is not in the array an exception is raised.
* This only make sense for actions that accept values of course.
* @var array $choices Valid choices for the option
* The callback function (or method) to call for an action of type
* Callback, this can be any callable supported by the php function
* $parser->addOption('myoption', array(
* 'long_name' => '--myoption',
* 'action' => 'Callback',
* 'callback' => 'myCallbackFunction'
* @var callable $callback The option callback
* An associative array of additional params to pass to the class
* corresponding to the action, this array will also be passed to the
* callback defined for an action of type Callback, Example:
* $parser->addOption('myoption', array(
* 'long_name' => '--myoption',
* 'action' => 'MyCustomAction',
* 'action_params' => array('foo'=>true, 'bar'=>false)
* // $ <yourprogram> -m spam
* // in your MyCustomAction class the execute() method will be called
* // with the value 'spam' as first parameter and
* // array('foo'=>true, 'bar'=>false) as second parameter
* @var array $action_params Additional parameters to pass to the action
* For options that expect an argument, this property tells the parser if
* the option argument is optional and can be ommited.
* @var bool $argumentOptional Whether the option arg is optional or not
* For options that uses the "choice" property only.
* Adds a --list-<choice> option to the parser that displays the list of
* choices for the option.
* @var bool $add_list_option Whether to add a list option or not
// Private properties {{{
* When an action is called remember it to allow for multiple calls.
* @var object $action_instance Placeholder for action
private $_action_instance = null;
* @param string $name The name of the option
* @param array $params An optional array of parameters
public function __construct($name = null , $params = array ())
if ($this->action == 'Password') {
// special case for Password action, password can be passed to the
// commandline or prompted by the parser
* Returns the string representation of the option.
* @param string $delim Delimiter to use between short and long option
* @return string The string representation of the option
* @todo use __toString() instead
* Returns true if the option requires one or more argument and false
* @return bool Whether the option expects an argument or not
if ($this->action == 'StoreTrue' || $this->action == 'StoreFalse' ||
* Formats the value $value according to the action of the option and
* updates the passed Console_CommandLine_Result object.
* @param mixed $value The value to format
* @param Console_CommandLine_Result $result The result instance
* @param Console_CommandLine $parser The parser instance
* @throws Console_CommandLine_Exception
if (true === $actionInfo[1 ]) {
// we have a "builtin" action
$tokens = explode('_', $actionInfo[0 ]);
include_once implode('/', $tokens) . '.php';
$clsname = $actionInfo[0 ];
if ($this->_action_instance === null ) {
$this->_action_instance = new $clsname($result, $this, $parser);
// check value is in option choices
'OPTION_VALUE_NOT_VALID',
* Validates the option instance.
* @throws Console_CommandLine_Exception
* @todo use exceptions instead
// check if the option name is valid
if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/',
E_USER_ERROR , array ('{$name}' => $this->name));
// call the parent validate method
// a short_name or a long_name must be provided
E_USER_ERROR , array ('{$name}' => $this->name));
// check if the option short_name is valid
'{$name}' => $this->name,
// check if the option long_name is valid
'{$name}' => $this->name,
// check if we have a valid action
E_USER_ERROR , array ('{$name}' => $this->name));
Console_CommandLine ::triggerError('option_unregistered_action',
// if the action is a callback, check that we have a valid callback
E_USER_ERROR , array ('{$name}' => $this->name));
* Set the default value according to the configured action.
* Note that for backward compatibility issues this method is only called
* when the 'force_options_defaults' is set to true, it will become the
* default behaviour in the next major release of Console_CommandLine.
Documentation generated on Mon, 11 Mar 2019 15:51:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|