/** * @param array $argv * * @return OptionResult|Option[] * * @throws Exception\RequireValueException * @throws Exception\InvalidOptionException * @throws \Exception */ public function parse(array $argv) { $result = new OptionResult(); list($argv, $extra) = $this->preprocessingArguments($argv); foreach ($this->specs as $opt) { if ($opt->defaultValue !== null) { $opt->setValue($opt->defaultValue); $result->set($opt->getId(), $opt); } } $len = count($argv); // some people might still pass only the option names here. $first = new Argument($argv[0]); if ($first->isOption()) { throw new Exception('parse(argv) expects the first argument to be the program name.'); } for ($i = 1; $i < $len; ++$i) { $arg = new Argument($argv[$i]); // if looks like not an option, push it to argument list. // TODO: we might want to support argument with preceding dash (?) if (!$arg->isOption()) { $result->addArgument($arg); continue; } // if the option is with extra flags, // split the string, and insert into the argv array if ($arg->withExtraFlagOptions()) { $extra = $arg->extractExtraFlagOptions(); array_splice($argv, $i + 1, 0, $extra); $argv[$i] = $arg->arg; // update argument to current argv list. $len = count($argv); // update argv list length } $next = null; if ($i + 1 < count($argv)) { $next = new Argument($argv[$i + 1]); } $spec = $this->specs->get($arg->getOptionName()); if (!$spec) { throw new InvalidOptionException('Invalid option: ' . $arg); } // This if expr might be unnecessary, becase we have default mode - flag // if ($spec->isRequired() || $spec->isMultiple() || $spec->isOptional() || $spec->isFlag()) { $i += $this->consumeOptionToken($spec, $arg, $next); $result->set($spec->getId(), $spec); } return $result; }
function parse($argv) { // create new Result object. $result = new OptionResult(); if (!$this->argv) { $this->argv = $argv; $this->length = count($argv); } if ($this->isEnd()) { return $result; } // from last parse index for (; $this->index < $this->length; ++$this->index) { $arg = new Argument($argv[$this->index]); /* let the application decide for: command or arguments */ if (!$arg->isOption()) { # echo "stop at {$this->index}\n"; return $result; } // if the option is with extra flags, // split it out, and insert into the argv array // // like -abc if ($arg->withExtraFlagOptions()) { $extra = $arg->extractExtraFlagOptions(); array_splice($argv, $this->index + 1, 0, $extra); $argv[$this->index] = $arg->arg; // update argument to current argv list. $len = count($argv); // update argv list length } $next = null; if ($this->index + 1 < count($argv)) { $next = new Argument($argv[$this->index + 1]); } $spec = $this->specs->getSpec($arg->getOptionName()); if (!$spec) { throw new InvalidOptionException("Invalid option: " . $arg); } if ($spec->isAttributeRequire()) { if (!$this->foundRequireValue($spec, $arg, $next)) { throw new RequireValueException("Option '{$arg->getOptionName()}' requires a value."); } $this->takeOptionValue($spec, $arg, $next); if ($next && !$next->isOption()) { $this->index++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeMultiple()) { $this->pushOptionValue($spec, $arg, $next); if ($next && !$next->isOption()) { $this->index++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeOptional()) { $this->takeOptionValue($spec, $arg, $next); if ($spec->value && $next && !$next->isOption()) { $this->index++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeFlag()) { $spec->value = true; $result->set($spec->getId(), $spec); } else { throw new Exception('Unknown attribute.'); } } return $result; }
/** * @param array $argv * @return OptionResult|Option[] * @throws Exception\RequireValueException * @throws Exception\InvalidOptionException * @throws \Exception */ public function parse(array $argv) { $result = new OptionResult(); $argv = $this->preprocessingArguments($argv); foreach ($this->specs as $spec) { if ($spec->defaultValue !== null) { $result->set($spec->getId(), $spec); } } $len = count($argv); for ($i = 0; $i < $len; ++$i) { $arg = new Argument($argv[$i]); // if looks like not an option, push it to argument list. // TODO: we might want to support argument with preceding dash (?) if (!$arg->isOption()) { $result->addArgument($arg); continue; } // if the option is with extra flags, // split the string, and insert into the argv array if ($arg->withExtraFlagOptions()) { $extra = $arg->extractExtraFlagOptions(); array_splice($argv, $i + 1, 0, $extra); $argv[$i] = $arg->arg; // update argument to current argv list. $len = count($argv); // update argv list length } $next = null; if ($i + 1 < count($argv)) { $next = new Argument($argv[$i + 1]); } $spec = $this->specs->get($arg->getOptionName()); if (!$spec) { throw new InvalidOptionException("Invalid option: " . $arg); } if ($spec->isRequired()) { if (!$this->foundRequireValue($spec, $arg, $next)) { throw new RequireValueException("Option {$arg->getOptionName()} requires a value. given '{$next}'"); } $this->takeOptionValue($spec, $arg, $next); if ($next && !$next->anyOfOptions($this->specs)) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isMultiple()) { $this->pushOptionValue($spec, $arg, $next); if ($next && $next->isOption()) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isOptional()) { $this->takeOptionValue($spec, $arg, $next); if (($spec->value || $spec->defaultValue) && $next && !$next->isOption()) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isIncremental()) { $spec->increaseValue(); $result->set($spec->getId(), $spec); } elseif ($spec->isFlag()) { $spec->setValue(true); $result->set($spec->getId(), $spec); } else { throw new Exception('Unknown attribute.'); } } return $result; }
public function parse(array $argv) { // create new Result object. $result = new OptionResult(); list($this->argv, $extra) = $this->preprocessingArguments($argv); $this->length = count($this->argv); // register option result from options with default value foreach ($this->specs as $opt) { if ($opt->defaultValue !== null) { $opt->setValue($opt->defaultValue); $result->set($opt->getId(), $opt); } } // from last parse index for (; $this->index < $this->length; ++$this->index) { $arg = new Argument($this->argv[$this->index]); /* let the application decide for: command or arguments */ if (!$arg->isOption()) { # echo "stop at {$this->index}\n"; return $result; } // if the option is with extra flags, // split it out, and insert into the argv array // // like -abc if ($arg->withExtraFlagOptions()) { $extra = $arg->extractExtraFlagOptions(); array_splice($this->argv, $this->index + 1, 0, $extra); $this->argv[$this->index] = $arg->arg; // update argument to current argv list. $this->length = count($this->argv); // update argv list length } $next = null; if ($this->index + 1 < count($this->argv)) { $next = new Argument($this->argv[$this->index + 1]); } $spec = $this->specs->get($arg->getOptionName()); if (!$spec) { throw new InvalidOptionException('Invalid option: ' . $arg); } // This if block is unnecessary // if ($spec->isRequired() || $spec->isMultiple() || $spec->isOptional() || $spec->isFlag()) { $this->index += $this->consumeOptionToken($spec, $arg, $next); $result->set($spec->getId(), $spec); } return $result; }
function parse($argv) { $result = new OptionResult(); $len = count($argv); for ($i = 0; $i < $len; ++$i) { $arg = new Argument($argv[$i]); if (!$arg->isOption()) { $result->addArgument($arg); continue; } // if the option is with extra flags, // split it out, and insert into the argv array if ($arg->withExtraFlagOptions()) { $extra = $arg->extractExtraFlagOptions(); array_splice($argv, $i + 1, 0, $extra); $argv[$i] = $arg->arg; // update argument to current argv list. $len = count($argv); // update argv list length } $next = new Argument(@$argv[$i + 1]); $spec = $this->specs->getSpec($arg->getOptionName()); if (!$spec) { throw new InvalidOptionException("Invalid option: " . $arg); } if ($spec->isAttributeRequire()) { if (!$this->foundRequireValue($spec, $arg, $next)) { throw new RequireValueException("Option {$arg->getOptionName()} require a value."); } $this->takeOptionValue($spec, $arg, $next); if (!$next->isOption()) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeMultiple()) { $this->pushOptionValue($spec, $arg, $next); if ($next->isOption()) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeOptional()) { $this->takeOptionValue($spec, $arg, $next); if ($spec->value && !$next->isOption()) { $i++; } $result->set($spec->getId(), $spec); } elseif ($spec->isAttributeFlag()) { $spec->value = true; $result->set($spec->getId(), $spec); } else { throw new Exception('Unknown attribute.'); } } return $result; }