help.drush.inc

  1. 8.0.x commands/core/help.drush.inc
  2. 6.x commands/core/help.drush.inc
  3. 7.x commands/core/help.drush.inc
  4. 4.x commands/core/help.drush.inc
  5. 5.x commands/core/help.drush.inc
  6. master commands/core/help.drush.inc

Functions

Namesort descending Description
drush_core_help Command callback for help command. This is the default command, when none other has been specified.
drush_global_options_command Build a fake command for the purposes of showing examples and options.
drush_help_html Return an HTML page documenting all available commands and global options.
drush_help_html_command_list
drush_help_html_global_options
drush_help_html_header Return an HTML page header that includes all global options.

File

commands/core/help.drush.inc
View source
  1. <?php
  2. /**
  3. * Build a fake command for the purposes of showing examples and options.
  4. */
  5. function drush_global_options_command($brief = FALSE) {
  6. $global_options_help = array(
  7. 'description' => 'Execute a drush command. Run `drush help [command]` to view command-specific help. Run `drush topic` to read even more documentation.',
  8. 'sections' => array(
  9. 'options' => 'Global options (see `drush topic core-global-options` for the full list).',
  10. ),
  11. 'options' => drush_get_global_options($brief),
  12. 'examples' => array(
  13. 'drush dl cck zen' => 'Download CCK module and Zen theme.',
  14. 'drush --uri=http://example.com status' => 'Show status command for the example.com multi-site.',
  15. ),
  16. );
  17. $global_options_help += drush_command_defaults('global-options', 'global_options', __FILE__);
  18. return $global_options_help;
  19. }
  20. /**
  21. * Command callback for help command. This is the default command, when none
  22. * other has been specified.
  23. */
  24. function drush_core_help() {
  25. $commands = func_get_args();
  26. if (empty($commands)) {
  27. // For speed, only bootstrap up to DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION.
  28. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  29. $implemented = drush_get_commands();
  30. // Organize all commands into categories
  31. $command_categories = array();
  32. $category_map = array();
  33. foreach ($implemented as $key => $candidate) {
  34. if ((!array_key_exists('is_alias', $candidate) || !$candidate['is_alias']) && !$candidate['hidden']) {
  35. $category = $candidate['commandfile'];
  36. // If we have decided to remap a category, remap every command
  37. if (array_key_exists($category, $category_map)) {
  38. $category = $category_map[$category];
  39. }
  40. if (!array_key_exists($category, $command_categories)) {
  41. $title = drush_command_invoke_all('drush_help', "meta:$category:title");
  42. $alternate_title = '';
  43. if (!$title) {
  44. // If there is no title, then check to see if the
  45. // command file is stored in a folder with the same
  46. // name as some other command file (e.g. 'core') that
  47. // defines a title.
  48. $alternate = basename($candidate['path']);
  49. $alternate_title = drush_command_invoke_all('drush_help', "meta:$alternate:title");
  50. }
  51. if (!empty($alternate_title)) {
  52. $category_map[$category] = $alternate;
  53. $category = $alternate;
  54. $title = $alternate_title;
  55. }
  56. $command_categories[$category]['title'] = empty($title) ? '' : $title[0];
  57. $summary = drush_command_invoke_all('drush_help', "meta:$category:summary");
  58. if ($summary) {
  59. $command_categories[$category]['summary'] = $summary[0];
  60. }
  61. }
  62. $command_categories[$category]['commands'][] = $key;
  63. }
  64. }
  65. // Sort the command categories; make sure that 'core' is
  66. // always first in the list
  67. $core_category = array('core' => $command_categories['core']);
  68. unset($command_categories['core']);
  69. // Post-process the categories that have no title.
  70. // Any that have fewer than 4 commands will go into a
  71. // section called "other".
  72. $processed_categories = array();
  73. $misc_categories = array();
  74. $other_commands = array();
  75. $other_categories = array();
  76. foreach ($command_categories as $key => $info) {
  77. if (empty($info['title'])) {
  78. $one_category = $key;
  79. if (count($info['commands']) < 4) {
  80. $other_commands = array_merge($other_commands, $info['commands']);
  81. $other_categories[] = $one_category;
  82. }
  83. else {
  84. $info['title'] = dt("All commands in !category", array('!category' => $key));
  85. $misc_categories[$one_category] = $info;
  86. }
  87. }
  88. else {
  89. $processed_categories[$key] = $info;
  90. }
  91. }
  92. $other_category = array();
  93. if (!empty($other_categories)) {
  94. $other_category[implode(',', $other_categories)] = array('title' => dt("Other commands"), 'commands' => $other_commands);
  95. }
  96. asort($processed_categories);
  97. asort($misc_categories);
  98. $command_categories = array_merge($core_category, $processed_categories, $misc_categories, $other_category);
  99. // If the user specified --filter w/out a value, then
  100. // present a list of help options.
  101. if (drush_get_option('filter', FALSE) === TRUE) {
  102. $help_categories = array();
  103. foreach ($command_categories as $key => $info) {
  104. $description = $info['title'];
  105. if (array_key_exists('summary', $info)) {
  106. $description .= ": " . $info['summary'];
  107. }
  108. $help_categories[$key] = $description;
  109. }
  110. $result = drush_choice($help_categories, 'Select a help category:');
  111. if (!$result) {
  112. return drush_user_abort();
  113. }
  114. drush_set_option('filter', $result);
  115. }
  116. if (drush_get_option('html')) {
  117. drush_print(drush_help_html_header());
  118. }
  119. // Make a fake command section to hold the global options, then print it.
  120. $global_options_help = drush_global_options_command(TRUE);
  121. if ((!drush_get_option('html')) && (!drush_get_option('filter'))) {
  122. drush_print_help($global_options_help);
  123. }
  124. // Filter out categories that the user does not want to see
  125. if ($filter_category = drush_get_option('filter')) {
  126. if (!array_key_exists($filter_category, $command_categories)) {
  127. return drush_set_error('DRUSH_NO_CATEGORY', dt("The specified command category !filter does not exist.", array('!filter' => $filter_category)));
  128. }
  129. $command_categories = array($filter_category => $command_categories[$filter_category]);
  130. }
  131. // If the user specified --sort, then merge all of the remaining
  132. // categories together
  133. if (drush_get_option('sort', FALSE)) {
  134. $combined_commands = array();
  135. foreach ($command_categories as $key => $info) {
  136. $combined_commands = array_merge($combined_commands, $info['commands']);
  137. }
  138. $command_categories = array('all' => array('commands' => $combined_commands, 'title' => dt("Commands:")));
  139. }
  140. // Next, print out the table of commands by category.
  141. $all_commands = array();
  142. foreach ($command_categories as $key => $info) {
  143. // Get the commands in this category and sort them
  144. $commands = $info['commands'];
  145. sort($commands);
  146. // Remove hidden commands and build output for drush_print_table.
  147. $rows = array();
  148. foreach($commands as $cmd) {
  149. $command = $implemented[$cmd];
  150. $all_commands[$cmd] = $command;
  151. $name = $command['aliases'] ? $cmd . ' (' . implode(', ', $command['aliases']) . ')': $cmd;
  152. $rows[$cmd] = array($name, $command['description']);
  153. $pipe[] = $cmd;
  154. }
  155. // Vary the output by mode: text or html
  156. if (drush_get_option('html')) {
  157. $sorted_commands = array();
  158. foreach($commands as $cmd) {
  159. $sorted_commands[$cmd] = $implemented[$cmd];
  160. }
  161. drush_print("<h3>" . $info['title'] . "</h3>");
  162. drush_print(drush_help_html_command_list($sorted_commands));
  163. }
  164. else {
  165. drush_print($info['title'] . ": (" . $key . ")");
  166. drush_print_table($rows, FALSE, array(0 => 20));
  167. }
  168. }
  169. // Print out the long-form help for all commands
  170. if (drush_get_option('html')) {
  171. drush_print(drush_help_html_global_options($global_options_help));
  172. drush_print(drush_help_html($all_commands));
  173. }
  174. // Newline-delimited list for use by other scripts. Set the --pipe option.
  175. if (drush_get_option('pipe') && isset($pipe)) {
  176. sort($pipe);
  177. drush_print_pipe(implode(" ", $pipe));
  178. }
  179. return;
  180. }
  181. else {
  182. $result = TRUE;
  183. while ((count($commands) > 0) && !drush_get_error()) {
  184. $result = drush_show_help(array_shift($commands));
  185. }
  186. return $result;
  187. }
  188. return drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command !command.', array('!command' => implode(" ", $commands))));
  189. }
  190. /**
  191. * Return an HTML page header that includes all global options.
  192. */
  193. function drush_help_html_header() {
  194. $output = "<html><head><title>drush help</title><style>dt {font-size: 110%; font-weight: bold}</style></head><body>\n";
  195. }
  196. function drush_help_html_global_options($global_options_help) {
  197. // Global options
  198. $global_option_rows = drush_format_help_section($global_options_help, 'options');
  199. $output = '<h3>Global Options (see `drush topic core-global-options` for the full list)</h3><table>';
  200. foreach ($global_option_rows as $row) {
  201. $output .= "<tr>";
  202. foreach ($row as $value) {
  203. $output .= "<td>" . htmlspecialchars($value) . "</td>\n";
  204. }
  205. $output .= "</tr>";
  206. }
  207. $output .= "</table>\n";
  208. return $output;
  209. }
  210. function drush_help_html_command_list($commands) {
  211. // Command table
  212. $output = "<table>\n";
  213. foreach ($commands as $key => $command) {
  214. $output .= " <tr><td><a href=\"#$key\">$key</a></td><td>" . $command['description'] . "</td></tr>\n";
  215. }
  216. $output .= "</table>\n";
  217. return $output;
  218. }
  219. /**
  220. * Return an HTML page documenting all available commands and global options.
  221. */
  222. function drush_help_html($commands) {
  223. // Command details
  224. $output = '<h3>Command detail</h3><dl>';
  225. foreach ($commands as $key => $command) {
  226. $output .= "\n<a name=\"$key\"></a><dt>$key</dt><dd><pre>\n";
  227. ob_start();
  228. drush_show_help($key);
  229. $output .= ob_get_clean();
  230. $output .= "</pre></dd>\n";
  231. }
  232. $output .= "</body></html>\n";
  233. return $output;
  234. }