French language update
[dokuwiki/radio.git] / inc / cliopts.php
blobede559a63dad67b8ed2999b17f6084146fc9c00b
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 $len = strlen($arg);
172 for ($i = 0; $i < $len; $i++) {
173 $opt = $arg{$i};
174 $opt_arg = null;
176 /* Try to find the short option in the specifier string. */
177 if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
179 return Doku_Cli_Opts::raiseError(
180 DOKU_CLI_OPTS_UNKNOWN_OPT,
181 "Unrecognized option -- $opt"
185 if (strlen($spec) > 1 && $spec{1} == ':') {
186 if (strlen($spec) > 2 && $spec{2} == ':') {
187 if ($i + 1 < strlen($arg)) {
188 /* Option takes an optional argument. Use the remainder of
189 the arg string if there is anything left. */
190 $opts[] = array($opt, substr($arg, $i + 1));
191 break;
193 } else {
194 /* Option requires an argument. Use the remainder of the arg
195 string if there is anything left. */
196 if ($i + 1 < strlen($arg)) {
197 $opts[] = array($opt, substr($arg, $i + 1));
198 break;
199 } else if (list(, $opt_arg) = each($args))
200 /* Else use the next argument. */;
201 else
202 return Doku_Cli_Opts::raiseError(
203 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
204 "Option requires an argument -- $opt"
209 $opts[] = array($opt, $opt_arg);
213 function _parseLongOption($arg, $long_options, &$opts, &$args) {
214 @list($opt, $opt_arg) = explode('=', $arg);
215 $opt_len = strlen($opt);
216 $opt_cnt = count($long_options);
218 for ($i = 0; $i < $opt_cnt; $i++) {
219 $long_opt = $long_options[$i];
220 $opt_start = substr($long_opt, 0, $opt_len);
222 /* Option doesn't match. Go on to the next one. */
223 if ($opt_start != $opt)
224 continue;
226 $opt_rest = substr($long_opt, $opt_len);
228 /* Check that the options uniquely matches one of the allowed
229 options. */
230 if ($opt_rest != '' && $opt{0} != '=' &&
231 $i + 1 < $opt_cnt &&
232 $opt == substr($long_options[$i+1], 0, $opt_len)) {
233 return Doku_Cli_Opts::raiseError(
234 DOKU_CLI_OPTS_OPT_ABIGUOUS,
235 "Option --$opt is ambiguous"
239 if (substr($long_opt, -1) == '=') {
240 if (substr($long_opt, -2) != '==') {
241 /* Long option requires an argument.
242 Take the next argument if one wasn't specified. */;
243 if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
244 return Doku_Cli_Opts::raiseError(
245 DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
246 "Option --$opt requires an argument"
250 } else if ($opt_arg) {
251 return Doku_Cli_Opts::raiseError(
252 DOKU_CLI_OPTS_OPT_ARG_DENIED,
253 "Option --$opt doesn't allow an argument"
257 $opts[] = array('--' . $opt, $opt_arg);
258 return;
261 return Doku_Cli_Opts::raiseError(
262 DOKU_CLI_OPTS_UNKNOWN_OPT,
263 "Unrecognized option --$opt"
267 function readPHPArgv() {
268 global $argv;
269 if (!is_array($argv)) {
270 if (!@is_array($_SERVER['argv'])) {
271 if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
272 return Doku_Cli_Opts::raiseError(
273 DOKU_CLI_OPTS_ARG_READ,
274 "Could not read cmd args (register_argc_argv=Off?)"
277 return $GLOBALS['HTTP_SERVER_VARS']['argv'];
279 return $_SERVER['argv'];
281 return $argv;
284 function raiseError($code, $msg) {
285 return new Doku_Cli_Opts_Error($code, $msg);
288 function isError($obj) {
289 return is_a($obj, 'Doku_Cli_Opts_Error');
294 //------------------------------------------------------------------------------
295 class Doku_Cli_Opts_Error {
297 var $code;
298 var $msg;
300 function Doku_Cli_Opts_Error($code, $msg) {
301 $this->code = $code;
302 $this->msg = $msg;
305 function getMessage() {
306 return $this->msg;
309 function isError() {
310 return true;
315 //------------------------------------------------------------------------------
316 class Doku_Cli_Opts_Container {
318 var $options = array();
319 var $args = array();
321 function Doku_Cli_Opts_Container($options) {
322 foreach ( $options[0] as $option ) {
323 if ( false !== ( strpos($option[0], '--') ) ) {
324 $opt_name = substr($option[0], 2);
325 } else {
326 $opt_name = $option[0];
328 $this->options[$opt_name] = $option[1];
331 $this->args = $options[1];
334 function has($option) {
335 return array_key_exists($option, $this->options);
338 function get($option) {
339 if ( isset($this->options[$option]) ) {
340 return ( $this->options[$option] ) ;
344 function arg($index) {
345 if ( isset($this->args[$index]) ) {
346 return $this->args[$index];
350 function numArgs() {
351 return count($this->args);
354 function hasArgs() {
355 return count($this->args) !== 0;
358 function isError() {
359 return false;