[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cli/src/ -> lmbCliOption.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  lmb_require('limb/cli/src/lmbCliException.class.php');
  10  
  11  /**

  12   * class lmbCliOption.

  13   *

  14   * @package cli

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

  16   */
  17  class lmbCliOption
  18  {
  19    const VALUE_OPT = 0;
  20    const VALUE_REQ = 1;
  21    const VALUE_NO  = 2;
  22  
  23    protected $short_name;
  24    protected $long_name;
  25    protected $value_mode;
  26    protected $value;
  27    protected $is_present = false;
  28  
  29    function __construct($short_or_long_name, $long_name = null, $value_mode = self :: VALUE_NO)
  30    {
  31      if(!is_string($long_name) && strlen($short_or_long_name) > 1)
  32      {
  33        $this->long_name = $short_or_long_name;
  34        $this->value_mode = !is_null($long_name) ? $long_name : self :: VALUE_NO;
  35      }
  36      elseif(!is_string($long_name) && strlen($short_or_long_name) == 1)
  37      {
  38        $this->short_name = $short_or_long_name;
  39        $this->value_mode = !is_null($long_name) ? $long_name : self :: VALUE_NO;
  40      }
  41      else
  42      {
  43        if(strlen($short_or_long_name) > strlen($long_name))
  44          throw new lmbCliException("Option short name '$short_or_long_name' longer than long name '$long_name'");
  45  
  46        $this->short_name = $short_or_long_name;
  47        $this->long_name = $long_name;
  48        $this->value_mode = $value_mode;
  49      }
  50    }
  51  
  52    function reset()
  53    {
  54      $this->value = null;
  55      $this->is_present = false;
  56    }
  57  
  58    function match($name)
  59    {
  60      return ($name == $this->short_name || $name == $this->long_name);
  61    }
  62  
  63    function getShortName()
  64    {
  65      return $this->short_name;
  66    }
  67  
  68    function getLongName()
  69    {
  70      return $this->long_name;
  71    }
  72  
  73    function getValueMode()
  74    {
  75      return $this->value_mode;
  76    }
  77  
  78    function isValueRequired()
  79    {
  80      return $this->value_mode === self :: VALUE_REQ;
  81    }
  82  
  83    function isValueOptional()
  84    {
  85      return $this->value_mode === self :: VALUE_OPT;
  86    }
  87  
  88    function isValueForbidden()
  89    {
  90      return $this->value_mode === self :: VALUE_NO;
  91    }
  92  
  93    function touch()
  94    {
  95      $this->is_present = true;
  96    }
  97  
  98    function isPresent()
  99    {
 100      return $this->is_present;
 101    }
 102  
 103    function setValue($value)
 104    {
 105      $this->touch();
 106      $this->value = $value;
 107    }
 108  
 109    function getValue()
 110    {
 111      return $this->value;
 112    }
 113  
 114    function validate()
 115    {
 116      if(empty($this->value) && $this->isValueRequired())
 117        throw new lmbCliException("Option '" . $this->toString() . "' is required to have a value");
 118  
 119      if(!empty($this->value) && $this->isValueForbidden())
 120        throw new lmbCliException("Option '" . $this->toString() . "' is forbidden to have a value");
 121    }
 122  
 123    function toString()
 124    {
 125      $res = array();
 126      if($this->short_name)
 127        $res[] = '-' . $this->short_name;
 128      if($this->long_name)
 129        $res[] = '--' . $this->long_name;
 130  
 131      return implode('|', $res);
 132    }
 133  }
 134  
 135  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7