/** * Parses the input and returns the Getopt handler. * * @return Getopt */ public function parse() { $this->handler->parse($this->input); $output = $this->handler->getOptions(); if (false === isset($output['from'])) { throw new \BadMethodCallException('The --from argument is required.'); } if (false === isset($output['release'])) { throw new \BadMethodCallException('The --release argument is required.'); } return $this->handler; }
/** * Parse the arguments. * @return array */ private function parseArguments() { try { $this->getopt->parse(); $options = $this->getopt->getOptions(); if (isset($options['version'])) { echo sprintf($this->getopt->getBanner(), ''); exit(0); } if (!isset($options['extpath'])) { throw new \Exception('Option \'extpath\' must be given'); } $this->options = $options; } catch (\Exception $exception) { echo sprintf($this->getopt->getBanner(), PHP_EOL . $exception->getMessage() . PHP_EOL); exit(0); } }
/** * Parse the arguments. * @return array */ private function parseArguments() { try { $this->getopt->parse(); $options = $this->getopt->getOptions(); if (isset($options['version'])) { $this->showVersion(); } if (isset($options['help'])) { $this->showUsage(); } if (!isset($options['config'])) { throw new \Exception('Option \'config\' must be given'); } if (!file_exists($options['config'])) { throw new \Exception('Option \'config\' must be a valid file'); } $this->options = $options; } catch (\Exception $exception) { $this->showFailure($exception); } }
/** * The main logic block for dispatching the CLI to the implementation * classes. Assuming it makes it all the way to the actual method cleanly, * its return code will be propagated up. Otherwise, RuntimeExceptions exit * 1 (user error), LogicExceptions exit 2 (programmer error), and everything * else exits 3 (doom) * * @return int The intended exit status cide */ public function run() { $trie = self::getCommandTrie(); $class = Utilities::searchTrieFromArgv($trie, $this->argv); $cmd = new $class(); $banner = $cmd->getBanner(); try { $opt = new Getopt(); $opt->addOptions($cmd->getOptions()); $opt->addOptions(self::getDefaultOptions()); if ($banner) { $opt->setBanner($banner . PHP_EOL); } $opt->parse(implode(' ', $this->argv)); } catch (\UnexpectedValueException $e) { // Unexpected CLI arguments $this->console->exception($e); $this->console->writeLine($opt->getHelpText()); return 1; } catch (\InvalidArgumentException $e) { // Command is broken - most likely duplicated arguments $this->console->exception($e); return 2; } catch (\Exception $e) { // Catch-all, $this->console->exception($e); return 3; } // Unfortunately we can't easily do this earlier. Native getopt() is // useless on all subcommands, and the class implementation screams // about unexpected values. $v = Utilities::parseVerbosity($opt['q'], $opt['v']); $this->console->setVerbosity($v); if ($opt['help']) { return $this->showHelp($cmd, $opt->getHelpText()); } if ($opt['version']) { return $this->showVersion($cmd); } try { return $cmd->setOutput($this->console)->setOperands($opt->getOperands())->setOptionValues($opt->getOptions())->execute(); } catch (\RuntimeException $e) { $this->console->exception($e); $this->console->writeLine($opt->getHelpText()); return 1; } catch (\LogicException $e) { $this->console->exception($e); return 2; } catch (\Exception $e) { $this->console->exception($e); return 3; } }
} if (strpos(__DIR__, 'vendor') === false) { // We are in a local development $vendorDirectory = __DIR__ . '/../../vendor/'; } else { // We are in vendor directory $vendorDirectory = __DIR__ . '/../../../../'; } require_once $vendorDirectory . 'autoload.php'; $getOpt = new Getopt(array(new Option('e', 'entity', Getopt::REQUIRED_ARGUMENT), new Option('f', 'file', Getopt::OPTIONAL_ARGUMENT), new Option(null, 'regenerate', Getopt::OPTIONAL_ARGUMENT), new Option(null, 'clean', Getopt::OPTIONAL_ARGUMENT), new Option(null, 'help', Getopt::NO_ARGUMENT), new Option(null, 'version', Getopt::NO_ARGUMENT))); $getOpt->parse(); if ($getOpt->getOption('help')) { echo $getOpt->getHelpText(); } else { if ($getOpt->getOption('version')) { echo 'Version ' . \OpenLdapObject\OpenLdapObject::VERSION . ' (' . \OpenLdapObject\OpenLdapObject::DATE . ')' . PHP_EOL; } else { if ($getOpt->getOption('entity')) { if ($getOpt->getOption('regenerate')) { $command = new \OpenLdapObject\Command\ReGenerateCommand($getOpt->getOptions()); } elseif ($getOpt->getOption('clean')) { $command = new \OpenLdapObject\Command\CleanCommand($getOpt->getOptions()); } else { $command = new \OpenLdapObject\Command\GenerateCommand($getOpt->getOptions()); } $command->exec(); } else { echo $getOpt->getHelpText(); } } }