/** * Evaluate the given arguments. These can be passed either as a string or as an array. * If nothing is passed, the running script's command line arguments are used. * * An {@link \UnexpectedValueException} or {@link \InvalidArgumentException} is thrown * when the arguments are not well-formed or do not conform to the options passed by the user. * * @param mixed $arguments optional ARGV array or space separated string */ public function parse($arguments = null) { $this->options = array(); if (!isset($arguments)) { global $argv; $arguments = $argv; $this->scriptName = array_shift($arguments); // $argv[0] is the script's name } elseif (is_string($arguments)) { $this->scriptName = $_SERVER['PHP_SELF']; $arguments = explode(' ', $arguments); } $parser = new CommandLineParser($this->optionList); $parser->parse($arguments); $this->options = $parser->getOptions(); $this->operands = $parser->getOperands(); }
public function testParseInvalidArgument() { $this->setExpectedException('UnexpectedValueException'); $validation = 'is_numeric'; $option = new Option('a', null, Getopt::OPTIONAL_ARGUMENT); $option->setArgument(new Argument(null, $validation)); $parser = new CommandLineParser(array($option)); $parser->parse('-a nonnumeric'); }