/** * Parses the command-line options. * * The first parameter to this function should be the list of command-line * arguments without the leading reference to the running program. * * The second parameter is a string of allowed short options. Each of the * option letters can be followed by a colon ':' to specify that the option * requires an argument, or a double colon '::' to specify that the option * takes an optional argument. * * The third argument is an optional array of allowed long options. The * leading '--' should not be included in the option name. Options that * require an argument should be followed by '=', and options that take an * option argument should be followed by '=='. * * The return value is an array of two elements: the list of parsed * options and the list of non-option command-line arguments. Each entry in * the list of parsed options is a pair of elements - the first one * specifies the option, and the second one specifies the option argument, * if there was one. * * Long and short options can be mixed. * * Most of the semantics of this function are based on GNU getopt_long(). * * @param $args array an array of command-line arguments * @param $short_options string specifies the list of allowed short options * @param $long_options array specifies the list of allowed long options * * @return array two-element array containing the list of parsed options and * the non-option arguments * * @access public * */ public static function getopt($args, $short_options, $long_options = null) { $opts = array(); $non_opts = array(); settype($args, 'array'); if ($long_options) { sort($long_options); } reset($args); while (list(, $arg) = each($args)) { /* The special element '--' means explicit end of options. Treat the rest of the arguments as non-options and end the loop. */ if ($arg == '--') { $non_opts = array_merge($non_opts, array_slice($args, $i + 1)); break; } if ($arg[0] != '-' || $arg[1] == '-' && !$long_options) { $non_opts[] = $arg; } else { if ($arg[1] == '-') { if (!Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args)) { return false; } } else { if (!Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args)) { return false; } } } } return array($opts, $non_opts); }
/** * The actual implementation of the argument parsing code. */ function doGetopt($version, $args, $short_options, $long_options = null) { // in case you pass directly readPHPArgv() as the first arg if (PEAR::isError($args)) { return $args; } if (empty($args)) { return array(array(), array()); } $opts = array(); $non_opts = array(); settype($args, 'array'); if ($long_options) { sort($long_options); } /* * Preserve backwards compatibility with callers that relied on * erroneous POSIX fix. */ if ($version < 2) { if (isset($args[0][0]) && $args[0][0] != '-') { array_shift($args); } } reset($args); while (list($i, $arg) = each($args)) { /* The special element '--' means explicit end of options. Treat the rest of the arguments as non-options and end the loop. */ if ($arg == '--') { $non_opts = array_merge($non_opts, array_slice($args, $i + 1)); break; } if ($arg[0] != '-' || strlen($arg) > 1 && $arg[1] == '-' && !$long_options) { $non_opts = array_merge($non_opts, array_slice($args, $i)); break; } elseif (strlen($arg) > 1 && $arg[1] == '-') { $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args); if (PEAR::isError($error)) { return $error; } } else { $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args); if (PEAR::isError($error)) { return $error; } } } return array($opts, $non_opts); }