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.7 2008/12/22 15:08:11 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.6
  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.      * @param string $delim Delimiter to use between short and long option
  173.      *
  174.      * @return string The string representation of the option
  175.      * @todo use __toString() instead
  176.      */
  177.     public function toString($delim ", ")
  178.     {
  179.         $ret     '';
  180.         $padding '';
  181.         if ($this->short_name != null{
  182.             $ret .= $this->short_name;
  183.             if ($this->expectsArgument()) {
  184.                 $ret .= ' ' $this->help_name;
  185.             }
  186.             $padding $delim;
  187.         }
  188.         if ($this->long_name != null{
  189.             $ret .= $padding $this->long_name;
  190.             if ($this->expectsArgument()) {
  191.                 $ret .= '=' $this->help_name;
  192.             }
  193.         }
  194.         return $ret;
  195.     }
  196.  
  197.     // }}}
  198.     // expectsArgument() {{{
  199.  
  200.     /**
  201.      * Returns true if the option requires one or more argument and false
  202.      * otherwise.
  203.      *
  204.      * @return bool Whether the option expects an argument or not
  205.      */
  206.     public function expectsArgument()
  207.     {
  208.         if ($this->action == 'StoreTrue' || $this->action == 'StoreFalse' ||
  209.             $this->action == 'Help' || $this->action == 'Version' ||
  210.             $this->action == 'Counter' || $this->action == 'List'{
  211.             return false;
  212.         }
  213.         return true;
  214.     }
  215.  
  216.     // }}}
  217.     // dispatchAction() {{{
  218.  
  219.     /**
  220.      * Formats the value $value according to the action of the option and
  221.      * updates the passed Console_CommandLine_Result object.
  222.      *
  223.      * @param mixed                      $value  The value to format
  224.      * @param Console_CommandLine_Result $result The result instance
  225.      * @param Console_CommandLine        $parser The parser instance
  226.      *
  227.      * @return void 
  228.      * @throws Console_CommandLine_Exception
  229.      */
  230.     public function dispatchAction($value$result$parser)
  231.     {
  232.         // check value is in option choices
  233.         if (!empty($this->choices&& !in_array($value$this->choices)) {
  234.             throw Console_CommandLine_Exception::factory(
  235.                 'OPTION_VALUE_NOT_VALID',
  236.                 array(
  237.                     'name'    => $this->name,
  238.                     'choices' => implode('", "'$this->choices),
  239.                     'value'   => $value,
  240.                 )$parser
  241.             );
  242.         }
  243.         $actionInfo Console_CommandLine::$actions[$this->action];
  244.         if (true === $actionInfo[1]{
  245.             // we have a "builtin" action
  246.             $tokens explode('_'$actionInfo[0]);
  247.             include_once implode('/'$tokens'.php';
  248.         }
  249.         $clsname $actionInfo[0];
  250.         $action  = new $clsname($result$this$parser);
  251.         $action->execute($value$this->action_params);
  252.     }
  253.  
  254.     // }}}
  255.     // validate() {{{
  256.  
  257.     /**
  258.      * Validates the option instance.
  259.      *
  260.      * @return void 
  261.      * @throws Console_CommandLine_Exception
  262.      * @todo use exceptions instead
  263.      */
  264.     public function validate()
  265.     {
  266.         // check if the option name is valid
  267.         if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/',
  268.             $this->name)) {
  269.             Console_CommandLine::triggerError('option_bad_name',
  270.                 E_USER_ERRORarray('{$name}' => $this->name));
  271.         }
  272.         // call the parent validate method
  273.         parent::validate();
  274.         // a short_name or a long_name must be provided
  275.         if ($this->short_name == null && $this->long_name == null{
  276.             Console_CommandLine::triggerError('option_long_and_short_name_missing',
  277.                 E_USER_ERRORarray('{$name}' => $this->name));
  278.         }
  279.         // check if the option short_name is valid
  280.         if ($this->short_name != null && 
  281.             !(preg_match('/^\-[a-zA-Z]{1}$/'$this->short_name))) {
  282.             Console_CommandLine::triggerError('option_bad_short_name',
  283.                 E_USER_ERRORarray(
  284.                     '{$name}' => $this->name
  285.                     '{$short_name}' => $this->short_name
  286.                 ));
  287.         }
  288.         // check if the option long_name is valid
  289.         if ($this->long_name != null && 
  290.             !preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/'$this->long_name)) {
  291.             Console_CommandLine::triggerError('option_bad_long_name',
  292.                 E_USER_ERRORarray(
  293.                     '{$name}' => $this->name
  294.                     '{$long_name}' => $this->long_name
  295.                 ));
  296.         }
  297.         // check if we have a valid action
  298.         if (!is_string($this->action)) {
  299.             Console_CommandLine::triggerError('option_bad_action',
  300.                 E_USER_ERRORarray('{$name}' => $this->name));
  301.         }
  302.         if (!isset(Console_CommandLine::$actions[$this->action])) {
  303.             Console_CommandLine::triggerError('option_unregistered_action',
  304.                 E_USER_ERRORarray(
  305.                     '{$action}' => $this->action,
  306.                     '{$name}' => $this->name
  307.                 ));
  308.         }
  309.         // if the action is a callback, check that we have a valid callback
  310.         if ($this->action == 'Callback' && !is_callable($this->callback)) {
  311.             Console_CommandLine::triggerError('option_invalid_callback',
  312.                 E_USER_ERRORarray('{$name}' => $this->name));
  313.         }
  314.     }
  315.  
  316.     // }}}
  317.     // setDefaults() {{{
  318.  
  319.     /**
  320.      * Set the default value according to the configured action.
  321.      *
  322.      * Note that for backward compatibility issues this method is only called
  323.      * when the 'force_options_defaults' is set to true, it will become the
  324.      * default behaviour in the next major release of Console_CommandLine.
  325.      *
  326.      * @return void 
  327.      */
  328.     public function setDefaults()
  329.     {
  330.         if ($this->default !== null{
  331.             // already set
  332.             return;
  333.         }
  334.         switch ($this->action{
  335.         case 'Counter':
  336.         case 'StoreInt':
  337.             $this->default = 0;
  338.             break;
  339.         case 'StoreFloat':
  340.             $this->default = 0.0;
  341.             break;
  342.         case 'StoreArray':
  343.             $this->default = array();
  344.             break;
  345.         case 'StoreTrue':
  346.             $this->default = false;
  347.             break;
  348.         case 'StoreFalse':
  349.             $this->default = true;
  350.             break;
  351.         default:
  352.             return;
  353.         }
  354.     }
  355.  
  356.     // }}}
  357. }

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