/** * Starts the console application. The method checks the action to run, * parses the arguments and fires it. * * @throws Opl_Console_Exception * @param array $argv The argument list. */ public function run(array $argv) { $action = isset($argv[1]) ? $argv[1] : $this->_default; if (!Opl_Registry::exists('stdout')) { Opl_Registry::set('stdout', new Opl_Stream_Console_Output()); } if (!Opl_Registry::exists('stdin')) { Opl_Registry::set('stdin', new Opl_Stream_Console_Input()); } if (isset($this->_actions[$action])) { unset($argv[0], $argv[1]); $getopt = new Opl_Getopt(Opl_Getopt::ALLOW_LONG_ARGS | Opl_Getopt::ALLOW_SHORT_ARGS | Opl_Getopt::AUTO_HELP); foreach ($this->_actions[$action]->getParams() as $option) { $getopt->addOption($option); } if (!$getopt->parse(array_values($argv))) { // TODO: Exception goes here! return; } $this->runAction($action, $getopt->getIterator()->getArrayCopy()); } else { if ($this->_flags & self::AUTO_HELP) { $this->showHelp(); } else { throw new Opl_Console_Exception('The specified action \'' . $action . '\' does not exist.'); } } }
/** * @covers Opl_Getopt::__construct * @covers Opl_Getopt::parse * @covers Opl_Getopt::_validateArgument */ public function testArgumentsString() { $getopt = new Opl_Getopt(Opl_Getopt::ALLOW_LONG_ARGS); $getopt->addOption($option = new Opl_Getopt_Option('foo', null, 'foo')); $option->setArgument(Opl_Getopt_Option::REQUIRED, Opl_Getopt_Option::STRING); try { $getopt->parse(array('--foo=hello')); $this->assertSame('hello', $option->getValue()); } catch (Opl_Getopt_Exception $exception) { $this->fail('The string "hello" should be valid with Opl_Getopt_Option::STRING'); } $ok = false; try { $getopt->parse(array('--foo=')); } catch (Opl_Getopt_Exception $exception) { $ok = true; } if (!$ok) { $this->fail('The empty value should not be valid with Opl_Getopt_Option::STRING'); } }