function test3() { $arg = new Argument('-abc'); ok($arg->withExtraFlagOptions()); $args = $arg->extractExtraFlagOptions(); ok($args); count_ok(2, $args); is('-b', $args[0]); is('-c', $args[1]); is('-a', $arg->arg); }
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; }
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; }