$arg_opts = array('u') * @param $default_options * @return * A associative array: * $return['commands'] ia a numeric array of all commands, * $return['options'] contains the options. The option keys * are always noted without - or -- and are set to TRUE if they were * invoked, to the argument if followed by an argument, and if not present * to their default value or FALSE if no default value was specified. */ function drush_parse_args($args = array(), $arg_opts = array(), $default_options = array()) { $options = $default_options; $commands = array(); for ($i = 1; $i < count($args); $i++) { $opt = $args[$i]; // Is the arg an option (starting with '-')? if ($opt{0} == "-" && strlen($opt) != 1) { // Do we have multiple options behind one '-'? if (strlen($opt) > 2 && $opt{1} != "-") { // Each char becomes a key of its own. for ($j = 1; $j < strlen($opt); $j++) { $options[substr($opt, $j, 1)] = true; } } // Do we have a longopt (starting with '--')? elseif ($opt{1} == "-") { if ($pos = strpos($opt, '=')) { $options[substr($opt, 2, $pos - 2)] = substr($opt, $pos + 1); } else { $options[substr($opt, 2)] = true; } } else { $opt = substr($opt, 1); // Check if the current opt is in $arg_opts (= has to be followed by an argument). if ((in_array($opt, $arg_opts))) { if (($args[$i+1] == NULL) || ($args[$i+1] == "") || ($args[$i + 1]{0} == "-")) { exit("Invalid input: -$opt needs to be followed by an argument."); } $options[$opt] = $args[$i + 1]; $i++; } else { $options[$opt] = true; } } } // If it's not an option, it's a command. else { $commands[] = $opt; } } return array('options' => $options, 'commands' => $commands); } /** * Get the value for an option. * * If the first argument is an array, then it checks whether one of the options * exists and return the value of the first one found. Useful for allowing both * -h and --host-name * */ function drush_get_option($option, $default = NULL) { $options = $GLOBALS['args']['options']; if (is_array($option)) { foreach ($option as $current) { if (array_key_exists($current, $options)) { return $options[$current]; } } return $default; } if (!array_key_exists($option, $options)) { return $default; } else { return $options[$option]; } }