private static function handleArguments($args) { $scriptName = array_shift($args); if (count($args) == 0) { echo self::getHelp(); } elseif ($args[0][0] == '-') { $parser = new OptionParser(self::getOptions()); list($opts, $nonOpts) = $parser->parseArgs($args); if (isset($opts['version'])) { echo 'stato version ' . self::$version . "\n"; } if (isset($opts['help'])) { $command = self::getCommand($opts['help']); if (!$command) { echo "stato: '{$opts['help']}' is not a stato command. See 'stato --help'.\n"; } else { echo $command->getHelp(); } } } else { $commandName = array_shift($args); $command = self::getCommand($commandName); if (!$command) { echo "stato: '{$commandName}' is not a stato command. See 'stato --help'.\n"; return; } $parser = new OptionParser($command->getOptions()); list($opts, $nonOpts) = $parser->parseArgs($args); $command->run($opts, $nonOpts); } }
/** * Extends the list of known options. Takes the same argument types as the constructor. * * @param mixed $options * @throws \InvalidArgumentException */ public function addOptions($options) { if (is_string($options)) { $this->mergeOptions($this->optionParser->parseString($options)); } elseif (is_array($options)) { $this->mergeOptions($this->optionParser->parseArray($options)); } else { throw new \InvalidArgumentException("Getopt(): argument must be string or array"); } }
function parse_arguments(&$argv) { // Default options $defaults = array('i' => 'localhost', 'p' => '8000', "mime-file" => "/etc/mime.types", "wp-root" => ".", "wp-version" => "latest", "show-errors" => 'E_ALL', "show-assets" => false, "show-hooks" => '', "show-everything" => false, "wordpresses" => $_SERVER['HOME'] . "/.cache/whippet/wordpresses", "cb-cache" => $_SERVER['HOME'] . "/.cache/whippet/callback-cache", "multisite" => false); // Are there some options in a config file? Check them in order. if (file_exists("/etc/whippetrc")) { $defaults = array_merge($defaults, parse_ini_file("/etc/whippetrc")); } if (!empty($_SERVER['HOME']) && file_exists($_SERVER['HOME'] . "/.whippetrc")) { $defaults = array_merge($defaults, parse_ini_file($_SERVER['HOME'] . "/.whippetrc")); } $optparser = new OptionParser(); $optparser->addRule('h|help'); $optparser->addRule('i::'); $optparser->addRule('p::'); $optparser->addRule('siteurl::'); $optparser->addRule('q'); $optparser->addRule('multisite'); $optparser->addRule('mime-file::'); $optparser->addRule('no-sql'); $optparser->addRule('no-templates'); $optparser->addRule('no-params'); $optparser->addRule('no-scripts'); $optparser->addRule('show-assets'); $optparser->addRule('show-wp-errors'); $optparser->addRule('show-wp-queries'); $optparser->addRule('show-wp-hooks'); $optparser->addRule('show-errors::'); $optparser->addRule('show-everything'); $optparser->addRule('wp-version::'); $optparser->addRule('show-hooks::'); $optparser->addRule('wordpresses::'); try { $argv = $optparser->parse(); } catch (Exception $e) { echo Colours::fg('red') . "Error: " . Colours::fg("white") . $e->getMessage() . "\n\n"; usage(); exit(0); } $arguments = $optparser->getAllOptions(); if (!isset($arguments->siteurl)) { $i = isset($arguments['i']) ? $arguments['i'] : $defaults['i']; $p = isset($arguments['p']) ? $arguments['p'] : $defaults['p']; if ($p == 80) { $defaults['siteurl'] = "http://{$i}/"; } else { $defaults['siteurl'] = "http://{$i}:{$p}/"; } } return array_merge($defaults, $arguments); }
<?php require dirname(__FILE__) . '/../lib/OptionParser.php'; $debug = false; function enable_debugging() { global $debug; $debug = true; } $parser = new OptionParser(); $parser->addHead("Copies the contents of one file (or stream) to another.\n"); $parser->addHead("Usage: " . basename($argv[0]) . " -i <input> [ options ]\n"); $parser->addRule('d|debug', 'enable_debugging', "Enable debug mode"); $parser->addRule('i|input::', 'The input file to read, use "-" for stdin'); $parser->addRule('o|output:', '(optional) The output file to write to, defaults to stdout'); $parser->addRule('help', 'Display a help message and exit'); $parser->addTail("\nThis program brought to you by me!\n"); try { $parser->parse(); } catch (Exception $e) { die($parser->getUsage()); } if ($parser->help) { die($parser->getUsage()); } if (!isset($parser->input)) { die("You must specify an input file!\n"); } if ($parser->input == '-') { $input = 'php://stdin'; } else {
<?php require dirname(__FILE__) . '/../lib/OptionParser.php'; $parser = new OptionParser(); $parser->addHead("Echo the parameters of each flag as determined by OptionParser.\n"); $parser->addHead("Usage: " . basename($argv[0]) . " [ options ]\n"); $parser->addRule('a', "A short flag with no parameter"); $parser->addRule('b:', "A short flag with an optional parameter"); $parser->addRule('c::', "A short flag with a required parameter"); $parser->addRule('long-a', "A long flag with no parameter"); $parser->addRule('long-b:', "A long flag with an optional parameter"); $parser->addRule('long-c::', "A long flag with a required parameter"); $args = $argv; try { $parser->parse($args); } catch (Exception $e) { die($parser->getUsage()); } $flagNames = explode(' ', 'a b c long-a long-b long-c'); echo "Parsed arguments:\n"; foreach ($flagNames as $flag) { $param = var_export($parser->getOption($flag), true); echo sprintf(' %6s %s', $flag, $param) . "\n"; } echo "\nRemaining arguments: " . implode(' ', $args) . "\n";
public function testParseArrayInvalid() { $this->setExpectedException('InvalidArgumentException'); $this->parser->parseArray(array('a', 'b')); }
public function testLongOptionWithParameter() { $op = new OptionParser(); $op->addRule('verbose'); $op->addRule('dir-name::'); $op->addRule('long-req::'); $op->addRule('long-opt:'); $args = array("-", "--verbose=yes", '--dir-name', 'lib/test/dir', '--long-req=hi there', '--long-opt=-a'); $op->parse($args); $this->assertEquals($op->verbose, 'yes'); $this->assertEquals($op->getOption('dir-name'), 'lib/test/dir'); $this->assertEquals($op->getOption('long-req'), 'hi there'); $this->assertEquals($op->getOption('long-opt'), '-a'); $this->setExpectedException('Exception'); $args = array('-', '--dir-name'); $op->parse($args); }