/** * 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 options. */ public function parse_options() { $getopt = new Getopt([['d', 'directory', Getopt::REQUIRED_ARGUMENT, 'Run under given directory'], ['l', 'level', Getopt::REQUIRED_ARGUMENT, 'Practice level on epic'], ['s', 'skip', Getopt::NO_ARGUMENT, 'Skip user input'], ['t', 'time', Getopt::REQUIRED_ARGUMENT, 'Delay each turn by seconds'], ['L', 'locale', Getopt::REQUIRED_ARGUMENT, 'Specify locale like en_US'], ['h', 'help', Getopt::NO_ARGUMENT, 'Show this message']]); try { $getopt->parse(); } catch (\Exception $e) { echo $e->getMessage() . "\n"; exit; } if ($getopt->getOption('h')) { echo $getopt->getHelpText(); exit; } if ($getopt->getOption('d')) { Config::$path_prefix = $getopt->getOption('d'); } if ($getopt->getOption('l')) { Config::$practice_level = $getopt->getOption('l'); } if ($getopt->getOption('s')) { Config::$skip_input = true; } if (!is_null($getopt->getOption('t'))) { Config::$delay = $getopt->getOption('t'); } // get locale from $LANG like en_US.UTF8 list(Config::$locale) = explode('.', getenv('LANG')); if ($getopt->getOption('L')) { Config::$locale = $getopt->getOption('L'); } }
private function getopt() { $getopt = new Getopt([(new Option('h', 'help', Getopt::NO_ARGUMENT))->setDescription('Show this help'), (new Option('d', 'default', Getopt::NO_ARGUMENT))->setDescription('Get Default config in json mode'), (new Option('r', 'reload', Getopt::NO_ARGUMENT))->setDescription('Reload workers'), (new Option('D', 'daemon', Getopt::NO_ARGUMENT))->setDescription('Daemonize'), (new Option('c', 'config', Getopt::OPTIONAL_ARGUMENT))->setDefaultValue(false)->setDescription('Set custom config file')->setArgument(new Argument(null, null, 'file path')), (new Option('v', 'verbose', Getopt::NO_ARGUMENT))->setDescription('Increase verbosity'), (new Option('V', 'version', Getopt::NO_ARGUMENT))->setDescription('Get version')]); $getopt->parse(); if ($getopt->getOption('h')) { echo $getopt->getHelpText(30); exit(0); } if ($getopt->getOption('V')) { echo "LVMCloud Core Worker\nVersion: " . Server::VERSION . PHP_EOL; exit(0); } if ($getopt->getOption('r')) { $this->getConfig(); $rel = $this->reload_extern(); if ($rel !== true) { echo $rel . PHP_EOL; } exit(0); } if ($getopt->getOption('D')) { $this->daemon = true; } if ($getopt->getOption('d')) { $this->getDefaults(); exit(0); } $loglevel = is_null($getopt->getOption('v')) ? 0 : $getopt->getOption('v'); $config = is_null($getopt->getOption('c')) || is_numeric($getopt->getOption('c')) ? false : realpath($getopt->getOption('c')); $this->setConsoleLogLevel($loglevel); if ($config) { $this->setConfigFile($config); } }
public static function main() { // manage the command line options $getopt = new Getopt(array((new Option(null, 'version', Getopt::NO_ARGUMENT))->setDescription('Display the current version'), (new Option(null, 'nomail', Getopt::NO_ARGUMENT))->setDescription('Do not send report email'), (new Option(null, 'csvfile', Getopt::REQUIRED_ARGUMENT))->setDescription('Use the specified path as csv input file'))); try { $getopt->parse(); if ($getopt->getOption('version') > 0) { echo "SotaImport version: " . Cli::VERSION . "\r\n"; exit(1); } $nomail = $getopt->getOption('nomail'); $csvfile = $getopt->getOption('csvfile'); } catch (\UnexpectedValueException $e) { echo "Error: " . $e->getMessage() . "\n"; echo $getopt->getHelpText(); exit(1); } $csvImport = new CsvImport(); if ($nomail > 0) { $csvImport->setSendMail(false); } if (isset($csvfile) && $csvfile != '') { $csvImport->setCsvFilePath($csvfile); } $csvImport->execute(); }
/** * 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); } }
protected function parseCommandLine() { try { $this->opts->parse(); } catch (\UnexpectedValueException $e) { echo "Error: " . $e->getMessage() . PHP_EOL . PHP_EOL; $this->printAbout(); $this->exitApp(self::FAILED_EXIT); } if (!$this->isValidCommandLine()) { $this->printAbout(); $this->exitApp(self::FAILED_EXIT); } }
/** * @return Configuration */ function getConfig() { $options = new Getopt([new Option(null, 'driver', Getopt::REQUIRED_ARGUMENT), new Option(null, 'hostname', Getopt::REQUIRED_ARGUMENT), new Option(null, 'username', Getopt::REQUIRED_ARGUMENT), new Option(null, 'password', Getopt::REQUIRED_ARGUMENT), new Option(null, 'database', Getopt::REQUIRED_ARGUMENT), new Option('n', 'namespace', Getopt::REQUIRED_ARGUMENT), new Option('o', 'outputDirectory', Getopt::REQUIRED_ARGUMENT), new Option(null, 'includeOverrideMethods', Getopt::NO_ARGUMENT)]); $options->parse(); $nn = function ($v) { return !is_null($v); }; $c = new Configuration(); $c->driver = $nn($options['driver']) ? $options['driver'] : $c->driver; $c->hostname = $nn($options['hostname']) ? $options['hostname'] : $c->hostname; $c->password = $options['password']; $c->database = $nn($options['database']) ? $options['database'] : $c->database; $c->namespace = $nn($options['namespace']) ? $options['namespace'] : $c->namespace; $c->outputDirectory = $nn($options['outputDirectory']) ? $options['outputDirectory'] : $c->outputDirectory; $c->includeOverrideMethods = $nn($options['includeOverrideMethods']) ? true : false; return $c; }
public function parse($arguments = null) { $optionsFromCommandLine = new Getopt\Getopt($this->addHelpOption(Onebip\array_map($this->options, function ($option) { return $option->specification(); }))); try { $optionsFromCommandLine->parse(); if ($this->helpHasBeenAsked($optionsFromCommandLine)) { $this->showHelpAndExitWith($optionsFromCommandLine, 0); } foreach ($this->options as $key => $option) { $this->values[$key] = $option->pickFrom($optionsFromCommandLine); } } catch (UnexpectedValueException $e) { $this->showErrorMessageAndExit($e, $optionsFromCommandLine); } return $this; }
/** * 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); } }
function retrieve_args() { $opt = new Getopt([(new Option('h', 'help'))->setDescription('Prints this help'), (new Option('v', 'verbose'))->setDescription('Verbose output'), (new Option('f', 'file-title'))->setDescription('Use file name for title tag'), (new Option('x', 'sort-lex'))->setDescription('Sort files lexicographically'), (new Option('t', 'tree-dst'))->setDescription('Retain the tree structure of the source album at destination'), (new Option('p', 'drop-dst'))->setDescription('Do not create destination directory'), (new Option('r', 'reverse'))->setDescription('Copy files in reverse order (last file first)'), (new Option('e', 'file-type', Getopt::REQUIRED_ARGUMENT))->setDescription('Accept only audio files of the specified type'), (new Option('u', 'unified-name', Getopt::REQUIRED_ARGUMENT))->setDescription('Base name for everything but the "Artist" tag'), (new Option('b', 'album-num', Getopt::REQUIRED_ARGUMENT))->setDescription('Album number'), (new Option('a', 'artist-tag', Getopt::REQUIRED_ARGUMENT))->setDescription('"Artist" tag'), (new Option('g', 'album-tag', Getopt::REQUIRED_ARGUMENT))->setDescription('"Album" tag')]); $opt->parse(); if ($opt->getOption('help')) { print $opt->getHelpText(); exit(2); } if (count($opt->getOperands()) !== 2) { print "Command line syntax: <src> and <dst> operands required.\n" . $opt->getHelpText(); exit(2); } if (!is_dir($opt->getOperand(0))) { print "Source directory \"{$opt->getOperand(0)}\" is not there.\n"; exit(2); } if (!is_dir($opt->getOperand(1))) { print "Destination path \"{$opt->getOperand(1)}\" is not there.\n"; exit(2); } return $opt; }
<?php /** * Creates a table containing rolling mean data for each player. */ use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; require_once 'init.php'; // Set up our command line arguments. $playerOption = (new Option(null, 'playerId', Getopt::REQUIRED_ARGUMENT))->setDescription('The ID of the player to calculate for (required)')->setValidation($vNotBlank); $args = array_merge($DatabaseOptions, [$playerOption]); $options = new Getopt($args); // Parse the command-line arguments and connect to the database. try { $options->parse(); } catch (UnexpectedValueException $e) { echo $e->getMessage() . "\n"; echo $options->getHelpText() . "\n"; exit; } // Connect to the database. $pdo = connectToDatabase($options); // Prepare the SQL. The base SQL allows for a rolling mean to be created from // a hard-coded set of game numbers. Here we modify that SQL so that we can // loop over a set of game numbers and store rolling mean data for each game // number for each player. $sql = file_get_contents('hitter-rolling-mean.sql'); $sql = str_replace('FROM', ', :s1 AS start_game, :e1 AS end_game FROM ', $sql); $sql = str_replace('/* AND p.id = */', ' AND p.id=:pid ', $sql); $sql = str_replace('1 AND 10', ' :s2 AND :e2 ', $sql); $sql = 'INSERT INTO hitter_rolling_mean ' . $sql;
*/ require_once 'core.php'; echo "Génération des listes de classe\n\n"; // ---------------------------------------------------------------------------- // Initialization // define('GETOPT_HELP_WIDTH', 21); // Process command-line arguments use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; use Ulrichsg\Getopt\Argument; $padding = str_repeat(' ', GETOPT_HELP_WIDTH + 1); $opt = new Getopt(array((new Option('f', 'filter', Getopt::REQUIRED_ARGUMENT))->setDescription('Ne traiter que les classes contenant <arg>'), (new Option('t', 'type', Getopt::REQUIRED_ARGUMENT))->setDescription("Type de fichier à générer: x=Excel, p=PDF (défaut), t=les deux")->setArgument(new Argument('p', 'ExportFileFormat::validate', 'type')), (new Option('s', 'send'))->setDescription("Envoi des fichiers Excel générés par e-mail\n" . $padding . "dédoubler l'argument (-ss) pour forcer l'envoi"), (new Option('k', 'keep'))->setDescription('Conserver les fichiers après envoi par e-mail'), (new Option('d', 'debug'))->setDescription("Utiliser une adresse de debug au lieu des destinataires\n" . $padding . "réels (implique l'option -s)"), (new Option('h', 'help'))->setDescription("Afficher ce message d'aide"))); $opt->setBanner("Utilisation: %s [options]\n"); try { $opt->parse(); } catch (UnexpectedValueException $e) { echo $opt->getHelpText(GETOPT_HELP_WIDTH) . "\n"; exit_error('Paramètre invalide (' . $e->getMessage() . ')'); } if ($opt['help']) { echo $opt->getHelpText(GETOPT_HELP_WIDTH); exit(0); } if ($opt['debug']) { echo 'DEBUG MODE: envoi des messages sur ' . implode(', ', $mail_debug_addresses) . "\n"; } // Retrieve data from classes export view if ($opt['filter']) { $filter = $opt['filter']; if (preg_match('/[%_]/', $filter)) {
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; ini_set('display_errors', 'on'); error_reporting(E_ALL); $getopt = new Getopt([new Option('h', 'host', Getopt::REQUIRED_ARGUMENT), new Option('u', 'username', Getopt::REQUIRED_ARGUMENT), new Option('p', 'password', Getopt::REQUIRED_ARGUMENT), new Option('q', 'query', Getopt::REQUIRED_ARGUMENT), new Option('f', 'pattern', Getopt::OPTIONAL_ARGUMENT), new Option('l', 'limit', Getopt::OPTIONAL_ARGUMENT), new Option('d', 'dry-run', Getopt::OPTIONAL_ARGUMENT)]); try { $getopt->parse(); $query = $getopt['query']; $limit = $getopt['limit'] ? $getopt['limit'] : 100; $dryRun = $getopt['dry-run'] ? true : false; $youtrack = new YouTrack\Connection($getopt['host'], $getopt['username'], $getopt['password']); $pattern = $getopt['pattern']; echo 'Executing query: "' . $query . '"' . PHP_EOL; $issues = $youtrack->getIssuesByFilter($query, null, $limit); echo count($issues) . ' issues found' . PHP_EOL; foreach ($issues as $issue) { $attachments = $issue->getAttachments(); $ac = count($attachments); $ts = substr($issue->created, 0, -3); $created = new \DateTime('@' . $ts); echo $issue->getId() . ', created: ' . $created->format('Y-m-d H:i:s') . ': ' . $ac . ' attachments.' . PHP_EOL; foreach ($attachments as $attachment) { echo ' ID: ' . $attachment->getId() . ' (' . $attachment->getName() . ')'; if (!$pattern || preg_match('/' . $pattern . '/', $attachment->getName())) { if (!$dryRun) { $success = $youtrack->deleteAttachment($issue, $attachment); if ($success) { echo ' deleted';
/** * 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; } }
use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; if (php_sapi_name() !== 'cli') { echo 'This Script must be run in a CLI.'; exit; } 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();
if (is_dir($path)) { $realIndex = $path . '/index.php'; } elseif (is_file($path)) { if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') { return false; } $realIndex = $path; } } // Try to find and load composer autoloader $vendorPaths = [realpath(__DIR__ . '/vendor/autoload.php'), realpath(__DIR__ . '/../vendor/autoload.php'), realpath(__DIR__ . '/../../vendor/autoload.php'), realpath(__DIR__ . '/../../../vendor/autoload.php'), realpath(__DIR__ . '/../../../../vendor/autoload.php'), realpath(__DIR__ . '/../../../../../vendor/autoload.php'), realpath('./vendor/autoload.php')]; foreach ($vendorPaths as $vendorPath) { if ($vendorPath && file_exists($vendorPath)) { require_once $vendorPath; break; } } // Parse additional options $cliOptions = new Getopt([[null, 'index', Getopt::OPTIONAL_ARGUMENT], [null, 'cov-src', Getopt::OPTIONAL_ARGUMENT], [null, 'cov-xml', Getopt::OPTIONAL_ARGUMENT], [null, 'cov-cov', Getopt::OPTIONAL_ARGUMENT], [null, 'cov-html', Getopt::OPTIONAL_ARGUMENT]]); $cliOptions->parse(getenv('PHPUNINT_ARGUMENTS')); $realIndex = isset($realIndex) ? $realIndex : realpath($cliOptions->getOption('index')); if (class_exists('\\JBZoo\\PHPUnit\\CovCatcher') && !(Sys::isPHP7() && Sys::hasXdebug())) { $testname = (new Data($_REQUEST))->get('testname'); $coverHash = md5(implode('||', [serialize($_REQUEST), serialize($_SERVER), PHP_VERSION])); $coverHash = $testname ? $testname . '-' . $coverHash : $testname; $covCatcher = new CovCatcher($coverHash, ['src' => $cliOptions->getOption('cov-src'), 'xml' => $cliOptions->getOption('cov-xml'), 'cov' => $cliOptions->getOption('cov-cov'), 'html' => $cliOptions->getOption('cov-html')]); $result = $covCatcher->includeFile($realIndex); } else { $result = (require_once $realIndex); } return $result;