24 require_once
'PEAR.php';
73 function getopt2($args, $short_options, $long_options = null, $skip_unknown =
false)
90 function getopt($args, $short_options, $long_options = null, $skip_unknown =
false)
106 function doGetopt($version, $args, $short_options, $long_options = null, $skip_unknown =
false)
114 return array(array(), array());
117 $non_opts = $opts = array();
119 settype($args,
'array');
130 if (isset($args[0]{0}) && $args[0]{0} !=
'-') {
136 while (list($i, $arg) = each($args)) {
141 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
145 if ($arg{0} !=
'-' || (strlen($arg) > 1 && $arg{1} ==
'-' && !$long_options)) {
146 $non_opts = array_merge($non_opts, array_slice($args, $i));
148 } elseif (strlen($arg) > 1 && $arg{1} ==
'-') {
157 } elseif ($arg ==
'-') {
159 $non_opts = array_merge($non_opts, array_slice($args, $i));
173 return array($opts, $non_opts);
190 for ($i = 0; $i < strlen($arg); $i++) {
195 if (($spec = strstr($short_options, $opt)) ===
false || $arg{$i} ==
':') {
196 if ($skip_unknown ===
true) {
200 $msg =
"Console_Getopt: unrecognized option -- $opt";
204 if (strlen($spec) > 1 && $spec{1} ==
':') {
205 if (strlen($spec) > 2 && $spec{2} ==
':') {
206 if ($i + 1 < strlen($arg)) {
209 $opts[] = array($opt, substr($arg, $i + 1));
215 if ($i + 1 < strlen($arg)) {
216 $opts[] = array($opt, substr($arg, $i + 1));
218 }
else if (list(, $opt_arg) = each($args)) {
222 $msg =
"option requires an argument --$opt";
226 $msg =
"option requires an argument --$opt";
232 $opts[] = array($opt, $opt_arg);
246 return strlen($arg) == 2 && $arg[0] ==
'-'
247 && preg_match(
'/[a-zA-Z]/', $arg[1]);
260 return strlen($arg) > 2 && $arg[0] ==
'-' && $arg[1] ==
'-' &&
261 preg_match(
'/[a-zA-Z]+$/', substr($arg, 2));
277 @list($opt, $opt_arg) = explode(
'=', $arg, 2);
279 $opt_len = strlen($opt);
281 for ($i = 0; $i < count($long_options); $i++) {
282 $long_opt = $long_options[$i];
283 $opt_start = substr($long_opt, 0, $opt_len);
285 $long_opt_name = str_replace(
'=',
'', $long_opt);
288 if ($long_opt_name != $opt) {
292 $opt_rest = substr($long_opt, $opt_len);
296 if ($i + 1 < count($long_options)) {
297 $next_option_rest = substr($long_options[$i + 1], $opt_len);
299 $next_option_rest =
'';
302 if ($opt_rest !=
'' && $opt{0} !=
'=' &&
303 $i + 1 < count($long_options) &&
304 $opt == substr($long_options[$i+1], 0, $opt_len) &&
305 $next_option_rest !=
'' &&
306 $next_option_rest{0} !=
'=') {
308 $msg =
"Console_Getopt: option --$opt is ambiguous";
312 if (substr($long_opt, -1) ==
'=') {
313 if (substr($long_opt, -2) !=
'==') {
316 if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
317 $msg =
"Console_Getopt: option requires an argument --$opt";
323 $msg =
"Console_Getopt: option requires an argument --$opt";
327 }
else if ($opt_arg) {
328 $msg =
"Console_Getopt: option --$opt doesn't allow an argument";
332 $opts[] = array(
'--' . $opt, $opt_arg);
336 if ($skip_unknown ===
true) {
353 if (!is_array($argv)) {
354 if (!@is_array($_SERVER[
'argv'])) {
355 if (!@is_array(
$GLOBALS[
'HTTP_SERVER_VARS'][
'argv'])) {
356 $msg =
"Could not read cmd args (register_argc_argv=Off?)";
359 return $GLOBALS[
'HTTP_SERVER_VARS'][
'argv'];
361 return $_SERVER[
'argv'];