/** * 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(); }
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; }
/** * 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; $stmt = $pdo->prepare($sql); // Calculate the rolling mean in groups of 10 games.
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)) { $msg = 'correspondant à'; } else {
$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'; } else { echo ' NOT deleted'; } } else { echo ' deleted (not really!)'; } } else { echo ' skipped'; } echo PHP_EOL; } echo PHP_EOL; } exit(0); } catch (UnexpectedValueException $e) { echo "Error: " . $e->getMessage() . PHP_EOL; echo $getopt->getHelpText(); exit(1); }
/** * 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 (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(); } else { echo $getOpt->getHelpText();
protected function printAbout() { echo sprintf("%s version %s (ko-worker version %s)", $this->name, $this->version, self::VERSION); echo PHP_EOL; echo $this->opts->getHelpText(); }
* - send the file to accountant */ require_once 'core.php'; echo "Génération du fichier de contrôle des cotisations\n"; // ---------------------------------------------------------------------------- // Initialization // // Process command-line arguments use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; $opt = new Getopt(array((new Option('o', 'only'))->setDescription('Générer le fichier uniquement, sans l\'envoyer par e-mail'), (new Option('k', 'keep'))->setDescription('Conserver le fichier après envoi par e-mail'), (new Option('d', 'debug'))->setDescription('Utiliser une adresse de debug au lieu des destinataires réels'), (new Option('h', 'help'))->setDescription("Afficher ce message d'aide"))); $opt->setBanner("\nUtilisation: %s [options]\n"); try { $opt->parse(); } catch (UnexpectedValueException $e) { echo $opt->getHelpText(18), "\n"; exit_error('Paramètre invalide'); } if ($opt['help']) { echo $opt->getHelpText(18); exit(0); } // Generated Excel file name $filename = '/tmp/APECOVE_contrôle_cotisations_' . date('Ymd') . '.xlsx'; // Retrieve data from export view $sql = "SELECT\n\t\temail,\n\t\tCONCAT(nom_pere, ' ', prenom_pere) \"Père\",\n\t\tCONCAT(nom_mere, ' ', prenom_mere) \"Mère\",\n\t\tadresse, npa, ville,\n\t\tcotisation\n\tFROM " . VIEW_MEMBRES_EXPORT; $qry = $db->query($sql); $data = $qry->fetchAll(); // ---------------------------------------------------------------------------- // Generate Excel spreadsheet //