/** * @access private * */ function _parseShortOption($arg, $short_options, &$opts, &$args) { for ($i = 0; $i < strlen($arg); $i++) { $opt = $arg[$i]; $opt_arg = null; /* Try to find the short option in the specifier string. */ if (($spec = strstr($short_options, $opt)) === false || $arg[$i] == ':') { return PEAR::raiseError("Console_Getopt: unrecognized option -- {$opt}"); } if (strlen($spec) > 1 && $spec[1] == ':') { if (strlen($spec) > 2 && $spec[2] == ':') { if ($i + 1 < strlen($arg)) { /* Option takes an optional argument. Use the remainder of the arg string if there is anything left. */ $opts[] = array($opt, substr($arg, $i + 1)); break; } } else { /* Option requires an argument. Use the remainder of the arg string if there is anything left. */ if ($i + 1 < strlen($arg)) { $opts[] = array($opt, substr($arg, $i + 1)); break; } else { if (list(, $opt_arg) = each($args)) { /* Else use the next argument. */ if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) { return PEAR::raiseError("Console_Getopt: option requires an argument -- {$opt}"); } } else { return PEAR::raiseError("Console_Getopt: option requires an argument -- {$opt}"); } } } } $opts[] = array($opt, $opt_arg); } }
/** * @access private * */ function _parseLongOption($arg, $long_options, &$opts, &$args) { @(list($opt, $opt_arg) = explode('=', $arg, 2)); $opt_len = strlen($opt); for ($i = 0; $i < count($long_options); $i++) { $long_opt = $long_options[$i]; $opt_start = substr($long_opt, 0, $opt_len); $long_opt_name = str_replace('=', '', $long_opt); /* Option doesn't match. Go on to the next one. */ if ($long_opt_name != $opt) { continue; } $opt_rest = substr($long_opt, $opt_len); /* Check that the options uniquely matches one of the allowed options. */ if ($i + 1 < count($long_options)) { $next_option_rest = substr($long_options[$i + 1], $opt_len); } else { $next_option_rest = ''; } if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < count($long_options) && $opt == substr($long_options[$i + 1], 0, $opt_len) && $next_option_rest != '' && $next_option_rest[0] != '=') { throw new Exception("Console_Getopt: option --{$opt} is ambiguous"); } if (substr($long_opt, -1) == '=') { if (substr($long_opt, -2) != '==') { /* Long option requires an argument. Take the next argument if one wasn't specified. */ if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) { throw new Exception("Console_Getopt: option --{$opt} requires an argument"); } if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) { throw new Exception("Console_Getopt: option requires an argument --{$opt}"); } } } else { if ($opt_arg) { throw new Exception("Console_Getopt: option --{$opt} doesn't allow an argument"); } } $opts[] = array('--' . $opt, $opt_arg); return; } throw new Exception("Console_Getopt: unrecognized option --{$opt}"); }