/** * @param \GetOptionKit\OptionCollection $opts */ public function options($opts) { $opts->add('test', 'Run tests after the installation.'); $opts->add('name:', 'The name of the installation. By default the installed path is equal to the release version name (php-5.x.x), however you can specify a custom name instead of the default `php-5.x.x`. For example, `myphp-5.3.2-dbg`')->valueName('name'); $opts->add('mirror:', 'Use specified mirror site. phpbrew will download the files from [mirror]/distributions/*'); $opts->add('post-clean', 'Run make clean after the installation.'); $opts->add('production', 'Use production configuration file. this installer will copy the php-production.ini into the etc directory.'); $opts->add('build-dir:', 'Specify the build directory. ' . 'the distribution tarball will be extracted to the directory you specified ' . 'instead of $PHPBREW_ROOT/build/{name}.')->isa('dir'); $opts->add('root', 'specify phpbrew root instead of PHPBREW_ROOT'); $opts->add('home', 'specify phpbrew home instead of PHPBREW_HOME'); $opts->add('no-clean', 'Do not clean previously compiled objects before building PHP. ' . 'By default phpbrew will run `make clean` before runnign the configure script ' . 'to ensure everything is cleaned up.'); $opts->add('no-patch', 'Do not apply any patch'); $opts->add('no-configure', 'Do not run configure script'); $opts->add('no-install', 'Do not install, just run build the target'); $opts->add('n|nice:', 'Runs build processes at an altered scheduling priority. The priority can be adjusted over a range of -20 (the highest) to 20 (the lowest).')->valueName('priority'); $opts->add('patch+:', 'Apply patch before build.')->isa('file'); $opts->add('old', 'Install phpbrew incompatible phps (< 5.3)'); $opts->add('http-proxy:', 'The HTTP Proxy to download PHP distributions. e.g. --http-proxy=22.33.44.55:8080')->valueName('proxy host'); $opts->add('http-proxy-auth:', 'The HTTP Proxy Auth to download PHP distributions. user:pass')->valueName('user:pass'); $opts->add('user-config', 'Allow users create their own config file (php.ini or extension config init files)'); $opts->add('connect-timeout:', 'The system aborts the command if downloading ' . 'of a php version not starts during this limit. This option ' . 'overrides a value of CONNECT_TIMEOUT environment variable.')->valueName('seconds'); $opts->add('f|force', 'Force the installation (redownloads source).')->defaultValue(false); $opts->add('d|dryrun', 'Do not build, but run through all the tasks.'); $opts->add('like:', 'Inherit variants from an existing build. This option would require an existing build directory from the {version}.')->valueName('version'); $opts->add('j|jobs:', 'Specifies the number of jobs to run build simultaneously (make -jN).')->valueName('concurrent job number'); $opts->add('stdout', 'Outputs install logs to stdout.'); }
public static function getSubCommandsSpecs() { $generateSpecs = new OptionCollection(); $generateSpecs->add('i|input:', 'Input file')->isa('File'); $generateSpecs->add('o|output?', 'Output directory')->isa('String')->defaultValue(null); $subCommandSpecs = (object) ['generate' => $generateSpecs]; return $subCommandSpecs; }
/** * Get options. * * @since 150424 Initial release. * * @param array $specs An array of option specs. * * @link https://github.com/c9s/GetOptionKit */ public function add(array $specs) { foreach ($specs as $_spec => $_data) { if ($_spec && is_string($_spec) && !empty($_data['desc'])) { $_Option = $this->OptionCollection->add($_spec, $_data['desc']); if (!empty($_data['type'])) { $_Option->isa($_data['type']); } if (isset($_data['default'])) { $_Option->defaultValue($_data['default']); } if (isset($_data['valid_values'])) { $_Option->validValues($_data['valid_values']); } } } // unset($_spec, $_data, $_Option); }
public function test() { $options = new OptionCollection(); $options->add('f|foo:', 'option requires a value.')->isa('String'); $options->add('b|bar+', 'option with multiple value.')->isa('Number'); $options->add('z|zoo?', 'option with optional value.')->isa('Boolean'); $options->add('n', 'n flag'); $options->add('verbose', 'verbose'); $options->add('o|output?', 'option with optional value.')->isa('File')->defaultValue('output.txt'); $printer = new ConsoleOptionPrinter(); $output = $printer->render($options); }
public static function addOptionsForCommand(OptionCollection $opts) { $opts->add('downloader:', 'Specify downloader instead of the default downloader.'); $opts->add('continue', 'Continue getting a partially-downloaded file.'); $opts->add('http-proxy:', 'The HTTP Proxy to download PHP distributions. e.g. --http-proxy=22.33.44.55:8080')->valueName('proxy host'); $opts->add('http-proxy-auth:', 'The HTTP Proxy Auth to download PHP distributions. user:pass')->valueName('user:pass'); $opts->add('connect-timeout:', 'Overrides the CONNECT_TIMEOUT env variable and aborts if download takes longer than specified.')->valueName('seconds'); return $opts; }
/** * Configures the parameters to be sent to each Task action * * option configuration * -------------------------------------------------------- * o|option flag option (with boolean value true) * option: option requires a value * option+ option with multiple values * option? option with optional values * option:=string option with type constraint of string * option:=number option with type constraint of number * option:=file option with type constraint of file * option:=date option with type constraint of date * option:=boolean option with type constraint of boolean * o single character only option * option long option name * * @param array $cmdDef * @return array */ protected function parseArgs($argv, $cmdDef) { // Configure the command line definition $specs = new OptionCollection(); foreach ($cmdDef['opts'] as $option => $help) { $specs->add($option, $help); } // Every program will have an auto-generated help $specs->add('h|help', 'display this help and exit'); // Assign the command definition try { $parser = new OptionParser($specs); } catch (\Exception $e) { error_log("{$cmd}: The program has misconfigured options."); exit(1); } if (isset($argv[0]) && in_array($argv[0], ['--help', '-h'])) { throw new PrintHelpException($cmdDef, $specs); } // Use the options definition to parse the program arguments try { $result = $parser->parse($argv); } catch (RequireValueException $e) { throw new PrintHelpException($cmdDef, $specs, $e->getMessage(), 1); } catch (InvalidOptionException $e) { throw new PrintHelpException($cmdDef, $specs, $e->getMessage(), 1); } catch (\Exception $e) { throw new PrintHelpException($cmdDef, $specs, $e->getMessage(), 1); } // Ensure that the required arguments are supplied if (count($result->arguments) < count($cmdDef['args']['required'])) { throw new PrintHelpException($cmdDef, $specs, 'missing operand', 1); } // Clean arguments $args = array_map(function ($arg) { return $arg->arg; }, $result->arguments); // Clean options $opts = array_map(function ($opt) { return $opt->value; }, $result->keys); // The final result to be used in Tasks $params = ['args' => $args, 'opts' => $opts]; return $params; }
/** * @expectedException LogicException */ public function testAdvancedOutOfBounds() { $options = new OptionCollection(); $options->add("v|verbose"); $parser = new ContinuousOptionParser($options); $result = $parser->parse(array('app', '-v')); $parser->advance(); }
function testParser5() { $appspecs = new OptionCollection(); $appspecs->add('v|verbose'); $appspecs->add('c|color'); $appspecs->add('d|debug'); $cmdspecs = new OptionCollection(); $cmdspecs->add('a:'); $cmdspecs->add('b'); $cmdspecs->add('c'); $parser = new ContinuousOptionParser($appspecs); ok($parser); $subcommands = array('subcommand1', 'subcommand2', 'subcommand3'); $subcommand_specs = array('subcommand1' => clone $cmdspecs, 'subcommand2' => clone $cmdspecs, 'subcommand3' => clone $cmdspecs); $subcommand_options = array(); $argv = explode(' ', 'program subcommand1 -a 1 subcommand2 -a 2 subcommand3 -a 3 arg1 arg2 arg3'); $app_options = $parser->parse($argv); $arguments = array(); while (!$parser->isEnd()) { if (@$subcommands[0] && $parser->getCurrentArgument() == $subcommands[0]) { $parser->advance(); $subcommand = array_shift($subcommands); $parser->setSpecs($subcommand_specs[$subcommand]); $subcommand_options[$subcommand] = $parser->continueParse(); } else { $arguments[] = $parser->advance(); } } is('arg1', $arguments[0]); is('arg2', $arguments[1]); is('arg3', $arguments[2]); ok($subcommand_options); is(1, $subcommand_options['subcommand1']->a); ok(2, $subcommand_options['subcommand2']->a); ok(3, $subcommand_options['subcommand3']->a); }
/** * @expectedException LogicException */ public function testAddInvalidOption() { $opts = new OptionCollection(); $opts->add(123); }
/** * Prints a the program help * * @param array $cmdDef * @param \GetOptionKit\OptionCollection $specs */ private function printHelp($cmdDef, $specs) { $reqArgs = array_map('strtoupper', $cmdDef['args']['required']); $optArgs = array_map(function ($arg) { return '[' . strtoupper($arg) . ']'; }, $cmdDef['args']['optional']); $args = array_merge($reqArgs, $optArgs); $argNames = implode(' ', $args); echo "Usage: {$this->progPath} {$this->cmd} [OPTION] {$argNames}\n"; echo "{$cmdDef['title']}\n"; if (isset($cmdDef['help'])) { echo "{$cmdDef['help']}\n"; } echo "\n"; $widths = array_map(function ($spec) { return strlen($spec->renderReadableSpec()); }, $specs->all()); $width = max($widths); $lines = []; foreach ($specs->all() as $spec) { $c1 = str_pad($spec->renderReadableSpec(), $width); $line = sprintf("%s %s", $c1, $spec->desc); $lines[] = $line; } foreach ($lines as $line) { $line = trim($line); echo " {$line}\n"; } }
/** * Check if an option is one of the option in the collection */ public function anyOfOptions(OptionCollection $options) { $name = $this->getOptionName(); $keys = $options->keys(); return in_array($name, $keys); }
/** * @expectedException GetOptionKit\Exception\RequireValueException */ public function testParseOptionRequireValueException() { $options = new OptionCollection(); $options->add('name:=string', 'name'); $parser = new OptionParser($options); $parser->parse(array('app', '--name')); }
<?php use WsConsoleClient\Cli; use GetOptionKit\OptionCollection; use GetOptionKit\OptionParser; use GetOptionKit\OptionPrinter\ConsoleOptionPrinter; $specs = new OptionCollection(); $specs->add('a|address?', 'Server address.')->isa('String')->defaultValue('ws://localhost:9028'); $specs->add('c|channel?', 'Target channel.')->isa('String'); $specs->add('e|error?', 'Monitor PHP error log file.'); $specs->add('f|file?', 'Log file to monitor.')->isa('File'); $specs->add('m|message?', 'Publish message.')->isa('String'); $specs->add('help', 'Print this help.'); try { $option = (new OptionParser($specs))->parse($argv); } catch (\Exception $ex) { Cli::error($ex->getMessage(), 1, false); } if ($option->help) { Cli::out("Usage: ./vendor/bin/websocket-console-client [options] -m \"some message\"" . PHP_EOL . " Or: ./vendor/bin/websocket-console-client [options] -f /path/to/access.log" . PHP_EOL . PHP_EOL . "Options:" . PHP_EOL . (new ConsoleOptionPrinter())->render($specs)); } else { Cli::out(sprintf("Connecting to %s ...", $option->address)); $client = (new WsConsoleClient\Client($option->address))->getInstance(); $client->connect(); if ($option->error) { if (is_file(ini_get("error_log"))) { (new WsConsoleClient\FileClient($client))->monitor(ini_get("error_log"), "PHP error log"); } else { Cli::out("Failed to detect PHP log file. Try -f option instead."); } } elseif ($option->file) {
#!/usr/bin/env php <?php /* * This file is part of the GetOptionKit package. * * (c) Yo-An Lin <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * */ require 'vendor/autoload.php'; use GetOptionKit\OptionCollection; use GetOptionKit\OptionParser; use GetOptionKit\OptionPrinter\ConsoleOptionPrinter; $specs = new OptionCollection(); $specs->add('f|foo:', 'option requires a value.')->isa('String'); $specs->add('b|bar+', 'option with multiple value.')->isa('Number'); $specs->add('z|zoo?', 'option with optional value.')->isa('Boolean'); $specs->add('o|output?', 'option with optional value.')->isa('File')->defaultValue('output.txt'); // works for -vvv => verbose = 3 $specs->add('v|verbose', 'verbose')->isa('Number')->incremental(); $specs->add('file:', 'option value should be a file.')->trigger(function ($value) { echo "Set value to :"; var_dump($value); })->isa('File'); $specs->add('r|regex:', 'with custom regex type value')->isa('Regex', '/^([a-z]+)$/'); $specs->add('d|debug', 'debug message.'); $specs->add('long', 'long option name only.'); $specs->add('s', 'short option name only.'); $specs->add('m', 'short option m');
/** * Get argument specifications * @return OptionCollection */ public static function getArgumentSpecifications() { $specs = new OptionCollection(); $specs->add('d|dir:', 'Tests directory path')->isa('String'); $specs->add('s|subdir+', 'Optional, subdirectory pattern, can be used multiple times as OR expression')->isa('String')->defaultValue(null); $specs->add('b|bootstrap?', 'Bootstrap file path')->isa('File')->defaultValue(null); $specs->add('v|verbose', 'Verbose output')->defaultValue(false); $specs->add('show-globals', 'Show values of global variables')->defaultValue(false); $specs->add('debug', 'Show debug messages')->defaultValue(false); $specs->add('h|help', 'Show help')->defaultValue(false); $specs->add('no-colors', 'No colors')->defaultValue(false); $specs->add('i|immediate', 'Show error output immediately as it appears')->defaultValue(false); $specs->add('server-host?', 'Server host')->defaultValue(null); $specs->add('server-root', 'Server root path, default is ./public')->defaultValue('./public'); return $specs; }
} else { die("I can't find my own libraries\n"); } $loader = (require $moosh_dir . '/vendor/autoload.php'); $loader->add('Moosh\\', $moosh_dir); $loader->add('DiffMatchPatch\\', $moosh_dir . '/vendor/yetanotherape/diff-match-patch/src'); $options = array('debug' => true, 'optimizations' => 0); require_once $moosh_dir . '/includes/functions.php'; require_once $moosh_dir . '/includes/default_options.php'; use GetOptionKit\ContinuousOptionParser; use GetOptionKit\OptionCollection; @error_reporting(E_ALL | E_STRICT); @ini_set('display_errors', '1'); define('MOOSH_VERSION', '0.23'); define('MOODLE_INTERNAL', true); $appspecs = new OptionCollection(); $spec_verbose = $appspecs->add('v|verbose', "be verbose"); $appspecs->add('p|moodle-path:', "Moodle directory."); $appspecs->add('u|user:'******'n|no-user-check', "Don't check if Moodle data is owned by the user running script"); $appspecs->add('t|performance', "Show performance infomation including timings"); $appspecs->add('h|help', "Show global help."); $parser = new ContinuousOptionParser($appspecs); $app_options = $parser->parse($argv); if ($app_options->has('moodle-path')) { $top_dir = $app_options['moodle-path']->value; } else { $top_dir = find_top_moodle_dir($cwd); } $moodle_version = moosh_moodle_version($top_dir); $local_dir = home_dir() . DIRECTORY_SEPARATOR . '.moosh';
/** * * @param array $argsList * @param type $aliveObject * @param string $action */ private function getArgs(array &$argsList, $aliveObject, $action) { $listOptions = array(); if (isset($aliveObject->options)) { $listOptions = $aliveObject->options; } $specs = new OptionCollection(); foreach ($listOptions as $option => $spec) { if ($spec['type'] != 'boolean') { if ($spec['multiple']) { $option .= '+'; } else { if ($spec['required']) { $option .= ':'; } else { $option .= '?'; } } } $specs->add($option, $spec['help'])->isa($spec['type']); } $parser = new OptionParser($specs); $parsedOptions = self::parseOptions($this->arguments, $parser); if (isset($aliveObject->objectName)) { $events = Di::getDefault()->get('events'); $manageCommandOptionsEvent = new ManageCommandOptionsEvent($aliveObject->objectName, $action, $listOptions, $parsedOptions); $events->emit('core.manage.command.options', array($manageCommandOptionsEvent)); $listOptions = $manageCommandOptionsEvent->getOptions(); $aliveObject->options = $listOptions; } $listOptions = array_merge($listOptions, array('h|help' => array('help' => 'help', 'type' => 'boolean', 'functionParams' => '', "toTransform" => '', 'required' => false, 'defaultValue' => false))); $specs = new OptionCollection(); foreach ($listOptions as $option => $spec) { if ($spec['type'] != 'boolean') { if ($spec['multiple']) { $option .= '+'; } else { if ($spec['required']) { $option .= ':'; } else { $option .= '?'; } } } $specs->add($option, $spec['help'])->isa($spec['type']); } try { $parser = new OptionParser($specs); $optionsParsed = $parser->parse($this->arguments); } catch (RequireValueException $ex) { echo $ex->getMessage(); } if ($optionsParsed->help) { $printer = new ConsoleOptionPrinter(); echo $printer->render($specs); die; } unset($listOptions['h|help']); $this->manageConsoleParams($listOptions, $optionsParsed, $argsList); }
<?php /** * Runs PHPDoc2 on a collection of projects and generates HTML output. */ if (file_exists(__DIR__ . "/vendor/autoload.php")) { require __DIR__ . "/vendor/autoload.php"; } else { require __DIR__ . "/../../autoload.php"; } use GetOptionKit\OptionCollection; use GetOptionKit\OptionParser; use GetOptionKit\OptionPrinter\ConsoleOptionPrinter; $specs = new OptionCollection(); $specs->add('d|directory+', 'PHP directories to parse'); $specs->add('c|config?', 'Config JSON'); $specs->add('j|json?', 'Options database as JSON to file'); $specs->add('o|output?', 'Output HTML to given directory'); $specs->add('t|templates+', 'Add template directories'); $specs->add('help', 'Display help'); $parser = new OptionParser($specs); $result = $parser->parse($argv); if (isset($result['help'])) { $printer = new ConsoleOptionPrinter(); echo $printer->render($specs); return; } // now parse $dirs = array(); $json_file = false; $output_dir = "docs/";