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;
}