diff view puts revision back into $REV
[dokuwiki/radio.git] / inc / cliopts.php
bloba3698ab249536bc37be8eb7d08914bb35824a2bc
1 <?php
2 /**
3 * Brutally chopped and modified from http://pear.php.net/package/Console_Getopts
4 */
5 // +----------------------------------------------------------------------+
6 // | PHP Version 4 |
7 // +----------------------------------------------------------------------+
8 // | Copyright (c) 1997-2003 The PHP Group |
9 // +----------------------------------------------------------------------+
10 // | This source file is subject to version 3.0 of the PHP license, |
11 // | that is bundled with this package in the file LICENSE, and is |
12 // | available through the world-wide-web at the following url: |
13 // | http://www.php.net/license/3_0.txt. |
14 // | If you did not receive a copy of the PHP license and are unable to |
15 // | obtain it through the world-wide-web, please send a note to |
16 // | license@php.net so we can mail you a copy immediately. |
17 // +----------------------------------------------------------------------+
18 // | Author: Andrei Zmievski <andrei@php.net> |
19 // | Modified: Harry Fuecks hfuecks gmail.com |
20 // +----------------------------------------------------------------------+
24 //------------------------------------------------------------------------------
25 /**
26 * Sets up CLI environment based on SAPI and PHP version
27 * Helps resolve some issues between the CGI and CLI SAPIs
28 * as well is inconsistencies between PHP 4.3+ and older versions
30 if (version_compare(phpversion(), '4.3.0', '<') || php_sapi_name() == 'cgi') {
31 // Handle output buffering
32 @ob_end_flush();
33 ob_implicit_flush(true);
35 // PHP ini settings
36 set_time_limit(0);
37 ini_set('track_errors', true);
38 ini_set('html_errors', false);
39 ini_set('magic_quotes_runtime', false);
41 // Define stream constants
42 define('STDIN', fopen('php://stdin', 'r'));
43 define('STDOUT', fopen('php://stdout', 'w'));
44 define('STDERR', fopen('php://stderr', 'w'));
46 // Close the streams on script termination
47 register_shutdown_function(
48 create_function('',
49 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')
53 //------------------------------------------------------------------------------
54 /**
55 * Error codes
57 define('DOKU_CLI_OPTS_UNKNOWN_OPT',1); //Unrecognized option
58 define('DOKU_CLI_OPTS_OPT_ARG_REQUIRED',2); //Option requires argument
59 define('DOKU_CLI_OPTS_OPT_ARG_DENIED',3); //Option not allowed argument
60 define('DOKU_CLI_OPTS_OPT_ABIGUOUS',4);//Option abiguous
61 define('DOKU_CLI_OPTS_ARG_READ',5);//Could not read argv
63 //------------------------------------------------------------------------------
64 /**
65 * Command-line options parsing class.
67 * @author Andrei Zmievski <andrei@php.net>
70 class Doku_Cli_Opts {
72 /**
73 * <?php ?>
74 * @see http://www.sitepoint.com/article/php-command-line-1/3
75 * @param string executing file name - this MUST be passed the __FILE__ constant
76 * @param string short options
77 * @param array (optional) long options
78 * @return Doku_Cli_Opts_Container or Doku_Cli_Opts_Error
80 function & getOptions($bin_file, $short_options, $long_options = null) {
81 $args = Doku_Cli_Opts::readPHPArgv();
83 if ( Doku_Cli_Opts::isError($args) ) {
84 return $args;
87 // Compatibility between "php extensions.php" and "./extensions.php"
88 if ( realpath($_SERVER['argv'][0]) == $bin_file ) {
89 $options = Doku_Cli_Opts::getOpt($args,$short_options,$long_options);
90 } else {
91 $options = Doku_Cli_Opts::getOpt2($args,$short_options,$long_options);
94 if ( Doku_Cli_Opts::isError($options) ) {
95 return $options;
98 $container = new Doku_Cli_Opts_Container($options);
99 return $container;
102 function getopt2($args, $short_options, $long_options = null) {
103 return Doku_Cli_Opts::doGetopt(
104 2, $args, $short_options, $long_options
108 function getopt($args, $short_options, $long_options = null) {
109 return Doku_Cli_Opts::doGetopt(
110 1, $args, $short_options, $long_options
114 function doGetopt($version, $args, $short_options, $long_options = null) {
116 // in case you pass directly readPHPArgv() as the first arg
117 if (Doku_Cli_Opts::isError($args)) {
118 return $args;
120 if (empty($args)) {
121 return array(array(), array());
123 $opts = array();
124 $non_opts = array();
126 settype($args, 'array');
128 if ($long_options && is_array($long_options)) {
129 sort($long_options);
133 * Preserve backwards compatibility with callers that relied on
134 * erroneous POSIX fix.
136 if ($version < 2) {
137 if (isset($args[0]{0}) && $args[0]{0} != '-') {
138 array_shift($args);
142 reset($args);
143 while (list($i, $arg) = each($args)) {
145 /* The special element '--' means explicit end of
146 options. Treat the rest of the arguments as non-options
147 and end the loop. */
148 if ($arg == '--') {
149 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
150 break;
153 if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
154 $non_opts = array_merge($non_opts, array_slice($args, $i));
155 break;
156 } elseif (strlen($arg) > 1 && $arg{1} == '-') {
157 $error = Doku_Cli_Opts::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
158 if (Doku_Cli_Opts::isError($error))
159 return $error;
160 } else {
161 $error = Doku_Cli_Opts::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
162 if (Doku_Cli_Opts::isError($error))
163 return $error;
167 return array($opts, $non_opts);
170 function _parseShortOption($arg, $short_options, &$opts, &$args) {
171 for ($i = 0; $i < strlen($arg); $i++) {
172 $opt = $arg{$i};
173 $opt_arg = null;
175 /* Try to find the short option in the specifier string. */
176 if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
178 return Doku_Cli_Opts::raiseError(
179 DOKU_CLI_OPTS_UNKNOWN_OPT,
180 "Unrecognized option -- $opt"
184 if (strlen($spec) > 1 && $spec{1} == ':') {
185 if (strlen($spec) > 2 && $spec{2} == ':') {
186 if ($i + 1 < strlen($arg)) {
187 /* Option takes an optional argument. Use the remainder of
188 the arg string if there is anything left. */
189 $opts[] = array($opt, substr($arg, $i + 1));
190 break;
192 } else {
193 /* Option requires an argument. Use the remainder of the arg
194 string if there is anything left. */
195 if ($i + 1 < strlen($arg)) {
196 $opts[] = array($opt, substr($arg, $i + 1));
197 break;
198 } else if (list(, $opt_arg) = each($args))
199 /* Else use the next argument. */;
200 else
201 return Doku_Cli_Opts::raiseError(
202 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
203 "Option requires an argument -- $opt"
208 $opts[] = array($opt, $opt_arg);
212 function _parseLongOption($arg, $long_options, &$opts, &$args) {
213 @list($opt, $opt_arg) = explode('=', $arg);
214 $opt_len = strlen($opt);
216 for ($i = 0; $i < count($long_options); $i++) {
217 $long_opt = $long_options[$i];
218 $opt_start = substr($long_opt, 0, $opt_len);
220 /* Option doesn't match. Go on to the next one. */
221 if ($opt_start != $opt)
222 continue;
224 $opt_rest = substr($long_opt, $opt_len);
226 /* Check that the options uniquely matches one of the allowed
227 options. */
228 if ($opt_rest != '' && $opt{0} != '=' &&
229 $i + 1 < count($long_options) &&
230 $opt == substr($long_options[$i+1], 0, $opt_len)) {
231 return Doku_Cli_Opts::raiseError(
232 DOKU_CLI_OPTS_OPT_ABIGUOUS,
233 "Option --$opt is ambiguous"
237 if (substr($long_opt, -1) == '=') {
238 if (substr($long_opt, -2) != '==') {
239 /* Long option requires an argument.
240 Take the next argument if one wasn't specified. */;
241 if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
242 return Doku_Cli_Opts::raiseError(
243 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
244 "Option --$opt requires an argument"
248 } else if ($opt_arg) {
249 return Doku_Cli_Opts::raiseError(
250 DOKU_CLI_OPTS_OPT_ARG_DENIED,
251 "Option --$opt doesn't allow an argument"
255 $opts[] = array('--' . $opt, $opt_arg);
256 return;
259 return Doku_Cli_Opts::raiseError(
260 DOKU_CLI_OPTS_UNKNOWN_OPT,
261 "Unrecognized option --$opt"
265 function readPHPArgv() {
266 global $argv;
267 if (!is_array($argv)) {
268 if (!@is_array($_SERVER['argv'])) {
269 if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
270 return Doku_Cli_Opts::raiseError(
271 DOKU_CLI_OPTS_ARG_READ,
272 "Could not read cmd args (register_argc_argv=Off?)"
275 return $GLOBALS['HTTP_SERVER_VARS']['argv'];
277 return $_SERVER['argv'];
279 return $argv;
282 function raiseError($code, $msg) {
283 return new Doku_Cli_Opts_Error($code, $msg);
286 function isError($obj) {
287 return is_a($obj, 'Doku_Cli_Opts_Error');
292 //------------------------------------------------------------------------------
293 class Doku_Cli_Opts_Error {
295 var $code;
296 var $msg;
298 function Doku_Cli_Opts_Error($code, $msg) {
299 $this->code = $code;
300 $this->msg = $msg;
303 function getMessage() {
304 return $this->msg;
307 function isError() {
308 return true;
313 //------------------------------------------------------------------------------
314 class Doku_Cli_Opts_Container {
316 var $options = array();
317 var $args = array();
319 function Doku_Cli_Opts_Container($options) {
320 foreach ( $options[0] as $option ) {
321 if ( false !== ( strpos($option[0], '--') ) ) {
322 $opt_name = substr($option[0], 2);
323 } else {
324 $opt_name = $option[0];
326 $this->options[$opt_name] = $option[1];
330 $this->args = $options[1];
333 function has($option) {
334 return array_key_exists($option, $this->options);
337 function get($option) {
338 if ( isset($this->options[$option]) ) {
339 return ( $this->options[$option] ) ;
343 function arg($index) {
344 if ( isset($this->args[$index]) ) {
345 return $this->args[$index];
349 function numArgs() {
350 return count($this->args);
353 function hasArgs() {
354 return count($this->args) !== 0;
357 function isError() {
358 return false;