/**
  * make parseDefinitions publicly callable
  *
  * @author Patrick Forget <patforg at webtrendi.com>
  */
 public function parseDefinitions()
 {
     parent::parseDefinitions();
 }
Ejemplo n.º 2
0
 /**
  * extracts params from arguments
  *
  * @author Patrick Forget <patforg at webtrendi.com>
  */
 protected function parseParams()
 {
     $argumentStack = $this->arguments;
     $expectingValue = false;
     $currentLongName = null;
     $currentValue = null;
     $trailingValues = "";
     $endOfDashedArguments = false;
     $addParam = false;
     $argumentsLeft = sizeof($argumentStack);
     $multiShortParams = array();
     $this->programName = array_shift($argumentStack);
     // remove first argument which is the program name
     while ($currentArgument = array_shift($argumentStack)) {
         $argumentsLeft--;
         $currentArgumentLength = strlen($currentArgument);
         // arguments that don't start with a dash
         if (substr($currentArgument, 0, 1) !== '-') {
             if ($expectingValue) {
                 $currentValue = $currentArgument;
                 $addParam = true;
             } else {
                 $trailingValues .= " " . $currentArgument;
                 $endOfDashedArguments = true;
             }
             //if
             // double dash detected
         } elseif (substr($currentArgument, 1, 1) === '-') {
             if ($expectingValue) {
                 throw new \UnexpectedValueException("Parameter {$currentLongName} expects a values");
             }
             //if
             /* stop parsing arguments if double dash
                only param is encountered  */
             if ($currentArgumentLength == 2) {
                 if ($trailingValues !== "") {
                     throw new \UnexpectedValueException("Trailing values must appear after double dash");
                 }
                 //if
                 $trailingValues = " " . implode(" ", $argumentStack);
                 $argumentStack = array();
                 $endOfDashedArguments = true;
                 break;
             }
             //if
             $longNameParts = explode("=", substr($currentArgument, 2), 2);
             $currentLongName = $longNameParts[0];
             if (sizeof($longNameParts) > 1) {
                 $currentValue = $longNameParts[1];
                 $addParam = true;
             } elseif ($this->definitions->allowsValue($currentLongName)) {
                 $expectingValue = true;
             } else {
                 $addParam = true;
             }
             //if
             // single dash
         } else {
             if ($expectingValue) {
                 throw new \UnexpectedValueException("Parameter {$currentLongName} expects a values");
             }
             //if
             $shortNameParts = explode("=", substr($currentArgument, 1), 2);
             $shortName = $shortNameParts[0];
             if (strlen($shortName) <= 1) {
                 $currentLongName = $this->definitions->getLongName($shortName);
                 if ($currentLongName === null) {
                     throw new \InvalidArgumentException("Unable to find name with " . "provided parameter ({$shortName})");
                 }
                 //if
                 if (sizeof($shortNameParts) > 1) {
                     $currentValue = $shortNameParts[1];
                     $addParam = true;
                 } elseif ($this->definitions->allowsValue($currentLongName)) {
                     $expectingValue = true;
                 } else {
                     $addParam = true;
                 }
                 //if
             } else {
                 $multiShortParams = str_split($shortName);
                 /* process the last one (which is the only one that can have a value) */
                 $lastParam = array_pop($multiShortParams);
                 $currentLongName = $this->definitions->getLongName($lastParam);
                 if (sizeof($shortNameParts) > 1) {
                     $currentValue = $shortNameParts[1];
                     $addParam = true;
                 } elseif ($this->definitions->allowsValue($lastParam)) {
                     $expectingValue = true;
                 } else {
                     $addParam = true;
                 }
                 //if
             }
             //if
         }
         //if
         if ($addParam) {
             if ($endOfDashedArguments) {
                 throw new \UnexpectedValueException("Unexpected argument after undashed values");
             }
             //if
             /* Not sure how this could happen */
             // @codeCoverageIgnoreStart
             if ($currentLongName === false || $currentLongName === null) {
                 throw new \UnexpectedValueException("Missing argument name");
             }
             //if
             // @codeCoverageIgnoreEnd
             if (!$this->definitions->paramExists($currentLongName)) {
                 throw new \InvalidArgumentException("Invalid argument name");
             }
             //if
             $allowsMultiple = $this->definitions->allowsMultiple($currentLongName);
             $allowsValue = $this->definitions->allowsValue($currentLongName);
             if (isset($this->params[$currentLongName]) && !$allowsMultiple) {
                 throw new \UnexpectedValueException("Multiple instace of parameter {$currentLongName} not allowed");
             }
             //if
             if ($allowsValue) {
                 /* Missing values should always be detected before addParam is true */
                 // @codeCoverageIgnoreStart
                 if ($currentValue === null) {
                     throw new \UnexpectedValueException("Parameter {$currentLongName} expects a values");
                 }
                 //if
                 // @codeCoverageIgnoreEnd
             } elseif ($currentValue !== null) {
                 throw new \UnexpectedValueException("Parameter {$currentLongName} does not accept values");
             } else {
                 $currentValue = true;
             }
             //if
             if ($allowsMultiple) {
                 if ($allowsValue) {
                     if (!isset($this->params[$currentLongName])) {
                         $this->params[$currentLongName] = array();
                     }
                     //if
                     $this->params[$currentLongName][] = $currentValue;
                 } else {
                     if (!isset($this->params[$currentLongName])) {
                         $this->params[$currentLongName] = 0;
                     }
                     //if
                     $this->params[$currentLongName]++;
                 }
                 //if
             } else {
                 $this->params[$currentLongName] = $currentValue;
             }
             //if
             foreach ($multiShortParams as $shortName) {
                 $argumentStack[] = "-{$shortName}";
                 $argumentsLeft++;
             }
             //foreach
             /* reset stuff for next param */
             $expectingValue = false;
             $currentLongName = null;
             $currentValue = null;
             $addParam = false;
             $multiShortParams = array();
         }
         //if
     }
     //while
     if ($expectingValue !== false) {
         throw new \UnexpectedValueException("Parameter {$currentLongName} expects a values");
     }
     //if
     /* Not sure how this could happen */
     // @codeCoverageIgnoreStart
     if ($currentLongName !== null || $addParam !== false || $currentValue !== null || sizeof($multiShortParams) !== 0) {
         throw new \UnexpectedValueException("Unable to process some parameters");
     }
     //if
     // @codeCoverageIgnoreEnd
     if ($trailingValues !== "") {
         $this->trailingValues = substr($trailingValues, 1);
         // remove extra space at the begging
     }
     //if
     $this->parsed = true;
 }