GetOptionKit\ContinuousOptionParser::parse PHP Method

parse() public method

public parse ( array $argv )
$argv array
    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;
    }

Usage Example

Example #1
0
 /**
  * @param array $argv Array of arguments passed to script
  * @example
  * ```php
  * $binary = new Bin([
  *     __FILE__,
  *     '--help'
  * ]);
  *
  * return $binary->invoke();
  * ```
  */
 public function __construct($argv)
 {
     $parser = new ContinuousOptionParser(static::getArgumentSpecifications());
     $subCommandSpecs = static::getSubCommandsSpecs();
     $subCommands = array_keys((array) $subCommandSpecs);
     //Parse specification arguments from given arguments
     $this->arguments = $parser->parse($argv);
     $arguments = [];
     $subCommandOptions = new \stdClass();
     while (!$parser->isEnd()) {
         if (@$subCommands[0] && $parser->getCurrentArgument() == $subCommands[0]) {
             $parser->advance();
             $subCommand = array_shift($subCommands);
             $parser->setSpecs($subCommandSpecs->{$subCommand});
             $subCommandOptions->{$subCommand} = $parser->continueParse();
         } else {
             $arguments[] = $parser->advance();
         }
     }
     $this->subCommandOptions = $subCommandOptions;
 }
All Usage Examples Of GetOptionKit\ContinuousOptionParser::parse