Getopt.php
319 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Matomo\Dependencies; |
| 4 | |
| 5 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
| 6 | /** |
| 7 | * PHP Version 5 |
| 8 | * |
| 9 | * Copyright (c) 2001-2015, The PEAR developers |
| 10 | * |
| 11 | * This source file is subject to the BSD-2-Clause license, |
| 12 | * that is bundled with this package in the file LICENSE, and is |
| 13 | * available through the world-wide-web at the following url: |
| 14 | * http://opensource.org/licenses/bsd-license.php. |
| 15 | * |
| 16 | * @category Console |
| 17 | * @package Console_Getopt |
| 18 | * @author Andrei Zmievski <andrei@php.net> |
| 19 | * @license http://opensource.org/licenses/bsd-license.php BSD-2-Clause |
| 20 | * @version CVS: $Id$ |
| 21 | * @link http://pear.php.net/package/Console_Getopt |
| 22 | */ |
| 23 | require_once 'PEAR.php'; |
| 24 | /** |
| 25 | * Command-line options parsing class. |
| 26 | * |
| 27 | * @category Console |
| 28 | * @package Console_Getopt |
| 29 | * @author Andrei Zmievski <andrei@php.net> |
| 30 | * @license http://opensource.org/licenses/bsd-license.php BSD-2-Clause |
| 31 | * @link http://pear.php.net/package/Console_Getopt |
| 32 | */ |
| 33 | class Console_Getopt |
| 34 | { |
| 35 | /** |
| 36 | * Parses the command-line options. |
| 37 | * |
| 38 | * The first parameter to this function should be the list of command-line |
| 39 | * arguments without the leading reference to the running program. |
| 40 | * |
| 41 | * The second parameter is a string of allowed short options. Each of the |
| 42 | * option letters can be followed by a colon ':' to specify that the option |
| 43 | * requires an argument, or a double colon '::' to specify that the option |
| 44 | * takes an optional argument. |
| 45 | * |
| 46 | * The third argument is an optional array of allowed long options. The |
| 47 | * leading '--' should not be included in the option name. Options that |
| 48 | * require an argument should be followed by '=', and options that take an |
| 49 | * option argument should be followed by '=='. |
| 50 | * |
| 51 | * The return value is an array of two elements: the list of parsed |
| 52 | * options and the list of non-option command-line arguments. Each entry in |
| 53 | * the list of parsed options is a pair of elements - the first one |
| 54 | * specifies the option, and the second one specifies the option argument, |
| 55 | * if there was one. |
| 56 | * |
| 57 | * Long and short options can be mixed. |
| 58 | * |
| 59 | * Most of the semantics of this function are based on GNU getopt_long(). |
| 60 | * |
| 61 | * @param array $args an array of command-line arguments |
| 62 | * @param string $short_options specifies the list of allowed short options |
| 63 | * @param array $long_options specifies the list of allowed long options |
| 64 | * @param boolean $skip_unknown suppresses Console_Getopt: unrecognized option |
| 65 | * |
| 66 | * @return array two-element array containing the list of parsed options and |
| 67 | * the non-option arguments |
| 68 | */ |
| 69 | public static function getopt2($args, $short_options, $long_options = null, $skip_unknown = \false) |
| 70 | { |
| 71 | return Console_Getopt::doGetopt(2, $args, $short_options, $long_options, $skip_unknown); |
| 72 | } |
| 73 | /** |
| 74 | * This function expects $args to start with the script name (POSIX-style). |
| 75 | * Preserved for backwards compatibility. |
| 76 | * |
| 77 | * @param array $args an array of command-line arguments |
| 78 | * @param string $short_options specifies the list of allowed short options |
| 79 | * @param array $long_options specifies the list of allowed long options |
| 80 | * |
| 81 | * @see getopt2() |
| 82 | * @return array two-element array containing the list of parsed options and |
| 83 | * the non-option arguments |
| 84 | */ |
| 85 | public static function getopt($args, $short_options, $long_options = null, $skip_unknown = \false) |
| 86 | { |
| 87 | return Console_Getopt::doGetopt(1, $args, $short_options, $long_options, $skip_unknown); |
| 88 | } |
| 89 | /** |
| 90 | * The actual implementation of the argument parsing code. |
| 91 | * |
| 92 | * @param int $version Version to use |
| 93 | * @param array $args an array of command-line arguments |
| 94 | * @param string $short_options specifies the list of allowed short options |
| 95 | * @param array $long_options specifies the list of allowed long options |
| 96 | * @param boolean $skip_unknown suppresses Console_Getopt: unrecognized option |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public static function doGetopt($version, $args, $short_options, $long_options = null, $skip_unknown = \false) |
| 101 | { |
| 102 | // in case you pass directly readPHPArgv() as the first arg |
| 103 | if (PEAR::isError($args)) { |
| 104 | return $args; |
| 105 | } |
| 106 | if (empty($args)) { |
| 107 | return array(array(), array()); |
| 108 | } |
| 109 | $non_opts = $opts = array(); |
| 110 | \settype($args, 'array'); |
| 111 | if ($long_options) { |
| 112 | \sort($long_options); |
| 113 | } |
| 114 | /* |
| 115 | * Preserve backwards compatibility with callers that relied on |
| 116 | * erroneous POSIX fix. |
| 117 | */ |
| 118 | if ($version < 2) { |
| 119 | if (isset($args[0][0]) && $args[0][0] != '-') { |
| 120 | \array_shift($args); |
| 121 | } |
| 122 | } |
| 123 | for ($i = 0; $i < \count($args); $i++) { |
| 124 | $arg = $args[$i]; |
| 125 | /* The special element '--' means explicit end of |
| 126 | options. Treat the rest of the arguments as non-options |
| 127 | and end the loop. */ |
| 128 | if ($arg == '--') { |
| 129 | $non_opts = \array_merge($non_opts, \array_slice($args, $i + 1)); |
| 130 | break; |
| 131 | } |
| 132 | if ($arg[0] != '-' || \strlen($arg) > 1 && $arg[1] == '-' && !$long_options) { |
| 133 | $non_opts = \array_merge($non_opts, \array_slice($args, $i)); |
| 134 | break; |
| 135 | } elseif (\strlen($arg) > 1 && $arg[1] == '-') { |
| 136 | $error = Console_Getopt::_parseLongOption(\substr($arg, 2), $long_options, $opts, $i, $args, $skip_unknown); |
| 137 | if (PEAR::isError($error)) { |
| 138 | return $error; |
| 139 | } |
| 140 | } elseif ($arg == '-') { |
| 141 | // - is stdin |
| 142 | $non_opts = \array_merge($non_opts, \array_slice($args, $i)); |
| 143 | break; |
| 144 | } else { |
| 145 | $error = Console_Getopt::_parseShortOption(\substr($arg, 1), $short_options, $opts, $i, $args, $skip_unknown); |
| 146 | if (PEAR::isError($error)) { |
| 147 | return $error; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return array($opts, $non_opts); |
| 152 | } |
| 153 | /** |
| 154 | * Parse short option |
| 155 | * |
| 156 | * @param string $arg Argument |
| 157 | * @param string[] $short_options Available short options |
| 158 | * @param string[][] &$opts |
| 159 | * @param int &$argIdx |
| 160 | * @param string[] $args |
| 161 | * @param boolean $skip_unknown suppresses Console_Getopt: unrecognized option |
| 162 | * |
| 163 | * @return void |
| 164 | */ |
| 165 | protected static function _parseShortOption($arg, $short_options, &$opts, &$argIdx, $args, $skip_unknown) |
| 166 | { |
| 167 | for ($i = 0; $i < \strlen($arg); $i++) { |
| 168 | $opt = $arg[$i]; |
| 169 | $opt_arg = null; |
| 170 | /* Try to find the short option in the specifier string. */ |
| 171 | if (($spec = \strstr($short_options, $opt)) === \false || $arg[$i] == ':') { |
| 172 | if ($skip_unknown === \true) { |
| 173 | break; |
| 174 | } |
| 175 | $msg = "Console_Getopt: unrecognized option -- {$opt}"; |
| 176 | return PEAR::raiseError($msg); |
| 177 | } |
| 178 | if (\strlen($spec) > 1 && $spec[1] == ':') { |
| 179 | if (\strlen($spec) > 2 && $spec[2] == ':') { |
| 180 | if ($i + 1 < \strlen($arg)) { |
| 181 | /* Option takes an optional argument. Use the remainder of |
| 182 | the arg string if there is anything left. */ |
| 183 | $opts[] = array($opt, \substr($arg, $i + 1)); |
| 184 | break; |
| 185 | } |
| 186 | } else { |
| 187 | /* Option requires an argument. Use the remainder of the arg |
| 188 | string if there is anything left. */ |
| 189 | if ($i + 1 < \strlen($arg)) { |
| 190 | $opts[] = array($opt, \substr($arg, $i + 1)); |
| 191 | break; |
| 192 | } else { |
| 193 | if (isset($args[++$argIdx])) { |
| 194 | $opt_arg = $args[$argIdx]; |
| 195 | /* Else use the next argument. */ |
| 196 | if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) { |
| 197 | $msg = "option requires an argument --{$opt}"; |
| 198 | return PEAR::raiseError("Console_Getopt: " . $msg); |
| 199 | } |
| 200 | } else { |
| 201 | $msg = "option requires an argument --{$opt}"; |
| 202 | return PEAR::raiseError("Console_Getopt: " . $msg); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | $opts[] = array($opt, $opt_arg); |
| 208 | } |
| 209 | } |
| 210 | /** |
| 211 | * Checks if an argument is a short option |
| 212 | * |
| 213 | * @param string $arg Argument to check |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | protected static function _isShortOpt($arg) |
| 218 | { |
| 219 | return \strlen($arg) == 2 && $arg[0] == '-' && \preg_match('/[a-zA-Z]/', $arg[1]); |
| 220 | } |
| 221 | /** |
| 222 | * Checks if an argument is a long option |
| 223 | * |
| 224 | * @param string $arg Argument to check |
| 225 | * |
| 226 | * @return bool |
| 227 | */ |
| 228 | protected static function _isLongOpt($arg) |
| 229 | { |
| 230 | return \strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' && \preg_match('/[a-zA-Z]+$/', \substr($arg, 2)); |
| 231 | } |
| 232 | /** |
| 233 | * Parse long option |
| 234 | * |
| 235 | * @param string $arg Argument |
| 236 | * @param string[] $long_options Available long options |
| 237 | * @param string[][] &$opts |
| 238 | * @param int &$argIdx |
| 239 | * @param string[] $args |
| 240 | * |
| 241 | * @return void|PEAR_Error |
| 242 | */ |
| 243 | protected static function _parseLongOption($arg, $long_options, &$opts, &$argIdx, $args, $skip_unknown) |
| 244 | { |
| 245 | @(list($opt, $opt_arg) = \explode('=', $arg, 2)); |
| 246 | $opt_len = \strlen($opt); |
| 247 | for ($i = 0; $i < \count($long_options); $i++) { |
| 248 | $long_opt = $long_options[$i]; |
| 249 | $opt_start = \substr($long_opt, 0, $opt_len); |
| 250 | $long_opt_name = \str_replace('=', '', $long_opt); |
| 251 | /* Option doesn't match. Go on to the next one. */ |
| 252 | if ($long_opt_name != $opt) { |
| 253 | continue; |
| 254 | } |
| 255 | $opt_rest = \substr($long_opt, $opt_len); |
| 256 | /* Check that the options uniquely matches one of the allowed |
| 257 | options. */ |
| 258 | if ($i + 1 < \count($long_options)) { |
| 259 | $next_option_rest = \substr($long_options[$i + 1], $opt_len); |
| 260 | } else { |
| 261 | $next_option_rest = ''; |
| 262 | } |
| 263 | if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < \count($long_options) && $opt == \substr($long_options[$i + 1], 0, $opt_len) && $next_option_rest != '' && $next_option_rest[0] != '=') { |
| 264 | $msg = "Console_Getopt: option --{$opt} is ambiguous"; |
| 265 | return PEAR::raiseError($msg); |
| 266 | } |
| 267 | if (\substr($long_opt, -1) == '=') { |
| 268 | if (\substr($long_opt, -2) != '==') { |
| 269 | /* Long option requires an argument. |
| 270 | Take the next argument if one wasn't specified. */ |
| 271 | if (!\strlen($opt_arg)) { |
| 272 | if (!isset($args[++$argIdx])) { |
| 273 | $msg = "Console_Getopt: option requires an argument --{$opt}"; |
| 274 | return PEAR::raiseError($msg); |
| 275 | } |
| 276 | $opt_arg = $args[$argIdx]; |
| 277 | } |
| 278 | if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) { |
| 279 | $msg = "Console_Getopt: option requires an argument --{$opt}"; |
| 280 | return PEAR::raiseError($msg); |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | if ($opt_arg) { |
| 285 | $msg = "Console_Getopt: option --{$opt} doesn't allow an argument"; |
| 286 | return PEAR::raiseError($msg); |
| 287 | } |
| 288 | } |
| 289 | $opts[] = array('--' . $opt, $opt_arg); |
| 290 | return; |
| 291 | } |
| 292 | if ($skip_unknown === \true) { |
| 293 | return; |
| 294 | } |
| 295 | return PEAR::raiseError("Console_Getopt: unrecognized option --{$opt}"); |
| 296 | } |
| 297 | /** |
| 298 | * Safely read the $argv PHP array across different PHP configurations. |
| 299 | * Will take care on register_globals and register_argc_argv ini directives |
| 300 | * |
| 301 | * @return mixed the $argv PHP array or PEAR error if not registered |
| 302 | */ |
| 303 | public static function readPHPArgv() |
| 304 | { |
| 305 | global $argv; |
| 306 | if (!\is_array($argv)) { |
| 307 | if (!@\is_array($_SERVER['argv'])) { |
| 308 | if (!@\is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) { |
| 309 | $msg = "Could not read cmd args (register_argc_argv=Off?)"; |
| 310 | return PEAR::raiseError("Console_Getopt: " . $msg); |
| 311 | } |
| 312 | return $GLOBALS['HTTP_SERVER_VARS']['argv']; |
| 313 | } |
| 314 | return $_SERVER['argv']; |
| 315 | } |
| 316 | return $argv; |
| 317 | } |
| 318 | } |
| 319 |