[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cli/src/ -> lmbCliInput.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class lmbCliInput.

  12   *

  13   * @package cli

  14   * @version $Id: lmbCliInput.class.php 5945 2007-06-06 08:31:43Z pachanga $

  15   */
  16  class lmbCliInput
  17  {
  18    protected $minimum_args = null;
  19    protected $options = array();
  20    protected $arguments = array();
  21    protected $throw_exception = false;
  22    protected $strict_mode = true;
  23    protected $argv;
  24  
  25    function __construct()
  26    {
  27      $args = func_get_args();
  28      $this->_addOptions($args);
  29    }
  30  
  31    function strictMode($flag = true)
  32    {
  33      $this->strict_mode = $flag;
  34    }
  35  
  36    function setMinimumArguments($minimum_args)
  37    {
  38      $this->minimum_args = $minimum_args;
  39    }
  40  
  41    function read($argv = null, $is_posix = true)
  42    {
  43      try
  44      {
  45        if(is_null($argv))
  46          $this->argv = self :: readPHPArgv();
  47        else
  48          $this->argv = $argv;
  49  
  50        if($is_posix)
  51          array_shift($this->argv);
  52  
  53        $this->_parse($this->argv);
  54        $this->_validate();
  55      }
  56      catch(lmbCliException $e)
  57      {
  58        if($this->throw_exception)
  59          throw $e;
  60  
  61        return false;
  62      }
  63      return true;
  64    }
  65  
  66    function getArgv()
  67    {
  68      return $this->argv;
  69    }
  70  
  71    function throwException($flag = true)
  72    {
  73      $this->throw_exception = $flag;
  74    }
  75  
  76    //idea taken from PEAR::Getopt

  77    function readPHPArgv()
  78    {
  79      global $argv;
  80      if(is_array($argv))
  81        return $argv;
  82  
  83      if(@is_array($_SERVER['argv']))
  84        return $_SERVER['argv'];
  85  
  86      if(@is_array($GLOBALS['HTTP_SERVER_VARS']['argv']))
  87        return $GLOBALS['HTTP_SERVER_VARS']['argv'];
  88  
  89      throw new lmbCliException('Could not read cmd args (register_argc_argv=Off?)');
  90    }
  91  
  92    function getOption($name)
  93    {
  94      foreach($this->options as $option)
  95      {
  96        if($option->match($name))
  97          return $option;
  98      }
  99    }
 100  
 101    function isOptionPresent($name)
 102    {
 103      if($option = $this->getOption($name))
 104        return $option->isPresent();
 105  
 106      return false;
 107    }
 108  
 109    function getOptionValue($name, $default = null)
 110    {
 111      if($option = $this->getOption($name))
 112        return $option->getValue();
 113  
 114      return $default;
 115    }
 116  
 117    function getArgument($index, $default = null)
 118    {
 119      return isset($this->arguments[$index]) ? $this->arguments[$index] : $default;
 120    }
 121  
 122    function addOption($option)
 123    {
 124      $this->options[] = $option;
 125    }
 126  
 127    function getOptions()
 128    {
 129      return $this->options;
 130    }
 131  
 132    function getArguments()
 133    {
 134      return $this->arguments;
 135    }
 136  
 137    protected function _validate()
 138    {
 139      if(!is_null($this->minimum_args) && $this->minimum_args > sizeof($this->arguments))
 140        throw new lmbCliException("Minimum {$this->minimum_args} required");
 141  
 142      foreach($this->options as $option)
 143        $option->validate();
 144    }
 145  
 146    protected function _addOptions($args)
 147    {
 148      foreach($args as $arg)
 149      {
 150        if(is_string($arg))
 151          $this->options += $this->_objectify($arg);
 152        elseif(is_object($arg))
 153          $this->options[] = $arg;
 154      }
 155    }
 156  
 157    protected function _objectify($str)
 158    {
 159      $opts = array();
 160      foreach(explode(';', $str) as $item)
 161      {
 162        if(!$item)
 163          continue;
 164  
 165        if(preg_match('~^(?:((\w)\|(\w+))|(\w\b)|(\w+)?)(=)?~', $item, $m))
 166        {
 167          $req = isset($m[6]) ? lmbCliOption :: VALUE_REQ : lmbCliOption :: VALUE_NO;
 168  
 169          if($m[1])
 170            $opt = new lmbCliOption($m[2], $m[3], $req);
 171          elseif($m[4])
 172            $opt = new lmbCliOption($m[4], $req);
 173          elseif($m[5])
 174            $opt = new lmbCliOption($m[5], $req);
 175  
 176          $opts[] = $opt;
 177        }
 178      }
 179      return $opts;
 180    }
 181  
 182    protected function _parse($argv)
 183    {
 184      $this->_reset();
 185  
 186      $postponed_option = null;
 187  
 188      for($i=0;$i<sizeof($argv);$i++)
 189      {
 190        $arg = $argv[$i];
 191  
 192        if($this->_extractLongOption($arg, $name, $value))
 193        {
 194          $postponed_option = $this->_addLongOption($name);
 195  
 196          if(isset($value))
 197          {
 198            $postponed_option->setValue($value);
 199            unset($postponed_option);
 200          }
 201        }
 202        elseif($this->_extractShortOption($arg, $name, $value))
 203        {
 204          $postponed_option = $this->_addShortOption($name, $value);
 205  
 206          if(isset($value))
 207          {
 208            if(!$postponed_option->isValueForbidden())
 209              $postponed_option->setValue($value);
 210            elseif($this->_maybeArgumentNext($argv, $i))
 211            {
 212              $this->arguments[] = $value;
 213              $i++;
 214            }
 215  
 216            unset($postponed_option);
 217          }
 218          elseif($postponed_option->isValueForbidden())
 219            unset($postponed_option);
 220        }
 221        elseif(isset($postponed_option) && $this->strict_mode)
 222        {
 223          $postponed_option->setValue($arg);
 224          unset($postponed_option);
 225        }
 226        else
 227        {
 228          $this->arguments[] = $arg;
 229        }
 230      }
 231    }
 232  
 233    protected function _extractLongOption($arg, &$option, &$value = null)
 234    {
 235      if(!preg_match('~^--([a-z][a-z0-9]+)(=(.*))?$~', $arg, $m))
 236        return false;
 237  
 238      $option = $m[1];
 239      $value = isset($m[3]) ? $m[3] : null;
 240      return true;
 241    }
 242  
 243    protected function _extractShortOption($arg, &$option, &$value = null)
 244    {
 245      if(!preg_match('~^-([a-z][^=\s]*)((=|\s+)(.*))?$~', $arg, $m))
 246        return false;
 247  
 248      $option = $m[1];
 249      $value = isset($m[4]) ? $m[4] : null;
 250      return true;
 251    }
 252  
 253    protected function _maybeArgumentNext($argv, $i)
 254    {
 255      return (isset($argv[$i+1]) &&
 256              strpos($argv[$i+1], '-') === false);
 257    }
 258  
 259    protected function _reset()
 260    {
 261      $this->arguments = array();
 262      foreach($this->options as $option)
 263        $option->reset();
 264    }
 265  
 266    protected function _addLongOption($name)
 267    {
 268      $option = $this->_approveOption($name);
 269      return $option;
 270    }
 271  
 272    protected function _addShortOption($name)
 273    {
 274      list($glued, $last) = $this->_getGluedOptions($name);
 275      foreach($glued as $name)
 276        $this->_approveOption($name);
 277  
 278      $option = $this->_approveOption($last);
 279      return $option;
 280    }
 281  
 282    protected function _approveOption($name)
 283    {
 284      if(!$option = $this->getOption($name))
 285      {
 286        if(!$this->strict_mode)
 287        {
 288          $option = new lmbCliOption($name);
 289          $this->addOption($option);
 290        }
 291        else
 292          throw new lmbCliException("Option '{$name}' is illegal");
 293      }
 294  
 295      $option->touch();
 296      return $option;
 297    }
 298  
 299    protected function _getGluedOptions($glue)
 300    {
 301      $glued = array();
 302      for($j=0;$j<strlen($glue)-1;$j++)
 303        $glued[] = $glue{$j};
 304  
 305      $last = substr($glue, -1, 1);
 306  
 307      return array($glued, $last);
 308    }
 309  }
 310  
 311  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7