public function testCanRetrieveListOfSwitchesInRightOrderToDisplay() { $options = new DefinedSwitches(); $options->addSwitch('version', 'show the version number')->setWithShortSwitch('v')->setWithLongSwitch('version'); $options->addSwitch('properties', 'specify the build.properties file to use')->setWithShortSwitch('b')->setWithLongSwitch('build.properties')->setWithRequiredArg('<build.properties>', 'the path to the build.properties file to use')->setArgHasDefaultValueOf('build.properties'); $options->addSwitch('packageXml', 'specify the package.xml file to expand')->setWithShortSwitch('p')->setWithLongSwitch('packageXml')->setwithRequiredArg('<package.xml>', 'the path to the package.xml file to use')->setArgHasDefaultValueOf('.build/package.xml'); $options->addSwitch('srcFolder', 'specify the src folder to feed into package.xml')->setWithShortSwitch('s')->setWithLongSwitch('src')->setWithRequiredArg('<folder>', 'the path to the folder where the package source files are')->setArgHasDefaultValueOf('src'); $options->addSwitch('help', 'displays a summary of how to use this command')->setWithShortSwitch('h')->setWithShortSwitch('?')->setWithLongSwitch('help'); $options->addSwitch('include', 'adds additional folders to PHP include_path')->setWithShortSwitch('I')->setWithLongSwitch('include')->setWithRequiredArg('<path>', 'path to add to include_path')->setLongDesc("phix finds all of its commands by searching PHP's include_path for PHP files in " . "folders called 'PhixCommands'. If you want to phix to look in other folders " . "without having to add them to PHP's include_path, use --include to tell phix " . "to look in these folders." . \PHP_EOL . \PHP_EOL . "phix expects '<path>' to point to a folder that conforms to the PSR0 standard " . "for autoloaders." . \PHP_EOL . \PHP_EOL . "For example, if your command is the class '\\Me\\Tools\\PhixCommands\\ScheduledTask', phix would " . "expect to autoload this class from the 'Me/Tools/PhixCommands/ScheduledTask.php' file." . \PHP_EOL . \PHP_EOL . "If your class lives in the './myApp/lib/Me/Tools/PhixCommands' folder, you would call phix " . "with 'phix --include=./myApp/lib'"); $switches = $options->getSwitchesInDisplayOrder(); // short switches first ... // // do we have the expected structure back? $this->assertTrue(isset($switches['shortSwitchesWithArgs'])); $this->assertTrue(isset($switches['shortSwitchesWithoutArgs'])); // has it worked? $expectedOrder = array('I', 'b', 'p', 's'); $actualOrder = array_keys($switches['shortSwitchesWithArgs']); $this->assertEquals($expectedOrder, $actualOrder); $expectedOrder = array('?', 'h', 'v'); $actualOrder = array_keys($switches['shortSwitchesWithoutArgs']); $this->assertEquals($expectedOrder, $actualOrder); // then long switches // // do we have the expected structure back? $this->assertTrue(isset($switches['longSwitchesWithArgs'])); $this->assertTrue(isset($switches['longSwitchesWithoutArgs'])); // has it worked? $expectedOrder = array('build.properties', 'include', 'packageXml', 'src'); $actualOrder = array_keys($switches['longSwitchesWithArgs']); $this->assertEquals($expectedOrder, $actualOrder); $expectedOrder = array('help', 'version'); $actualOrder = array_keys($switches['longSwitchesWithoutArgs']); $this->assertEquals($expectedOrder, $actualOrder); // finally, the list of all switches // // do we have the expected structure back? $this->assertTrue(isset($switches['allSwitches'])); // has it worked? $expectedOrder = array('-?', '-I', '-b', '-h', '-p', '-s', '-v', '--build.properties', '--help', '--include', '--packageXml', '--src', '--version'); $actualOrder = array_keys($switches['allSwitches']); $this->assertEquals($expectedOrder, $actualOrder); }
protected function requireValidExpectedSwitchName(DefinedSwitches $expectedOptions, $switchName) { if (!$expectedOptions->testHasSwitchByName($switchName)) { throw new \Exception("Unknown switch name " . $switchName); } }
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; }