21 require_once
'PEAR.php';
69 public static function getopt2($args, $short_options, $long_options =
null, $skip_unknown =
false)
86 public static function getopt($args, $short_options, $long_options =
null, $skip_unknown =
false)
102 public static function doGetopt($version, $args, $short_options, $long_options =
null, $skip_unknown =
false)
110 return array(array(), array());
113 $non_opts = $opts = array();
115 settype($args,
'array');
126 if (isset($args[0][0]) && $args[0][0] !=
'-') {
131 for ($i = 0; $i < count($args); $i++) {
137 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
141 if ($arg[0] !=
'-' || (strlen($arg) > 1 && $arg[1] ==
'-' && !$long_options)) {
142 $non_opts = array_merge($non_opts, array_slice($args, $i));
144 } elseif (strlen($arg) > 1 && $arg[1] ==
'-') {
154 } elseif ($arg ==
'-') {
156 $non_opts = array_merge($non_opts, array_slice($args, $i));
171 return array($opts, $non_opts);
186 protected static function _parseShortOption($arg, $short_options, &$opts, &$argIdx, $args, $skip_unknown)
188 for ($i = 0; $i < strlen($arg); $i++) {
193 if (($spec = strstr($short_options, $opt)) ===
false || $arg[$i] ==
':') {
194 if ($skip_unknown ===
true) {
198 $msg =
"Console_Getopt: unrecognized option -- $opt";
199 return PEAR::raiseError($msg);
202 if (strlen($spec) > 1 && $spec[1] ==
':') {
203 if (strlen($spec) > 2 && $spec[2] ==
':') {
204 if ($i + 1 < strlen($arg)) {
207 $opts[] = array($opt, substr($arg, $i + 1));
213 if ($i + 1 < strlen($arg)) {
214 $opts[] = array($opt, substr($arg, $i + 1));
216 }
else if (isset($args[++$argIdx])) {
217 $opt_arg = $args[$argIdx];
221 $msg =
"option requires an argument --$opt";
222 return PEAR::raiseError(
"Console_Getopt: " . $msg);
225 $msg =
"option requires an argument --$opt";
226 return PEAR::raiseError(
"Console_Getopt: " . $msg);
231 $opts[] = array($opt, $opt_arg);
244 return strlen($arg) == 2 && $arg[0] ==
'-'
245 && preg_match(
'/[a-zA-Z]/', $arg[1]);
257 return strlen($arg) > 2 && $arg[0] ==
'-' && $arg[1] ==
'-' &&
258 preg_match(
'/[a-zA-Z]+$/', substr($arg, 2));
272 protected static function _parseLongOption($arg, $long_options, &$opts, &$argIdx, $args, $skip_unknown)
274 @list($opt, $opt_arg) = explode(
'=', $arg, 2);
276 $opt_len = strlen($opt);
278 for ($i = 0; $i < count($long_options); $i++) {
279 $long_opt = $long_options[$i];
280 $opt_start = substr($long_opt, 0, $opt_len);
282 $long_opt_name = str_replace(
'=',
'', $long_opt);
285 if ($long_opt_name != $opt) {
289 $opt_rest = substr($long_opt, $opt_len);
293 if ($i + 1 < count($long_options)) {
294 $next_option_rest = substr($long_options[$i + 1], $opt_len);
296 $next_option_rest =
'';
299 if ($opt_rest !=
'' && $opt[0] !=
'=' &&
300 $i + 1 < count($long_options) &&
301 $opt == substr($long_options[$i+1], 0, $opt_len) &&
302 $next_option_rest !=
'' &&
303 $next_option_rest[0] !=
'=') {
305 $msg =
"Console_Getopt: option --$opt is ambiguous";
306 return PEAR::raiseError($msg);
309 if (substr($long_opt, -1) ==
'=') {
310 if (substr($long_opt, -2) !=
'==') {
313 if (!strlen($opt_arg)) {
314 if (!isset($args[++$argIdx])) {
315 $msg =
"Console_Getopt: option requires an argument --$opt";
316 return PEAR::raiseError($msg);
318 $opt_arg = $args[$argIdx];
323 $msg =
"Console_Getopt: option requires an argument --$opt";
324 return PEAR::raiseError($msg);
327 }
else if ($opt_arg) {
328 $msg =
"Console_Getopt: option --$opt doesn't allow an argument";
329 return PEAR::raiseError($msg);
332 $opts[] = array(
'--' . $opt, $opt_arg);
336 if ($skip_unknown ===
true) {
340 return PEAR::raiseError(
"Console_Getopt: unrecognized option --$opt");
352 if (!is_array($argv)) {
353 if (!@is_array($_SERVER[
'argv'])) {
354 if (!@is_array(
$GLOBALS[
'HTTP_SERVER_VARS'][
'argv'])) {
355 $msg =
"Could not read cmd args (register_argc_argv=Off?)";
356 return PEAR::raiseError(
"Console_Getopt: " . $msg);
358 return $GLOBALS[
'HTTP_SERVER_VARS'][
'argv'];
360 return $_SERVER[
'argv'];