예제 #1
0
 function __construct($options = false, $handleUnknown = false)
 {
     global $argc, $argv;
     if ($options !== false) {
         self::$ACCEPTED = $options;
     }
     // Parse params
     if ($argc > 1) {
         $paramSwitch = false;
         for ($i = 1; $i < $argc; $i++) {
             $arg = $argv[$i];
             $isSwitch = preg_match('/^-+/', $arg);
             if ($isSwitch) {
                 $arg = preg_replace('/^-+/', '', $arg);
             }
             if ($paramSwitch and $isSwitch) {
                 $this->error("[param] expected after '{$paramSwitch}' switch (" . self::$ACCEPTED[1][$paramSwitch] . ')');
             } else {
                 if (!$paramSwitch and !$isSwitch) {
                     if ($handleUnknown) {
                         $this->params['unknown'][] = $arg;
                     } else {
                         $this->error("'{$arg}' is an invalid option, use --help to display valid switches.");
                     }
                 } else {
                     if (!$paramSwitch and $isSwitch) {
                         if (isset($this->params[$arg])) {
                             $this->error("'{$arg}' switch can't occur more than once");
                         }
                         $this->params[$arg] = true;
                         if (isset(self::$ACCEPTED[1][$arg])) {
                             $paramSwitch = $arg;
                         } else {
                             if (!isset(self::$ACCEPTED[0][$arg])) {
                                 $this->error("there's no '{$arg}' switch, use --help to display all switches.");
                             }
                         }
                     } else {
                         if ($paramSwitch and !$isSwitch) {
                             $this->params[$paramSwitch] = $arg;
                             $paramSwitch = false;
                         }
                     }
                 }
             }
         }
     }
     // Final check
     foreach ($this->params as $k => $v) {
         if (isset(self::$ACCEPTED[1][$k]) and $v === true) {
             $this->error("[param] expected after '{$k}' switch (" . self::$ACCEPTED[1][$k] . ')');
         }
     }
 }