[ Index ]

PHP Cross Reference of phool

title

Body

[close]

/Phool/Options/ -> Base.php (source)

   1  <?php
   2  //============================================================================
   3  // This program is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU Lesser General Public License (LGPL) as
   5  // published by the Free Software Foundation, either version 3 of the License,
   6  // or (at your option) any later version.
   7  //
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU Lesser General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU Lesser General Public License
  14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15  //============================================================================
  16  /**
  17  * @copyright Francois Laupretre <phool@tekwire.net>
  18  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  19  * @category phool
  20  * @package phool
  21  */
  22  //============================================================================
  23  
  24  namespace Phool\Options;
  25  
  26  //=============================================================================
  27  /**
  28  * This class manages command line options. It is a wrapper above Getopt
  29  */
  30  
  31  abstract class Base
  32  {
  33  // These properties must be declared in the child class (see 'Dummy' class)
  34  
  35  // protected $opt_modifiers; /* Modifier args */
  36  // protected $options;    /* Option values */
  37  
  38  abstract protected function processOption($opt,$arg);
  39  
  40  //-----------------------
  41  
  42  public function options()
  43  {
  44  return $this->options;
  45  }
  46  
  47  //-----------------------
  48  
  49  public function option($opt)
  50  {
  51  if (!array_key_exists($opt,$this->options))
  52      throw new \Exception("$opt: Unknown option");
  53  return $this->options[$opt];
  54  }
  55  
  56  //-----------------------
  57  
  58  public function set($opt,$value)
  59  {
  60  if (!array_key_exists($opt,$this->options))
  61      throw new \Exception("$opt: Unknown option");
  62  $this->options=$value;
  63  }
  64  
  65  //-----------------------
  66  
  67  public function parse(&$args)
  68  {
  69  $short_opts='';
  70  $long_opts=array();
  71  foreach($this->opt_modifiers as $mod)
  72      {
  73      $short_opts .= $mod['short'];
  74      $long=$mod['long'];
  75      if ($mod['value'])
  76          {
  77          $short_opts .= ':';
  78          $long .= '=';
  79          }
  80      $long_opts[]=$long;
  81      }
  82  
  83  list($opts,$args2)=Getopt::getopt2($args,$short_opts,$long_opts);
  84  foreach($opts as $opt_val)
  85      {
  86      list($opt,$arg)=$opt_val;
  87      if (strlen($opt)>1) // Convert long option to short
  88          {
  89          $opt=substr($opt,2);
  90          foreach($this->opt_modifiers as $mod)
  91              {
  92              if ($mod['long']==$opt)
  93                  {
  94                  $opt=$mod['short'];
  95                  break;
  96                  }
  97              }
  98          }
  99      if (strlen($opt)>1) throw new \Exception("--$opt: Unrecognized option");
 100      $this->processOption($opt,$arg);
 101      }
 102  
 103  $args=$args2;
 104  }
 105  
 106  //-----------------------
 107  
 108  public function parseAll(&$args)
 109  {
 110  $res=array();
 111  
 112  while(true)
 113      {
 114      $this->parse($args);
 115      if (!count($args)) break;
 116      $res[]=array_shift($args);
 117      }
 118  $args=$res;
 119  }
 120  
 121  //---------
 122  
 123  //============================================================================
 124  } // End of class
 125  ?>


Generated: Thu Jun 4 19:17:11 2015 Cross-referenced by PHPXref 0.7.1