public static function parseArgv($argv = null) { $argv = $argv !== null ? $argv : $_SERVER['argv']; $out = [0 => array_shift($argv)]; $countArgs = count($argv); for ($i = 0, $j = $countArgs; $i < $j; $i++) { $arg = $argv[$i]; if (substr($arg, 0, 2) === '--') { // --foo --bar=baz $eqPos = strpos($arg, '='); if ($eqPos === false) { // --foo $key = substr($arg, 0); if ($i + 1 < $j && $argv[$i + 1][0] !== '-') { // --foo value $value = $argv[$i + 1]; $i++; } else { $value = isset($out[$key]) ? $out[$key] : true; } $out[$key] = $value; } else { // --bar=baz $key = substr($arg, 0, $eqPos); $value = substr($arg, $eqPos + 1); $out[$key] = $value; } } else { // -k=value -abc if (substr($arg, 0, 1) === '-') { if (substr($arg, 2, 1) === '=') { // -k=value $key = substr($arg, 0, 2); $value = substr($arg, 3); $out[$key] = $value; } else { // -abc $chars = str_split(substr($arg, 1)); foreach ($chars as $char) { $key = '-' . $char; $value = isset($out[$key]) ? $out[$key] : true; $out[$key] = $value; } if ($i + 1 < $j && $argv[$i + 1][0] !== '-') { // -a value1 -abc value2 $out[$key] = $argv[$i + 1]; $i++; } } } else { // plain-arg $value = $arg; $out[] = $value; } } } self::$args = $out; return $out; }
/** * @return array */ public function dispatch() { $argv = $_SERVER['argv']; $resource = $argv[2]; array_shift($argv); array_shift($argv); $args = \CommandLine::parseArgs($argv); foreach ($args as $key => $value) { if (is_string($key)) { $this->options[$key] = $value; } else { $this->arguments[$key] = $value; } } return [$resource, 'action', []]; }
/** @return array */ private function loadConfig() { $cmd = new CommandLine(<<<XX FTP deployment v2.2 ------------------- Usage: \tdeploy.php <config_file> [-t | --test] Options: \t-t | --test Run in test-mode. \t--generate Only generates deployment file. XX , ['config' => [CommandLine::REALPATH => TRUE]]); if ($cmd->isEmpty()) { $cmd->help(); return; } $options = $cmd->parse(); $this->mode = $options['--generate'] ? 'generate' : ($options['--test'] ? 'test' : NULL); $this->configFile = $options['config']; return $this->loadConfigFile($options['config']); }
static function __static() { self::$WINDOWS = newinstance(__CLASS__, array(0, 'WINDOWS'), '{ static function __static() { } public function parse($cmd) { static $triple= "\\"\\"\\""; $parts= array(); $r= ""; for ($i= 0, $s= strlen($cmd); $i < $s; $i++) { if (" " === $cmd{$i}) { $parts[]= $r; $r= ""; } else if ("\\"" === $cmd{$i}) { $q= $i+ 1; do { if (FALSE === ($p= strpos($cmd, "\\"", $q))) { $q= $s; break; } $q= $p; if ($triple === substr($cmd, $q, 3)) { if (FALSE === ($p= strpos($cmd, $triple, $q+ 3))) { $q= $q+ 3; continue; } $q= $p+ 3; continue; } break; } while ($q < $s); $r.= str_replace($triple, "\\"", substr($cmd, $i+ 1, $q- $i- 1)); $i= $q; } else { $r.= $cmd{$i}; } } $parts[]= $r; return $parts; } protected static function quote($arg) { $l= strlen($arg); if ($l && strcspn($arg, "\\" ") >= $l) return $arg; return \'"\'.str_replace(\'"\', \'"""\', $arg).\'"\'; } public function compose($command, $arguments= array()) { $cmd= self::quote($command); foreach ($arguments as $arg) { $cmd.= " ".self::quote($arg); } return $cmd; } }'); self::$UNIX = newinstance(__CLASS__, array(1, 'UNIX'), '{ static function __static() { } public function parse($cmd) { $parts= array(); $o= 0; while (FALSE !== ($p= strcspn($cmd, " ", $o))) { $option= substr($cmd, $o, $p); if (1 === substr_count($option, \'"\')) { $l= $o+ $p; $qp= strpos($cmd, \'"\', $l)+ 1; $option.= substr($cmd, $l, $qp- $l); $o= $qp+ 1; } else if (1 === substr_count($option, "\'")) { $l= $o+ $p; $qp= strpos($cmd, "\'", $l)+ 1; $option.= substr($cmd, $l, $qp- $l); $o= $qp+ 1; } else { $o+= $p+ 1; } if (\'"\' === $option{0} || "\'" === $option{0}) $option= substr($option, 1, -1); $parts[]= $option; } return $parts; } protected static function quote($arg) { $l= strlen($arg); if ($l && strcspn($arg, "&;`\'\\"|*?~<>^()[]{}\\$ ") >= $l) return $arg; return "\'".str_replace("\'", "\'\\\'\'", $arg)."\'"; } public function compose($command, $arguments= array()) { $cmd= self::quote($command); foreach ($arguments as $arg) { $cmd.= " ".self::quote($arg); } return $cmd; } }'); }
/** * Get command line arguments * * @return string[] */ public function getArguments() { if (NULL === $this->status['arguments']) { $parts = CommandLine::forName(PHP_OS)->parse($this->status['command']); $this->status['arguments'] = array_slice($parts, 1); } return $this->status['arguments']; }
$fileName = str_replace($symbols, "_", $cat->getTitle()); $categoryFile .= "\\categoryfile{" . $fileName . "}" . PHP_EOL; } } return $categoryFile; } function generateShallowTexStructure($catPage) { $shallowTex = "\\tree{" . $catPage->getTitle() . "}{" . escape($catPage->intent) . "}{\n"; foreach ($catPage->members as $m) { $shallowTex .= "\\tab\\concept{" . $m->getTitle() . "}{" . escape($m->intent) . "}\n"; } return $shallowTex; } } $args = CommandLine::parseArgs($_SERVER['argv']); //$args['mode'] = 'matrixif'; //$args['ilist'] = "../lists/pppjImpls.lst"; //$args['output'] = "pppjif"; if ($args['mode'] == 'singlePage') { $title = $args['title']; if (startsWith("101implementation:", $title)) { echo $title; $page = new ImplementationPage($title); #var_dump($page); #echo $page->dumpToTex(); var_dump(formatter::toTex($page->illustration)); } } else { if ($args['mode'] == 'dump') { echo "Entering dump mode, please wait..." . PHP_EOL;
/** @test */ public function parseGlobalServerVariable() { $_SERVER['argv'] = array(self::FILE, 'a'); $result = CommandLine::parseArgv(null); $this->assertEquals(2, count($result)); }
return array_merge($this->getImplementedInterfaces($all), $this->getExtendedClasses($all)); } public function getNamespace() { return $this->namespace; } public function getFile() { return $this->file; } public function getName() { return $this->name; } public function getFQN() { return (string) $this->getNamespace() . '\\' . $this->name; } public function getPhpCode() { return $this->body; } public function __toString() { return "class " . $this->getName() . '{ ... }'; } } /* -------------------------------------------------------------------------- */ $options = CommandLine::getOptions(); $WeixinFile = WeixinFile::from($options['source'], $options['exclude']); $WeixinFile->saveTo($options['output']);
/** * PARSE ARGUMENTS * * This command line option parser supports any combination of three types of options * [single character options (`-a -b` or `-ab` or `-c -d=dog` or `-cd dog`), * long options (`--foo` or `--bar=baz` or `--bar baz`) * and arguments (`arg1 arg2`)] and returns a simple array. * * [pfisher ~]$ php test.php --foo --bar=baz --spam eggs * ["foo"] => true * ["bar"] => "baz" * ["spam"] => "eggs" * * [pfisher ~]$ php test.php -abc foo * ["a"] => true * ["b"] => true * ["c"] => "foo" * * [pfisher ~]$ php test.php arg1 arg2 arg3 * [0] => "arg1" * [1] => "arg2" * [2] => "arg3" * * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \ * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s * [0] => "plain-arg" * ["foo"] => true * ["bar"] => "baz" * ["funny"] => "spam=eggs" * ["also-funny"]=> "spam=eggs" * [1] => "plain arg 2" * ["a"] => true * ["b"] => true * ["c"] => true * ["k"] => "value" * [2] => "plain arg 3" * ["s"] => "overwrite" * * Not supported: `-cd=dog`. * * @author Patrick Fisher <*****@*****.**> * @since August 21, 2009 * @see https://github.com/pwfisher/CommandLine.php * @see http://www.php.net/manual/en/features.commandline.php * #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008 * #78651 function getArgs($args) by B Crawford, 22-Oct-2007 * @usage $args = CommandLine::parseArgs($_SERVER['argv']); */ public static function parseArgs($argv = null) { $argv = $argv ? $argv : $_SERVER['argv']; array_shift($argv); $out = array(); for ($i = 0, $j = count($argv); $i < $j; $i++) { $arg = $argv[$i]; // --foo --bar=baz if (substr($arg, 0, 2) === '--') { $eqPos = strpos($arg, '='); // --foo if ($eqPos === false) { $key = substr($arg, 2); // --foo value if ($i + 1 < $j && $argv[$i + 1][0] !== '-') { $value = $argv[$i + 1]; $i++; } else { $value = isset($out[$key]) ? $out[$key] : true; } $out[$key] = $value; } else { $key = substr($arg, 2, $eqPos - 2); $value = substr($arg, $eqPos + 1); $out[$key] = $value; } } else { if (substr($arg, 0, 1) === '-') { // -k=value if (substr($arg, 2, 1) === '=') { $key = substr($arg, 1, 1); $value = substr($arg, 3); $out[$key] = $value; } else { $chars = str_split(substr($arg, 1)); foreach ($chars as $char) { $key = $char; $value = isset($out[$key]) ? $out[$key] : true; $out[$key] = $value; } // -a value1 -abc value2 if ($i + 1 < $j && $argv[$i + 1][0] !== '-') { $out[$key] = $argv[$i + 1]; $i++; } } } else { $value = $arg; $out[] = $value; } } } self::$args = $out; return $out; }
/** * PARSE ARGUMENTS * * This command line option parser supports any combination of three types * of options (switches, flags and arguments) and returns a simple array. * * [pfisher ~]$ php test.php --foo --bar=baz * ["foo"] => true * ["bar"] => "baz" * * [pfisher ~]$ php test.php -abc * ["a"] => true * ["b"] => true * ["c"] => true * * [pfisher ~]$ php test.php arg1 arg2 arg3 * [0] => "arg1" * [1] => "arg2" * [2] => "arg3" * * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \ * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s * [0] => "plain-arg" * ["foo"] => true * ["bar"] => "baz" * ["funny"] => "spam=eggs" * ["also-funny"]=> "spam=eggs" * [1] => "plain arg 2" * ["a"] => true * ["b"] => true * ["c"] => true * ["k"] => "value" * [2] => "plain arg 3" * ["s"] => "overwrite" * * @author Patrick Fisher <*****@*****.**> * @since August 21, 2009 * @see http://www.php.net/manual/en/features.commandline.php * #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008 * #78651 function getArgs($args) by B Crawford, 22-Oct-2007 * @usage $args = CommandLine::parseArgs($_SERVER['argv']); */ public static function parseArgs($argv) { array_shift($argv); $out = array(); foreach ($argv as $arg) { // --foo --bar=baz if (substr($arg, 0, 2) == '--') { $eqPos = strpos($arg, '='); // --foo if ($eqPos === false) { $key = substr($arg, 2); $value = isset($out[$key]) ? $out[$key] : true; $out[$key] = $value; } else { $key = substr($arg, 2, $eqPos - 2); $value = substr($arg, $eqPos + 1); $out[$key] = $value; } } else { if (substr($arg, 0, 1) == '-') { // -k=value if (substr($arg, 2, 1) == '=') { $key = substr($arg, 1, 1); $value = substr($arg, 3); $out[$key] = $value; } else { $chars = str_split(substr($arg, 1)); foreach ($chars as $char) { $key = $char; $value = isset($out[$key]) ? $out[$key] : true; $out[$key] = $value; } } } else { $value = $arg; $out[] = $value; } } } self::$args = $out; return $out; }
public function __construct($argc, $argv) { // Store argc, argv parent::__construct($argc, $argv); }
/** @return array */ private function loadConfig() { $cmd = new CommandLine(<<<XX FTP deployment v2.6 ------------------- Usage: \tdeployment.php <config_file> [-t | --test] Options: \t-t | --test Run in test-mode. \t--generate Only generates deployment file. \t--no-progress Hide the progress indicators. XX , ['config' => [CommandLine::REALPATH => TRUE]]); if ($cmd->isEmpty()) { $cmd->help(); return; } $options = $cmd->parse(); $this->mode = $options['--generate'] ? 'generate' : ($options['--test'] ? 'test' : NULL); $this->configFile = $options['config']; $config = $this->loadConfigFile($options['config']); if (!$config) { throw new \Exception('Missing config.'); } $this->batches = isset($config['remote']) && is_string($config['remote']) ? ['' => $config] : array_filter($config, 'is_array'); foreach ($this->batches as &$batch) { $batch = array_change_key_case($batch, CASE_LOWER) + $this->defaults; } $config = array_change_key_case($config, CASE_LOWER) + ['log' => preg_replace('#\\.\\w+$#', '.log', $this->configFile), 'tempdir' => sys_get_temp_dir() . '/deployment', 'progress' => TRUE, 'colors' => PHP_SAPI === 'cli' && (function_exists('posix_isatty') && posix_isatty(STDOUT) || getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== FALSE)]; $config['progress'] = $options['--no-progress'] ? FALSE : $config['progress']; return $config; }
protected function checkargs(&$args) { if (!parent::checkargs($args)) { return false; } if (count($args) != 2) { return $this->error(Error::NO_OBJECT, null, null, 'Usage: cp [OPTIONS] SOURCE-URL DEST-URL'); } return true; }
public function forLinux() { $this->assertEquals(CommandLine::$UNIX, CommandLine::forName('Linux')); }
/** * 取得整理過後的 CLI 參數 * @dependency CommandLine class */ public static function getArgs() { return \CommandLine::parseArgs(self::$args); }
<?php require_once 'app/config.php'; require_once 'app/src/Database.php'; require_once 'app/src/CommandLine.php'; $database = new Database($config['database']); $commandLine = new CommandLine($argv); $database->connect(); switch ($argv[1]) { case 'migrate': if (isset($argv[2])) { if ($argv[2] == "rollback") { $database->thisConnection()->query("SET foreign_key_checks = 0"); if ($result = $database->thisConnection()->query("SHOW TABLES")) { while ($row = $result->fetch_array(MYSQLI_NUM)) { $database->thisConnection()->query("DROP TABLE IF EXISTS {$row[0]}"); } } $database->thisConnection()->query("SET foreign_key_checks = 1"); $database->thisConnection()->close(); echo "rollback successful\n"; } else { if ($argv[2] == "seed") { $query = file_get_contents($config['sql_seed']); $database->thisConnection()->multi_query($query); echo "seed successful\n"; echo $database->thisConnection()->error; } } } else { $query = file_get_contents($config['sql_file']);