Beispiel #1
0
 /**
  * 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 testParseWithArgumentValidation()
 {
     $validation = 'is_numeric';
     $optionA = new Option('a', null, Getopt::OPTIONAL_ARGUMENT);
     $optionA->setArgument(new Argument(null, $validation));
     $optionB = new Option('b', null, Getopt::REQUIRED_ARGUMENT);
     $optionB->setArgument(new Argument(null, $validation));
     $optionC = new Option('c', null, Getopt::OPTIONAL_ARGUMENT);
     $optionC->setArgument(new Argument(null, $validation));
     $parser = new CommandLineParser(array($optionA, $optionB, $optionC));
     $parser->parse('-a 1 -b 2 -c');
     $options = $parser->getOptions();
     $this->assertSame('1', $options['a']);
     $this->assertSame('2', $options['b']);
     $this->assertSame(1, $options['c']);
 }