...')); drush_print(); drush_print('Run "drush help [command]" to view command-specific help.'); drush_print(dt('Options: ')); foreach (drush_get_options() as $option => $description) { $rows[] = array($option, $description); } drush_print_table($rows, 2); drush_print(); drush_print('Commands: '); $commands = drush_get_commands(); $rows = array(); foreach($commands as $key => $command) { $rows[] = array($key, $commands[$key]['description']); } drush_print_table($rows, 2); return; } return drush_error(dt('Invalid command !command.', array('!command' => implode(" ", $commands)))); } /** * Get the available options for Drush for use by help page. * * @return * An associative array containing the option definition as the key, and the description as the value, * for each of the available options. */ function drush_get_options() { // TODO: Add a hook for this, to allow other modules to add their options $options['-r , --root='] = dt("Drupal root directory to use (default: current directory)"); $options['-l , --uri='] = dt('URI of the drupal site to use (only needed in multisite environments)'); $options['-v, --verbose'] = dt('Display all available output'); $options['-y, --yes'] = dt("Assume 'yes' as answer to all prompts"); $options['-s, --simulate'] = dt("Simulate all relevant actions (don't actually change the system)"); $options['-i, --include'] = dt("A list of paths to search for drush commands"); $options['-c, --config'] = dt("Specify a config file to use. See example.drushrc.php"); $options['-u, --user'] = dt("Specify a user to login with. May be a name or a number."); return $options; } /** * Prints out the default drush help page. */ function drush_show_help($commands) { $commandstring = implode(" ", $commands); if (!drush_is_command($commandstring)) { return drush_error(dt('Invalid command !command.', array('!command' => $commandstring))); } $help = drush_command_invoke_all('drush_help', 'drush:'. $commandstring); if (!empty($help)) { drush_print(implode("\n", $help)); } else { drush_print("No help available for command 'drush $commandstring'."); } } /** * */ function drush_help_command($command) { $commandstring = implode(" ", $commands); if (!drush_is_command($commandstring)) { return drush_error(dt('Invalid command: !command.', array('!command' => implode(" ", $commands)))); } $help = drush_command_invoke_all('drush_help', 'drush:'. $commandstring); if (!empty($help)) { drush_print(implode("\n", $help)); } else { drush_print("No help available for command 'drush $commandstring'."); } } /** * Executes a shell command. * Output is only printed if in verbose mode. * Output is stored and can be retrieved using drush_shell_exec_output(). * If in simulation mode, no action is taken. * * @param $cmd * The command to execute. * @param $indent * Indentation for the output (only for verbose mode). */ function drush_shell_exec($cmd, $indent = 0) { if (DRUSH_VERBOSE || DRUSH_SIMULATE) { drush_print('Executing: ' . $cmd, $indent); } if (DRUSH_SIMULATE) { return true; } exec($cmd . ' 2>&1', $output, $result); _drush_shell_exec_output_set($output); if (DRUSH_VERBOSE) { foreach ($output as $line) { drush_print($line, $indent + 2); } } // Exit code 0 means success. return ($result == 0); } /** * Stores output for the most recent shell command. * This should only be run from drush_shell_exec(). * * @param $output * The output of the most recent shell command. * If this is not set the stored value will be returned. */ function _drush_shell_exec_output_set($output = FALSE) { static $stored_output; if (!$output) return $stored_output; $stored_output = $output; } /** * Returns the output of the most recent shell command as an array of lines. */ function drush_shell_exec_output() { return _drush_shell_exec_output_set(); } /** * Exits with a message. * TODO: Exit with a correct status code. */ function drush_die($msg = NULL, $status = NULL) { die($msg ? "drush: $msg\n" : ''); } /** * Prints an error message. * Always returns FALSE. This allows to do e.g. * if ($error) { return drush_error('A error occured); } * to make your function return FALSE in case of an error. */ function drush_error($msg = '') { // TODO: print to stderr if running in CLI mode. drush_print("E: " . $msg); return FALSE; } /** * Prints a message. * @param $message * The message to print. * @param $indent * The indentation (space chars) */ function drush_print($message = '', $indent = 0) { print str_repeat(' ', $indent) . (string)$message . "\n"; } /** * Prints a message, but only if verbose mode is activated. * Returns TRUE if in verbose mode, otherwise FALSE. */ function drush_verbose($msg = FALSE, $indent = 0) { if (!DRUSH_VERBOSE) { return FALSE; } if (DRUSH_VERBOSE && $msg === FALSE) { return TRUE; } print str_repeat(' ', $indent) . (string)$msg . "\n"; return TRUE; } /** * Ask the user a basic yes/no question. * * @param $msg The question to ask * @return TRUE if the user entered 'y', FALSE if he entered 'n' */ function drush_confirm($msg, $indent = 0) { print str_repeat(' ', $indent) . (string)$msg . " (y/n): "; if (DRUSH_AFFIRMATIVE) { print "y\n"; return TRUE; } while ($line = trim(fgets(STDIN))) { if ($line == 'y') { return TRUE; } if ($line == 'n') { return FALSE; } print str_repeat(' ', $indent) . (string)$msg . " (y/n): "; } } /** * Print a formatted table. * @param $rows * The rows to print * @param $indent * Indentation for the whole table * @param $header * If TRUE, the first line will be treated as table * header and therefore be underlined. */ function drush_print_table($rows, $indent = 0, $header = FALSE) { if (count($rows) == 0) { // Nothing to output. return; } $indent = str_repeat(' ', $indent); $format = _drush_get_table_row_format($rows); $header_printed = FALSE; foreach ($rows as $cols) { // Print the current line. print $indent . vsprintf($format, $cols) . "\n"; // Underline the first row if $header is set to true. if (!$header_printed && $header) { $headers = array(); foreach ($cols as $col) { $headers[] = str_repeat('-', strlen($col)); } print $indent . trim(vsprintf($format, $headers)) . "\n"; $header_printed = TRUE; } } } /** * Create the table row format string to be used in vsprintf(). */ function _drush_get_table_row_format($table) { $widths = _drush_get_table_column_widths($table); foreach ($widths as $col_width) { $col_formats[] = "%-{$col_width}s"; } $format = implode("\t", $col_formats); return $format; } /** * Calculate table column widths. */ function _drush_get_table_column_widths($table) { $widths = array(); foreach ($table as $row => $cols) { foreach ($cols as $col => $value) { $old_width = isset($widths[$col]) ? $widths[$col] : 0; $widths[$col] = max($old_width, strlen((string)$value)); } } return $widths; }