Console_CommandLine
[ class tree: Console_CommandLine ] [ index: Console_CommandLine ] [ all elements ]

Source for file Option.php

Documentation is available at Option.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Console_CommandLine package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Console
  15.  * @package   Console_CommandLine
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2007 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id: Option.php,v 1.6 2008/10/09 10:44:54 izi Exp $
  20.  * @link      http://pear.php.net/package/Console_CommandLine
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Required by this class.
  27.  */
  28. require_once 'Console/CommandLine.php';
  29. require_once 'Console/CommandLine/Element.php';
  30.  
  31. /**
  32.  * Class that represent a commandline option.
  33.  *
  34.  * @category  Console
  35.  * @package   Console_CommandLine
  36.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  37.  * @copyright 2007 David JEAN LOUIS
  38.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  39.  * @version   Release: 1.0.5
  40.  * @link      http://pear.php.net/package/Console_CommandLine
  41.  * @since     Class available since release 0.1.0
  42.  */
  43. {
  44.     // Public properties {{{
  45.  
  46.     /**
  47.      * The option short name (ex: -v).
  48.      *
  49.      * @var string $short_name Short name of the option
  50.      */
  51.     public $short_name;
  52.  
  53.     /**
  54.      * The option long name (ex: --verbose).
  55.      *
  56.      * @var string $long_name Long name of the option
  57.      */
  58.     public $long_name;
  59.  
  60.     /**
  61.      * The option action, defaults to "StoreString".
  62.      *
  63.      * @var string $action Option action
  64.      */
  65.     public $action = 'StoreString';
  66.  
  67.     /**
  68.      * The default value of the option if not provided on the command line.
  69.      *
  70.      * @var mixed $default Default value of the option.
  71.      */
  72.     public $default;
  73.  
  74.     /**
  75.      * An array of possible values for the option if this array is not empty
  76.      * and the value passed is not in the array an exception is raised.
  77.      * This only make sense for actions that accept values of course.
  78.      *
  79.      * @var array $choices Valid choices for the option
  80.      */
  81.     public $choices = array();
  82.  
  83.     /**
  84.      * The callback function (or method) to call for an action of type
  85.      * Callback, this can be any callable supported by the php function
  86.      * call_user_func.
  87.      * 
  88.      * Example:
  89.      *
  90.      * <code>
  91.      * $parser->addOption('myoption', array(
  92.      *     'short_name' => '-m',
  93.      *     'long_name'  => '--myoption',
  94.      *     'action'     => 'Callback',
  95.      *     'callback'   => 'myCallbackFunction'
  96.      * ));
  97.      * </code>
  98.      *
  99.      * @var callable $callback The option callback
  100.      */
  101.     public $callback;
  102.  
  103.     /**
  104.      * An associative array of additional params to pass to the class
  105.      * corresponding to the action, this array will also be passed to the
  106.      * callback defined for an action of type Callback, Example:
  107.      *
  108.      * <code>
  109.      * // for a custom action
  110.      * $parser->addOption('myoption', array(
  111.      *     'short_name'    => '-m',
  112.      *     'long_name'     => '--myoption',
  113.      *     'action'        => 'MyCustomAction',
  114.      *     'action_params' => array('foo'=>true, 'bar'=>false)
  115.      * ));
  116.      *
  117.      * // if the user type:
  118.      * // $ <yourprogram> -m spam
  119.      * // in your MyCustomAction class the execute() method will be called
  120.      * // with the value 'spam' as first parameter and
  121.      * // array('foo'=>true, 'bar'=>false) as second parameter
  122.      * </code>
  123.      *
  124.      * @var array $action_params Additional parameters to pass to the action
  125.      */
  126.     public $action_params = array();
  127.  
  128.     /**
  129.      * For options that expect an argument, this property tells the parser if
  130.      * the option argument is optional and can be ommited.
  131.      *
  132.      * @var bool $argumentOptional Whether the option arg is optional or not
  133.      */
  134.     public $argument_optional = false;
  135.  
  136.     /**
  137.      * For options that uses the "choice" property only.
  138.      * Adds a --list-<choice> option to the parser that displays the list of
  139.      * choices for the option.
  140.      *
  141.      * @var bool $add_list_option Whether to add a list option or not
  142.      */
  143.     public $add_list_option = false;
  144.  
  145.     // }}}
  146.     // __construct() {{{
  147.  
  148.     /**
  149.      * Constructor.
  150.      *
  151.      * @param string $name   The name of the option
  152.      * @param array  $params An optional array of parameters
  153.      *
  154.      * @return void 
  155.      */
  156.     public function __construct($name = null$params = array()) 
  157.     {
  158.         parent::__construct($name$params);
  159.         if ($this->action == 'Password'{
  160.             // special case for Password action, password can be passed to the 
  161.             // commandline or prompted by the parser
  162.             $this->argument_optional = true;
  163.         }
  164.     }
  165.  
  166.     // }}}
  167.     // toString() {{{
  168.  
  169.     /**
  170.      * Returns the string representation of the option.
  171.      *
  172.      * @return string The string representation of the option
  173.      * @todo use __toString() instead
  174.      */
  175.     public function toString()
  176.     {
  177.         $ret     '';
  178.         $padding '';
  179.         if ($this->short_name != null{
  180.             $ret .= $this->short_name;
  181.             if ($this->expectsArgument()) {
  182.                 $ret .= ' ' $this->help_name;
  183.             }
  184.             $padding ', ';
  185.         }
  186.         if ($this->long_name != null{
  187.             $ret .= $padding $this->long_name;
  188.             if ($this->expectsArgument()) {
  189.                 $ret .= '=' $this->help_name;
  190.             }
  191.         }
  192.         return $ret;
  193.     }
  194.  
  195.     // }}}
  196.     // expectsArgument() {{{
  197.  
  198.     /**
  199.      * Returns true if the option requires one or more argument and false
  200.      * otherwise.
  201.      *
  202.      * @return bool Whether the option expects an argument or not
  203.      */
  204.     public function expectsArgument()
  205.     {
  206.         if ($this->action == 'StoreTrue' || $this->action == 'StoreFalse' ||
  207.             $this->action == 'Help' || $this->action == 'Version' ||
  208.             $this->action == 'Counter' || $this->action == 'List'{
  209.             return false;
  210.         }
  211.         return true;
  212.     }
  213.  
  214.     // }}}
  215.     // dispatchAction() {{{
  216.  
  217.     /**
  218.      * Formats the value $value according to the action of the option and
  219.      * updates the passed Console_CommandLine_Result object.
  220.      *
  221.      * @param mixed                      $value  The value to format
  222.      * @param Console_CommandLine_Result $result The result instance
  223.      * @param Console_CommandLine        $parser The parser instance
  224.      *
  225.      * @return void 
  226.      * @throws Console_CommandLine_Exception
  227.      */
  228.     public function dispatchAction($value$result$parser)
  229.     {
  230.         // check value is in option choices
  231.         if (!empty($this->choices&& !in_array($value$this->choices)) {
  232.             throw Console_CommandLine_Exception::factory(
  233.                 'OPTION_VALUE_NOT_VALID',
  234.                 array(
  235.                     'name'    => $this->name,
  236.                     'choices' => implode('", "'$this->choices),
  237.                     'value'   => $value,
  238.                 )$parser
  239.             );
  240.         }
  241.         $actionInfo Console_CommandLine::$actions[$this->action];
  242.         if (true === $actionInfo[1]{
  243.             // we have a "builtin" action
  244.             $tokens explode('_'$actionInfo[0]);
  245.             include_once implode('/'$tokens'.php';
  246.         }
  247.         $clsname $actionInfo[0];
  248.         $action  = new $clsname($result$this$parser);
  249.         $action->execute($value$this->action_params);
  250.     }
  251.  
  252.     // }}}
  253.     // validate() {{{
  254.  
  255.     /**
  256.      * Validates the option instance.
  257.      *
  258.      * @return void 
  259.      * @throws Console_CommandLine_Exception
  260.      * @todo use exceptions instead
  261.      */
  262.     public function validate()
  263.     {
  264.         // check if the option name is valid
  265.         if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/',
  266.             $this->name)) {
  267.             Console_CommandLine::triggerError('option_bad_name',
  268.                 E_USER_ERRORarray('{$name}' => $this->name));
  269.         }
  270.         // call the parent validate method
  271.         parent::validate();
  272.         // a short_name or a long_name must be provided
  273.         if ($this->short_name == null && $this->long_name == null{
  274.             Console_CommandLine::triggerError('option_long_and_short_name_missing',
  275.                 E_USER_ERRORarray('{$name}' => $this->name));
  276.         }
  277.         // check if the option short_name is valid
  278.         if ($this->short_name != null && 
  279.             !(preg_match('/^\-[a-zA-Z]{1}$/'$this->short_name))) {
  280.             Console_CommandLine::triggerError('option_bad_short_name',
  281.                 E_USER_ERRORarray(
  282.                     '{$name}' => $this->name
  283.                     '{$short_name}' => $this->short_name
  284.                 ));
  285.         }
  286.         // check if the option long_name is valid
  287.         if ($this->long_name != null && 
  288.             !preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/'$this->long_name)) {
  289.             Console_CommandLine::triggerError('option_bad_long_name',
  290.                 E_USER_ERRORarray(
  291.                     '{$name}' => $this->name
  292.                     '{$long_name}' => $this->long_name
  293.                 ));
  294.         }
  295.         // check if we have a valid action
  296.         if (!is_string($this->action)) {
  297.             Console_CommandLine::triggerError('option_bad_action',
  298.                 E_USER_ERRORarray('{$name}' => $this->name));
  299.         }
  300.         if (!isset(Console_CommandLine::$actions[$this->action])) {
  301.             Console_CommandLine::triggerError('option_unregistered_action',
  302.                 E_USER_ERRORarray(
  303.                     '{$action}' => $this->action,
  304.                     '{$name}' => $this->name
  305.                 ));
  306.         }
  307.         // if the action is a callback, check that we have a valid callback
  308.         if ($this->action == 'Callback' && !is_callable($this->callback)) {
  309.             Console_CommandLine::triggerError('option_invalid_callback',
  310.                 E_USER_ERRORarray('{$name}' => $this->name));
  311.         }
  312.     }
  313.  
  314.     // }}}
  315. }

Documentation generated on Mon, 11 Mar 2019 15:26:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.