/** * @covers Opl_Getopt_Option::setFound * @covers Opl_Getopt_Option::isFound */ public function testFoundStatus() { $option = new Opl_Getopt_Option('foo', 'x'); $option->setFound(true); $this->assertTrue($option->isFound()); $option->setFound(false); $this->assertFalse($option->isFound()); $option->setFound(17); $this->assertTrue($option->isFound()); }
/** * 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::__construct * @covers Opl_Getopt::parse */ public function testPlainArgumentsVersusShortArguments() { $getopt = new Opl_Getopt(Opl_Getopt::ALLOW_SHORT_ARGS); $getopt->addOption($short = new Opl_Getopt_Option('a', 'a')); $short->setArgument(Opl_Getopt_Option::REQUIRED, Opl_Getopt_Option::ANYTHING); $getopt->addOption(new Opl_Getopt_Option('foo', null, 'foo')); $getopt->addOption($option = new Opl_Getopt_Option('plain')); $this->assertTrue($getopt->parse(array('-a', 'moo', '--foo', 'goo'))); $this->assertTrue($short->isFound()); $this->assertSame('moo', $short->getValue()); $this->assertTrue($option->isFound()); $this->assertSame(array('goo'), $option->getValue()); }