3 * Brutally chopped and modified from http://pear.php.net/package/Console_Getopts
7 * Copyright (c) 1997-2004 The PHP Group
9 * LICENSE: This source file is subject to the New BSD license that is
10 * available through the world-wide-web at the following URI:
11 * http://www.opensource.org/licenses/bsd-license.php. If you did not receive
12 * a copy of the New BSD License and are unable to obtain it through the web,
13 * please send a note to license@php.net so we can mail you a copy immediately.
16 * @package Console_Getopt
17 * @author Andrei Zmievski <andrei@php.net>
18 * @modified Harry Fuecks hfuecks gmail.com
19 * @modified Tanguy Ortolo <tanguy+dokuwiki@ortolo.eu>
20 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
22 * @link http://pear.php.net/package/Console_Getopt
26 //------------------------------------------------------------------------------
28 * Sets up CLI environment based on SAPI and PHP version
29 * Helps resolve some issues between the CGI and CLI SAPIs
30 * as well is inconsistencies between PHP 4.3+ and older versions
32 if (version_compare(phpversion(), '4.3.0', '<') || php_sapi_name() == 'cgi') {
33 // Handle output buffering
35 ob_implicit_flush(true);
39 ini_set('track_errors', true);
40 ini_set('html_errors', false);
41 ini_set('magic_quotes_runtime', false);
43 // Define stream constants
44 define('STDIN', fopen('php://stdin', 'r'));
45 define('STDOUT', fopen('php://stdout', 'w'));
46 define('STDERR', fopen('php://stderr', 'w'));
48 // Close the streams on script termination
49 register_shutdown_function(
51 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')
55 //------------------------------------------------------------------------------
59 define('DOKU_CLI_OPTS_UNKNOWN_OPT',1); //Unrecognized option
60 define('DOKU_CLI_OPTS_OPT_ARG_REQUIRED',2); //Option requires argument
61 define('DOKU_CLI_OPTS_OPT_ARG_DENIED',3); //Option not allowed argument
62 define('DOKU_CLI_OPTS_OPT_ABIGUOUS',4);//Option abiguous
63 define('DOKU_CLI_OPTS_ARG_READ',5);//Could not read argv
65 //------------------------------------------------------------------------------
67 * Command-line options parsing class.
69 * @author Andrei Zmievski <andrei@php.net>
76 * @see http://www.sitepoint.com/article/php-command-line-1/3
77 * @param string executing file name - this MUST be passed the __FILE__ constant
78 * @param string short options
79 * @param array (optional) long options
80 * @return Doku_Cli_Opts_Container or Doku_Cli_Opts_Error
82 function & getOptions($bin_file, $short_options, $long_options = null) {
83 $args = Doku_Cli_Opts::readPHPArgv();
85 if ( Doku_Cli_Opts::isError($args) ) {
89 // Compatibility between "php extensions.php" and "./extensions.php"
90 if ( realpath($_SERVER['argv'][0]) == $bin_file ) {
91 $options = Doku_Cli_Opts::getOpt($args,$short_options,$long_options);
93 $options = Doku_Cli_Opts::getOpt2($args,$short_options,$long_options);
96 if ( Doku_Cli_Opts::isError($options) ) {
100 $container = new Doku_Cli_Opts_Container($options);
105 * Parses the command-line options.
107 * The first parameter to this function should be the list of command-line
108 * arguments without the leading reference to the running program.
110 * The second parameter is a string of allowed short options. Each of the
111 * option letters can be followed by a colon ':' to specify that the option
112 * requires an argument, or a double colon '::' to specify that the option
113 * takes an optional argument.
115 * The third argument is an optional array of allowed long options. The
116 * leading '--' should not be included in the option name. Options that
117 * require an argument should be followed by '=', and options that take an
118 * option argument should be followed by '=='.
120 * The return value is an array of two elements: the list of parsed
121 * options and the list of non-option command-line arguments. Each entry in
122 * the list of parsed options is a pair of elements - the first one
123 * specifies the option, and the second one specifies the option argument,
126 * Long and short options can be mixed.
128 * Most of the semantics of this function are based on GNU getopt_long().
130 * @param array $args an array of command-line arguments
131 * @param string $short_options specifies the list of allowed short options
132 * @param array $long_options specifies the list of allowed long options
134 * @return array two-element array containing the list of parsed options and
135 * the non-option arguments
138 function getopt2($args, $short_options, $long_options = null) {
139 return Doku_Cli_Opts::doGetopt(
140 2, $args, $short_options, $long_options
145 * This function expects $args to start with the script name (POSIX-style).
146 * Preserved for backwards compatibility.
148 * @param array $args an array of command-line arguments
149 * @param string $short_options specifies the list of allowed short options
150 * @param array $long_options specifies the list of allowed long options
153 * @return array two-element array containing the list of parsed options and
154 * the non-option arguments
156 function getopt($args, $short_options, $long_options = null) {
157 return Doku_Cli_Opts::doGetopt(
158 1, $args, $short_options, $long_options
163 * The actual implementation of the argument parsing code.
165 * @param int $version Version to use
166 * @param array $args an array of command-line arguments
167 * @param string $short_options specifies the list of allowed short options
168 * @param array $long_options specifies the list of allowed long options
172 function doGetopt($version, $args, $short_options, $long_options = null) {
174 // in case you pass directly readPHPArgv() as the first arg
175 if (Doku_Cli_Opts::isError($args)) {
179 return array(array(), array());
184 settype($args, 'array');
186 if ($long_options && is_array($long_options)) {
191 * Preserve backwards compatibility with callers that relied on
192 * erroneous POSIX fix.
195 if (isset($args[0]{0}) && $args[0]{0} != '-') {
201 while (list($i, $arg) = each($args)) {
203 /* The special element '--' means explicit end of
204 options. Treat the rest of the arguments as non-options
207 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
211 if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
212 $non_opts = array_merge($non_opts, array_slice($args, $i));
214 } elseif (strlen($arg) > 1 && $arg{1} == '-') {
215 $error = Doku_Cli_Opts::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
216 if (Doku_Cli_Opts::isError($error))
218 } elseif ($arg == '-') {
220 $non_opts = array_merge($non_opts, array_slice($args, $i));
223 $error = Doku_Cli_Opts::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
224 if (Doku_Cli_Opts::isError($error))
229 return array($opts, $non_opts);
235 * @param string $arg Argument
236 * @param string[] $short_options Available short options
237 * @param string[][] &$opts
238 * @param string[] &$args
243 function _parseShortOption($arg, $short_options, &$opts, &$args) {
245 for ($i = 0; $i < $len; $i++) {
249 /* Try to find the short option in the specifier string. */
250 if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
252 return Doku_Cli_Opts::raiseError(
253 DOKU_CLI_OPTS_UNKNOWN_OPT,
254 "Unrecognized option -- $opt"
258 if (strlen($spec) > 1 && $spec{1} == ':') {
259 if (strlen($spec) > 2 && $spec{2} == ':') {
260 if ($i + 1 < strlen($arg)) {
261 /* Option takes an optional argument. Use the remainder of
262 the arg string if there is anything left. */
263 $opts[] = array($opt, substr($arg, $i + 1));
267 /* Option requires an argument. Use the remainder of the arg
268 string if there is anything left. */
269 if ($i + 1 < strlen($arg)) {
270 $opts[] = array($opt, substr($arg, $i + 1));
272 } else if (list(, $opt_arg) = each($args)) {
273 /* Else use the next argument. */;
274 if (Doku_Cli_Opts::_isShortOpt($opt_arg) || Doku_Cli_Opts::_isLongOpt($opt_arg))
275 return Doku_Cli_Opts::raiseError(
276 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
277 "option requires an argument --$opt"
281 return Doku_Cli_Opts::raiseError(
282 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
283 "Option requires an argument -- $opt"
288 $opts[] = array($opt, $opt_arg);
293 * Checks if an argument is a short option
295 * @param string $arg Argument to check
300 function _isShortOpt($arg){
301 return strlen($arg) == 2 && $arg[0] == '-'
302 && preg_match('/[a-zA-Z]/', $arg[1]);
306 * Checks if an argument is a long option
308 * @param string $arg Argument to check
313 function _isLongOpt($arg){
314 return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
315 preg_match('/[a-zA-Z]+$/', substr($arg, 2));
321 * @param string $arg Argument
322 * @param string[] $long_options Available long options
323 * @param string[][] &$opts
324 * @param string[] &$args
327 * @return void|PEAR_Error
329 function _parseLongOption($arg, $long_options, &$opts, &$args) {
330 @list($opt, $opt_arg) = explode('=', $arg, 2);
331 $opt_len = strlen($opt);
332 $opt_cnt = count($long_options);
334 for ($i = 0; $i < $opt_cnt; $i++) {
335 $long_opt = $long_options[$i];
336 $opt_start = substr($long_opt, 0, $opt_len);
338 $long_opt_name = str_replace('=', '', $long_opt);
340 /* Option doesn't match. Go on to the next one. */
341 if ($opt_start != $opt)
344 $opt_rest = substr($long_opt, $opt_len);
346 /* Check that the options uniquely matches one of the allowed
348 if ($i + 1 < count($long_options)) {
349 $next_option_rest = substr($long_options[$i + 1], $opt_len);
351 $next_option_rest = '';
354 if ($opt_rest != '' && $opt{0} != '=' &&
356 $opt == substr($long_options[$i+1], 0, $opt_len) &&
357 $next_option_rest != '' &&
358 $next_option_rest{0} != '=') {
359 return Doku_Cli_Opts::raiseError(
360 DOKU_CLI_OPTS_OPT_ABIGUOUS,
361 "Option --$opt is ambiguous"
365 if (substr($long_opt, -1) == '=') {
366 if (substr($long_opt, -2) != '==') {
367 /* Long option requires an argument.
368 Take the next argument if one wasn't specified. */;
369 if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
370 return Doku_Cli_Opts::raiseError(
371 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
372 "Option --$opt requires an argument"
376 if (Doku_Cli_Opts::_isShortOpt($opt_arg)
377 || Doku_Cli_Opts::_isLongOpt($opt_arg))
378 return Doku_Cli_Opts::raiseError(
379 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
380 "Option --$opt requires an argument"
383 } else if ($opt_arg) {
384 return Doku_Cli_Opts::raiseError(
385 DOKU_CLI_OPTS_OPT_ARG_DENIED,
386 "Option --$opt doesn't allow an argument"
390 $opts[] = array('--' . $opt, $opt_arg);
394 return Doku_Cli_Opts::raiseError(
395 DOKU_CLI_OPTS_UNKNOWN_OPT,
396 "Unrecognized option --$opt"
401 * Safely read the $argv PHP array across different PHP configurations.
402 * Will take care on register_globals and register_argc_argv ini directives
405 * @return mixed the $argv PHP array or PEAR error if not registered
407 function readPHPArgv() {
409 if (!is_array($argv)) {
410 if (!@is_array($_SERVER['argv'])) {
411 if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
412 return Doku_Cli_Opts::raiseError(
413 DOKU_CLI_OPTS_ARG_READ,
414 "Could not read cmd args (register_argc_argv=Off?)"
417 return $GLOBALS['HTTP_SERVER_VARS']['argv'];
419 return $_SERVER['argv'];
424 function raiseError($code, $msg) {
425 return new Doku_Cli_Opts_Error($code, $msg);
428 function isError($obj) {
429 return is_a($obj, 'Doku_Cli_Opts_Error');
434 //------------------------------------------------------------------------------
435 class Doku_Cli_Opts_Error {
440 function Doku_Cli_Opts_Error($code, $msg) {
445 function getMessage() {
455 //------------------------------------------------------------------------------
456 class Doku_Cli_Opts_Container {
458 var $options = array();
461 function Doku_Cli_Opts_Container($options) {
462 foreach ( $options[0] as $option ) {
463 if ( false !== ( strpos($option[0], '--') ) ) {
464 $opt_name = substr($option[0], 2);
466 $opt_name = $option[0];
468 $this->options[$opt_name] = $option[1];
471 $this->args = $options[1];
474 function has($option) {
475 return array_key_exists($option, $this->options);
478 function get($option) {
479 if ( isset($this->options[$option]) ) {
480 return ( $this->options[$option] ) ;
484 function arg($index) {
485 if ( isset($this->args[$index]) ) {
486 return $this->args[$index];
491 return count($this->args);
495 return count($this->args) !== 0;