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 parseLongSwitch($args, $argIndex, ParsedSwitches $ParsedSwitches, DefinedSwitches $expectedOptions) { // $args[i] contains a long switch, and might contain // a parameter too $equalsPos = strpos($args[$argIndex], '='); if ($equalsPos !== false) { $longSwitch = substr($args[$argIndex], 2, $equalsPos - 2); } else { $longSwitch = substr($args[$argIndex], 2); } $arg = null; // is this a switch we expected? if (!$expectedOptions->testHasLongSwitch($longSwitch)) { throw new \Exception("unknown switch " . $longSwitch); } // yes it is $switch = $expectedOptions->getLongSwitch($longSwitch); // should it have an argument? if ($switch->testHasArgument()) { // did we find one earlier? if ($equalsPos !== false) { // yes we did list($arg, $argIndex) = $this->parseArgument($args, $argIndex, $equalsPos + 1, $switch, '--' . $longSwitch); } else { // no we did not; it might be next list($arg, $argIndex) = $this->parseArgument($args, $argIndex + 1, 0, $switch, '--' . $longSwitch); } } // increment to the next item in the list $argIndex++; $ParsedSwitches->addSwitch($expectedOptions, $switch->name, $arg); // all done return $argIndex; }