public function testCanRetrieveBothShortAndLongSwitches() { $switchName = 'help'; $switchDesc = 'Display this help message'; $obj = new DefinedSwitches(); $origSwitch = $obj->addSwitch($switchName, $switchDesc); $origSwitch->setWithShortSwitch('h')->setWithShortSwitch('?')->setWithLongSwitch('help')->setWithLongSwitch('?'); // did it work? $this->assertTrue($obj->testHasSwitchByName($switchName)); $retrievedSwitch1 = $obj->getShortSwitch('h'); $this->assertSame($origSwitch, $retrievedSwitch1); $retrievedSwitch2 = $obj->getShortSwitch('?'); $this->assertSame($origSwitch, $retrievedSwitch2); $this->assertSame($retrievedSwitch1, $retrievedSwitch2); $retrievedSwitch3 = $obj->getLongSwitch('help'); $this->assertSame($origSwitch, $retrievedSwitch1); $retrievedSwitch4 = $obj->getLongSwitch('?'); $this->assertSame($origSwitch, $retrievedSwitch2); $this->assertSame($retrievedSwitch1, $retrievedSwitch2); $this->assertSame($retrievedSwitch1, $retrievedSwitch3); $this->assertSame($retrievedSwitch1, $retrievedSwitch4); }
protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwitches, DefinedSwitches $expectedOptions) { // $args[$argIndex] contains one or more short switches, // which we expect to be defined in $expectedOptions $switchStringLength = strlen($args[$argIndex]); for ($j = 1; $j < $switchStringLength; $j++) { $shortSwitch = $args[$argIndex][$j]; // is this a valid switch? if (!$expectedOptions->testHasShortSwitch($shortSwitch)) { // we didn't expect this switch throw new \Exception("unknown switch " . $shortSwitch); } // yes it is $switch = $expectedOptions->getShortSwitch($shortSwitch); $arg = true; // should it have an argument? if ($switch->testHasArgument()) { // yes, but it may be optional // are we the first switch in this string? if ($j == 1) { // assume the rest of the string is the argument if ($j != $switchStringLength - 1) { list($arg, $argIndex) = $this->parseArgument($args, $argIndex, 2, $switch, '-' . $shortSwitch); // we've finished with this string, // so set $j to exit the loop $j = $switchStringLength - 1; } else { list($arg, $argIndex) = $this->parseArgument($args, $argIndex + 1, 0, $switch, '-' . $shortSwitch); } } else { // are we at the end of the list of switches? if ($j != $switchStringLength - 1) { // no, we are not // this is invalid behaviour throw new \Exception('switch -' . $shortSwitch . ' expected argument'); } list($arg, $argIndex) = $this->parseArgument($args, $argIndex + 1, 0, $switch, '-' . $shortSwitch); } } // var_dump("Adding switch " . $switch->name); $ParsedSwitches->addSwitch($expectedOptions, $switch->name, $arg); } // increment our counter through the args $argIndex++; // return the counter return $argIndex; }