gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

Getopt.php (4795B)


      1 <?php
      2 /*
      3  * This file is part of PHPUnit.
      4  *
      5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
      6  *
      7  * For the full copyright and license information, please view the LICENSE
      8  * file that was distributed with this source code.
      9  */
     10 
     11 /**
     12  * Command-line options parsing class.
     13  *
     14  * @since Class available since Release 3.0.0
     15  */
     16 class PHPUnit_Util_Getopt
     17 {
     18     public static function getopt(array $args, $short_options, $long_options = null)
     19     {
     20         if (empty($args)) {
     21             return array(array(), array());
     22         }
     23 
     24         $opts     = array();
     25         $non_opts = array();
     26 
     27         if ($long_options) {
     28             sort($long_options);
     29         }
     30 
     31         if (isset($args[0][0]) && $args[0][0] != '-') {
     32             array_shift($args);
     33         }
     34 
     35         reset($args);
     36         array_map('trim', $args);
     37 
     38         while (list($i, $arg) = each($args)) {
     39             if ($arg == '') {
     40                 continue;
     41             }
     42 
     43             if ($arg == '--') {
     44                 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
     45                 break;
     46             }
     47 
     48             if ($arg[0] != '-' ||
     49                 (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
     50                 $non_opts[] = $args[$i];
     51                 continue;
     52             } elseif (strlen($arg) > 1 && $arg[1] == '-') {
     53                 self::parseLongOption(
     54                     substr($arg, 2),
     55                     $long_options,
     56                     $opts,
     57                     $args
     58                 );
     59             } else {
     60                 self::parseShortOption(
     61                     substr($arg, 1),
     62                     $short_options,
     63                     $opts,
     64                     $args
     65                 );
     66             }
     67         }
     68 
     69         return array($opts, $non_opts);
     70     }
     71 
     72     protected static function parseShortOption($arg, $short_options, &$opts, &$args)
     73     {
     74         $argLen = strlen($arg);
     75 
     76         for ($i = 0; $i < $argLen; $i++) {
     77             $opt     = $arg[$i];
     78             $opt_arg = null;
     79 
     80             if (($spec = strstr($short_options, $opt)) === false ||
     81                 $arg[$i] == ':') {
     82                 throw new PHPUnit_Framework_Exception(
     83                     "unrecognized option -- $opt"
     84                 );
     85             }
     86 
     87             if (strlen($spec) > 1 && $spec[1] == ':') {
     88                 if (strlen($spec) > 2 && $spec[2] == ':') {
     89                     if ($i + 1 < $argLen) {
     90                         $opts[] = array($opt, substr($arg, $i + 1));
     91                         break;
     92                     }
     93                 } else {
     94                     if ($i + 1 < $argLen) {
     95                         $opts[] = array($opt, substr($arg, $i + 1));
     96                         break;
     97                     } elseif (list(, $opt_arg) = each($args)) {
     98                     } else {
     99                         throw new PHPUnit_Framework_Exception(
    100                             "option requires an argument -- $opt"
    101                         );
    102                     }
    103                 }
    104             }
    105 
    106             $opts[] = array($opt, $opt_arg);
    107         }
    108     }
    109 
    110     protected static function parseLongOption($arg, $long_options, &$opts, &$args)
    111     {
    112         $count   = count($long_options);
    113         $list    = explode('=', $arg);
    114         $opt     = $list[0];
    115         $opt_arg = null;
    116 
    117         if (count($list) > 1) {
    118             $opt_arg = $list[1];
    119         }
    120 
    121         $opt_len = strlen($opt);
    122 
    123         for ($i = 0; $i < $count; $i++) {
    124             $long_opt  = $long_options[$i];
    125             $opt_start = substr($long_opt, 0, $opt_len);
    126 
    127             if ($opt_start != $opt) {
    128                 continue;
    129             }
    130 
    131             $opt_rest = substr($long_opt, $opt_len);
    132 
    133             if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
    134                 $opt == substr($long_options[$i+1], 0, $opt_len)) {
    135                 throw new PHPUnit_Framework_Exception(
    136                     "option --$opt is ambiguous"
    137                 );
    138             }
    139 
    140             if (substr($long_opt, -1) == '=') {
    141                 if (substr($long_opt, -2) != '==') {
    142                     if (!strlen($opt_arg) &&
    143                         !(list(, $opt_arg) = each($args))) {
    144                         throw new PHPUnit_Framework_Exception(
    145                             "option --$opt requires an argument"
    146                         );
    147                     }
    148                 }
    149             } elseif ($opt_arg) {
    150                 throw new PHPUnit_Framework_Exception(
    151                     "option --$opt doesn't allow an argument"
    152                 );
    153             }
    154 
    155             $full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
    156             $opts[]      = array($full_option, $opt_arg);
    157 
    158             return;
    159         }
    160 
    161         throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
    162     }
    163 }