#!/usr/bin/php DRUSH_USER) : array('name' => DRUSH_USER)); if (empty($user)) { drush_die(is_numeric(DRUSH_USER) ? t('Could not login with user ID #%user.', array('%user' => DRUSH_USER)) : t('Could not login with user account `%user\'.', array('%user' => DRUSH_USER))); } return TRUE; } /** * Parse console arguments. * * @param $args The console argument array (usually $argv) * @param $arg_opts An array of options that are followed by an argument. * e.g. shell.php -u admin -v --> $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 wether 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]; } } /** * Converts a windows path (dir1\dir2\dir3) into a unix path (dir1/dir2/dir3). **/ function drush_convert_path($path) { return str_replace('\\','/', $path); } ?>