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'); }
/** * 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 static function Run($className) { try { if (!is_subclass_of($className, 'BaseApplication')) { throw new Exception('Application class must derive from BaseApplication'); } $parser = new CommandLineParser(); if (in_array('SetupCommandLine', get_class_methods($className))) { eval($className . '::SetupCommandLine( $parser );'); } $parser->AddArgument('switch: log; caption: Log file; optional: 1; default: <none>;'); $parameters = $parser->Parse(); if ($parameters != NULL) { $application = new $className($parameters); $application->Main(); } } catch (Exception $error) { print 'Error: ' . $error->getMessage() . "\n"; } }