public function validateAndExecute($args, $argsIndex, Context $context) { $so = $context->stdout; $se = $context->stderr; // step 1: parse the options $options = $this->getCommandOptions(); $parser = new CommandLineParser(); list($parsedSwitches, $argsIndex) = $parser->parseSwitches($args, $argsIndex, $options); // step 2: verify the args $errors = $parsedSwitches->validateSwitchValues(); if (count($errors) > 0) { // validation failed foreach ($errors as $errorMsg) { $se->output($context->errorStyle, $context->errorPrefix); $se->outputLine(null, $errorMsg); } // return the error code to the caller return 1; } // step 3: extract the values we need to carry on // var_dump($parsedSwitches); $buildPropertiesFile = $parsedSwitches->getFirstArgForSwitch('properties'); $packageXmlFile = $parsedSwitches->getFirstArgForSwitch('packageXml'); $pearConfig = $parsedSwitches->getFirstArgForSwitch('pearConfig'); // step 4: let's get it on return $this->populatePackageXmlFile($context, $buildPropertiesFile, $packageXmlFile, $pearConfig); }
protected function parsePhixArgs(Context $context, $argv) { // switches before the first command are switches that // affect phix. // // switches after the first command are switches for // that command $parser = new CommandLineParser(); return $parser->parseSwitches($argv, 1, $context->phixDefinedSwitches); }
public function testDefaultValuesAreAddedIfNoSwitchesPresent() { // this is a bug I first discovered in Phix, and here // is the code necessary to reproduce the faults $options = new DefinedSwitches(); $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')->setArgValidator(new MustBeValidFile()); $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')->setArgValidator(new MustBeValidFile())->setArgValidator(new MustBeWriteable()); $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')->setArgValidator(new MustBeValidPath()); $argv = array('./Phix', 'pear:expand-package-xml'); $parser = new CommandLineParser(); list($parsedSwitches, $argsIndex) = $parser->parseSwitches($argv, 2, $options); // did it work? $this->assertTrue($parsedSwitches instanceof ParsedSwitches); // is the argsIndex pointing to the right place? $this->assertTrue(is_int($argsIndex)); $this->assertEquals(2, $argsIndex); // are the defaults present? $switches = $parsedSwitches->getSwitches(); $this->assertTrue(is_array($switches)); $this->assertEquals(3, count($switches)); $this->assertTrue(isset($switches['properties'])); $this->assertTrue(isset($switches['packageXml'])); $this->assertTrue(isset($switches['srcFolder'])); }