/** * Does something strange with the option and its argument. * * @internal * @throws Opl_Getopt_Exception * @param Opl_Getopt_Option $option The option * @param string $optionName The option long name for debug purposes. * @param string $optionArgs The option argument */ private function _parseOption(Opl_Getopt_Option $option, $optionName, $optionArgs) { $argument = $option->getArgument(); // Test the arguments if ($argument === null && $optionArgs !== null) { throw new Opl_Getopt_Exception('The option ' . $optionName . ' does not take any arguments.'); } elseif ($argument[0] === Opl_Getopt_Option::REQUIRED && $optionArgs === null) { throw new Opl_Getopt_Exception('The option ' . $optionName . ' requires an argument.'); } // Add the argument. if ($optionArgs !== null) { if ($option->isFound()) { $argVal = $option->getValue(); if (is_array($argVal)) { $argVal[] = $this->_validateArgument($optionName, $optionArgs, $argument[1]); $option->setValue($argVal); } else { $option->setValue(array($argVal, $this->_validateArgument($optionName, $optionArgs, $argument[1]))); } } else { $option->setValue($this->_validateArgument($optionName, $optionArgs, $argument[1])); } } // Now check if it can occur multiple times... if (!($this->_flags & self::ALLOW_INCREMENTING) && $option->isFound()) { throw new Opl_Getopt_Exception($optionName . ' has been used twice.'); } $option->setFound(true); $this->_foundOpts[] = $option; }
/** * @covers Opl_Getopt_Option::setMinOccurences * @covers Opl_Getopt_Option::getMinOccurences */ public function testCountingOccurencesByFoundStatus() { $option = new Opl_Getopt_Option('foo', 'x'); $this->assertEquals(0, $option->getOccurences()); $option->setFound(true); $this->assertEquals(1, $option->getOccurences()); $option->setFound(true); $this->assertEquals(2, $option->getOccurences()); $option->setFound(true); $this->assertEquals(3, $option->getOccurences()); $option->setFound(false); $this->assertEquals(0, $option->getOccurences()); }
/** * @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'); } }