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_format_help_section Format one named help section from a command record
drush_global_options_command Build a fake command for the purposes of showing examples and options.
drush_help_categorize Organize commands into categories
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.
drush_help_listing_print Print CLI table or HTML table listing all commands.
drush_help_section_default_formatter The default section formatter. Replaces '[command]' with the command name.
drush_help_section_formatter_options The options section formatter. Adds a "--" in front of each item label. Also handles short-form and example-value components in the help attributes.
drush_help_visible
drush_print_help Print the help for a single command to the screen.
drush_show_help Prints out help for a given command.
_drush_format_help_subsection Format one named portion of a subsection from a command record. Subsections allow related parts of a help record to be grouped together. For example, in the 'options' section, sub-options that are related to a particular primary option are…
_drush_help_merge_subcommand_information Check to see if the specified command contains an 'allow-additional-options' record. If it does, find the additional options that are allowed, and add in the help text for the options of all of the sub-commands.
_drush_help_sort_command_options Sort command options alphabetically. Engine options at the end.

File

commands/core/help.drush.inc
View source
  1. <?php
  2. /**
  3. * Prints out help for a given command.
  4. */
  5. function drush_show_help($commandstring) {
  6. // First check and see if the command can already be found.
  7. $commands = drush_get_commands();
  8. if (!array_key_exists($commandstring, $commands)) {
  9. // If the command cannot be found, then bootstrap so that
  10. // additional commands will be brought in.
  11. // For speed, only bootstrap up to DRUSH_BOOTSTRAP_DRUPAL_SITE.
  12. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  13. $commands = drush_get_commands();
  14. }
  15. if (array_key_exists($commandstring, $commands)) {
  16. $command = $commands[$commandstring];
  17. drush_print_help($command);
  18. return TRUE;
  19. }
  20. $shell_aliases = drush_get_context('shell-aliases', array());
  21. if (array_key_exists($commandstring, $shell_aliases)) {
  22. $msg = dt("'@alias-name' is a shell alias. Its value is: !name. See `drush topic docs-shell-aliases` and `drush shell-alias` for more information.", array('@alias-name' => $commandstring, '!name' => $shell_aliases[$commandstring]));
  23. drush_log($msg, 'ok');
  24. return TRUE;
  25. }
  26. return drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command !command.', array('!command' => $commandstring)));
  27. }
  28. /**
  29. * Print the help for a single command to the screen.
  30. *
  31. * @param array $command
  32. * A fully loaded $command array.
  33. */
  34. function drush_print_help($command) {
  35. _drush_help_merge_subcommand_information($command);
  36. if (!$help = drush_command_invoke_all('drush_help', 'drush:'. $command['command'])) {
  37. $help = array($command['description']);
  38. }
  39. if ($command['strict-option-handling']) {
  40. $command['topics'][] = 'docs-strict-options';
  41. }
  42. // Give commandfiles an opportunity to add examples and options to the command.
  43. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  44. drush_engine_add_help_topics($command);
  45. drush_command_invoke_all_ref('drush_help_alter', $command);
  46. drush_print(wordwrap(implode("\n", $help), drush_get_context('DRUSH_COLUMNS', 80)));
  47. drush_print();
  48. // Sort command options.
  49. uksort($command['options'], '_drush_help_sort_command_options');
  50. // Print command sections help.
  51. foreach ($command['sections'] as $key => $value) {
  52. if (!empty($command[$key])) {
  53. $rows = drush_format_help_section($command, $key);
  54. if ($rows) {
  55. drush_print(dt($value) . ':');
  56. drush_print_table($rows, FALSE, array('label' => 40));
  57. unset($rows);
  58. drush_print();
  59. }
  60. }
  61. }
  62. // Append aliases if any.
  63. if ($command['aliases']) {
  64. drush_print(dt("Aliases: ") . implode(', ', $command['aliases']));
  65. }
  66. }
  67. /**
  68. * Sort command options alphabetically. Engine options at the end.
  69. */
  70. function _drush_help_sort_command_options($a, $b) {
  71. $engine_a = strpos($a, '=');
  72. $engine_b = strpos($b, '=');
  73. if ($engine_a && !$engine_b) {
  74. return 1;
  75. }
  76. else if (!$engine_a && $engine_b) {
  77. return -1;
  78. }
  79. elseif ($engine_a && $engine_b) {
  80. if (substr($a, 0, $engine_a) == substr($b, 0, $engine_b)) {
  81. return 0;
  82. }
  83. }
  84. return ($a < $b) ? -1 : 1;
  85. }
  86. /**
  87. * Check to see if the specified command contains an 'allow-additional-options'
  88. * record. If it does, find the additional options that are allowed, and
  89. * add in the help text for the options of all of the sub-commands.
  90. */
  91. function _drush_help_merge_subcommand_information(&$command) {
  92. // 'allow-additional-options' will either be FALSE (default),
  93. // TRUE ("allow anything"), or an array that lists subcommands
  94. // that are or may be called via drush_invoke by this command.
  95. if (is_array($command['allow-additional-options'])) {
  96. $implemented = drush_get_commands();
  97. foreach ($command['allow-additional-options'] as $subcommand_name) {
  98. if (array_key_exists($subcommand_name, $implemented)) {
  99. $command['options'] += $implemented[$subcommand_name]['options'];
  100. $command['sub-options'] = array_merge_recursive($command['sub-options'], $implemented[$subcommand_name]['sub-options']);
  101. if (empty($command['arguments'])) {
  102. $command['arguments'] = $implemented[$subcommand_name]['arguments'];
  103. }
  104. $command['topics'] = array_merge($command['topics'], $implemented[$subcommand_name]['topics']);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Format one named help section from a command record
  111. *
  112. * @param $command
  113. * A command record with help information
  114. * @param $section
  115. * The name of the section to format ('options', 'topic', etc.)
  116. * @returns array
  117. * Formatted rows, suitable for printing via drush_print_table. The returned
  118. * array can be empty.
  119. */
  120. function drush_format_help_section($command, $section) {
  121. $rows = array();
  122. $formatter = (function_exists('drush_help_section_formatter_' . $section)) ? 'drush_help_section_formatter_' . $section : 'drush_help_section_default_formatter';
  123. foreach ($command[$section] as $name => $help_attributes) {
  124. if (!is_array($help_attributes)) {
  125. $help_attributes = array('description' => $help_attributes);
  126. }
  127. $help_attributes['label'] = $name;
  128. call_user_func_array($formatter, array($command, &$help_attributes));
  129. if (empty($help_attributes['hidden'])) {
  130. $rows[] = array('label' => $help_attributes['label'], 'description' => $help_attributes['description']);
  131. // Process the subsections too, if any
  132. if (!empty($command['sub-' . $section]) && array_key_exists($name, $command['sub-' . $section])) {
  133. $rows = array_merge($rows, _drush_format_help_subsection($command, $section, $name, $formatter));
  134. }
  135. }
  136. }
  137. return $rows;
  138. }
  139. /**
  140. * Format one named portion of a subsection from a command record.
  141. * Subsections allow related parts of a help record to be grouped
  142. * together. For example, in the 'options' section, sub-options that
  143. * are related to a particular primary option are stored in a 'sub-options'
  144. * section whose name == the name of the primary option.
  145. *
  146. * @param $command
  147. * A command record with help information
  148. * @param $section
  149. * The name of the section to format ('options', 'topic', etc.)
  150. * @param $subsection
  151. * The name of the subsection (e.g. the name of the primary option)
  152. * @param $formatter
  153. * The name of a function to use to format the rows of the subsection
  154. * @param $prefix
  155. * Characters to prefix to the front of the label (for indentation)
  156. * @returns array
  157. * Formatted rows, suitable for printing via drush_print_table.
  158. */
  159. function _drush_format_help_subsection($command, $section, $subsection, $formatter, $prefix = ' ') {
  160. $rows = array();
  161. foreach ($command['sub-' . $section][$subsection] as $name => $help_attributes) {
  162. if (!is_array($help_attributes)) {
  163. $help_attributes = array('description' => $help_attributes);
  164. }
  165. $help_attributes['label'] = $name;
  166. call_user_func_array($formatter, array($command, &$help_attributes));
  167. if (!array_key_exists('hidden', $help_attributes)) {
  168. $rows[] = array('label' => $prefix . $help_attributes['label'], 'description' => $help_attributes['description']);
  169. // Process the subsections too, if any
  170. if (!empty($command['sub-' . $section]) && array_key_exists($name, $command['sub-' . $section])) {
  171. $rows = array_merge($rows, _drush_format_help_subsection($command, $section, $name, $formatter, $prefix . ' '));
  172. }
  173. }
  174. }
  175. return $rows;
  176. }
  177. /**
  178. * The options section formatter. Adds a "--" in front of each
  179. * item label. Also handles short-form and example-value
  180. * components in the help attributes.
  181. */
  182. function drush_help_section_formatter_options($command, &$help_attributes) {
  183. if ($help_attributes['label'][0] == '-') {
  184. drush_log(dt("Option '!option' of command !command should instead be declared as '!fixed'", array('!option' => $help_attributes['label'], '!command' => $command['command'], '!fixed' => preg_replace('/^--*/', '', $help_attributes['label']))), 'debug');
  185. }
  186. else {
  187. $help_attributes['label'] = '--' . $help_attributes['label'];
  188. }
  189. if (!empty($help_attributes['required'])) {
  190. $help_attributes['description'] .= " " . dt("Required.");
  191. }
  192. $prefix = '<';
  193. $suffix = '>';
  194. if (array_key_exists('example-value', $help_attributes)) {
  195. if (isset($help_attributes['value']) && $help_attributes['value'] == 'optional') {
  196. $prefix = '[';
  197. $suffix = ']';
  198. }
  199. $help_attributes['label'] .= '=' . $prefix . $help_attributes['example-value'] . $suffix;
  200. if (array_key_exists('short-form', $help_attributes)) {
  201. $help_attributes['short-form'] .= " $prefix" . $help_attributes['example-value'] . $suffix;
  202. }
  203. }
  204. if (array_key_exists('short-form', $help_attributes)) {
  205. $help_attributes['label'] = '-' . $help_attributes['short-form'] . ', ' . $help_attributes['label'];
  206. }
  207. drush_help_section_default_formatter($command, $help_attributes);
  208. }
  209. /**
  210. * The default section formatter. Replaces '[command]' with the
  211. * command name.
  212. */
  213. function drush_help_section_default_formatter($command, &$help_attributes) {
  214. // '[command]' is a token representing the current command. @see pm_drush_engine_version_control().
  215. $help_attributes['label'] = str_replace('[command]', $command['command'], $help_attributes['label']);
  216. }
  217. /**
  218. * Build a fake command for the purposes of showing examples and options.
  219. */
  220. function drush_global_options_command($brief = FALSE) {
  221. $global_options_help = array(
  222. 'description' => 'Execute a drush command. Run `drush help [command]` to view command-specific help. Run `drush topic` to read even more documentation.',
  223. 'sections' => array(
  224. 'options' => 'Global options (see `drush topic core-global-options` for the full list)',
  225. ),
  226. 'options' => drush_get_global_options($brief),
  227. 'examples' => array(
  228. 'drush dl cck zen' => 'Download CCK module and Zen theme.',
  229. 'drush --uri=http://example.com status' => 'Show status command for the example.com multi-site.',
  230. ),
  231. '#brief' => TRUE,
  232. );
  233. $global_options_help += drush_command_defaults('global-options', 'global_options', __FILE__);
  234. drush_command_invoke_all_ref('drush_help_alter', $global_options_help);
  235. ksort($global_options_help['options']);
  236. return $global_options_help;
  237. }
  238. /**
  239. * Command callback for help command. This is the default command, when none
  240. * other has been specified.
  241. */
  242. function drush_core_help() {
  243. $commands = func_get_args();
  244. $format = drush_get_option('format');
  245. // Backwards compatibility for --html option.
  246. if (drush_get_option('html')) {
  247. $format = 'html';
  248. }
  249. if (empty($commands)) {
  250. // For speed, only bootstrap up to DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION.
  251. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  252. $implemented = drush_get_commands();
  253. ksort($implemented);
  254. $command_categories = drush_help_categorize($implemented);
  255. $visible = drush_help_visible($command_categories);
  256. // If the user specified --filter w/out a value, then
  257. // present a choice list of help categories.
  258. if (drush_get_option('filter', FALSE) === TRUE) {
  259. $help_categories = array();
  260. foreach ($command_categories as $key => $info) {
  261. $description = $info['title'];
  262. if (array_key_exists('summary', $info)) {
  263. $description .= ": " . $info['summary'];
  264. }
  265. $help_categories[$key] = $description;
  266. }
  267. $result = drush_choice($help_categories, 'Select a help category:');
  268. if (!$result) {
  269. return drush_user_abort();
  270. }
  271. drush_set_option('filter', $result);
  272. }
  273. // Filter out categories that the user does not want to see
  274. $filter_category = drush_get_option('filter');
  275. if (!empty($filter_category) && ($filter_category !== TRUE)) {
  276. if (!array_key_exists($filter_category, $command_categories)) {
  277. return drush_set_error('DRUSH_NO_CATEGORY', dt("The specified command category !filter does not exist.", array('!filter' => $filter_category)));
  278. }
  279. $command_categories = array($filter_category => $command_categories[$filter_category]);
  280. }
  281. if ($format == 'html') {
  282. drush_print(drush_help_html_header());
  283. }
  284. // Make a fake command section to hold the global options, then print it.
  285. $global_options_help = drush_global_options_command(TRUE);
  286. if (!in_array($format, array('html', 'var_export', 'json')) && !drush_get_option('filter')) {
  287. drush_print_help($global_options_help);
  288. }
  289. if ($format == 'json' || $format == 'var_export') {
  290. // Print giant array, and set backend_result. We do it this way because this command
  291. // isn't yet fully compliant with outputformat system. To do so, we need to deal with
  292. // the fact that this command varies output significantly when showing help for one
  293. // command versus a listing. We also have oddball HTML format.
  294. drush_print(drush_format($command_categories, 'commands'));
  295. drush_backend_set_result($command_categories);
  296. return NULL;
  297. }
  298. else {
  299. // HTML and CLI command listing (in a table).
  300. drush_help_listing_print($command_categories, $format);
  301. if ($format == 'html') {
  302. // Print global options and long-form help for all commands.
  303. drush_print(drush_help_html_global_options($global_options_help));
  304. drush_print(drush_help_html($visible));
  305. }
  306. }
  307. // Newline-delimited list for use by other scripts. Set the --pipe option.
  308. if (drush_get_option('pipe')) {
  309. drush_print_pipe(implode("\n", array_keys($visible)));
  310. }
  311. return;
  312. }
  313. else {
  314. $result = TRUE;
  315. while ((count($commands) > 0) && !drush_get_error()) {
  316. $result = drush_show_help(array_shift($commands));
  317. }
  318. return $result;
  319. }
  320. return drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command !command.', array('!command' => implode(" ", $commands))));
  321. }
  322. // Uncategorize the list of commands. Hiddens have been removed and
  323. // filtering performed.
  324. function drush_help_visible($command_categories) {
  325. $all = array();
  326. foreach ($command_categories as $category => $info) {
  327. $all = array_merge($all, $info['commands']);
  328. }
  329. return $all;
  330. }
  331. /**
  332. * Print CLI table or HTML table listing all commands.
  333. */
  334. function drush_help_listing_print($command_categories, $format) {
  335. $all_commands = array();
  336. foreach ($command_categories as $key => $info) {
  337. // Get the commands in this category.
  338. $commands = $info['commands'];
  339. // Build rows for drush_print_table().
  340. $rows = array();
  341. foreach($commands as $cmd => $command) {
  342. $name = $command['aliases'] ? $cmd . ' (' . implode(', ', $command['aliases']) . ')': $cmd;
  343. $rows[$cmd] = array('name' => $name, 'description' => $command['description']);
  344. }
  345. // Vary the output by mode: CLI or HTML
  346. if ($format == 'html') {
  347. drush_print("<h3>" . $info['title'] . "</h3>");
  348. drush_print(drush_help_html_command_list($commands));
  349. }
  350. else {
  351. drush_print($info['title'] . ": (" . $key . ")");
  352. drush_print_table($rows, FALSE, array('name' => 20));
  353. }
  354. }
  355. }
  356. /**
  357. * Organize commands into categories
  358. */
  359. function drush_help_categorize($implemented) {
  360. $command_categories = array();
  361. $category_map = array();
  362. foreach ($implemented as $key => $candidate) {
  363. if ((!array_key_exists('is_alias', $candidate) || !$candidate['is_alias']) && !$candidate['hidden']) {
  364. $category = $candidate['category'];
  365. // If we have decided to remap a category, remap every command
  366. if (array_key_exists($category, $category_map)) {
  367. $category = $category_map[$category];
  368. }
  369. if (!array_key_exists($category, $command_categories)) {
  370. $title = drush_command_invoke_all('drush_help', "meta:$category:title");
  371. $alternate_title = '';
  372. if (!$title) {
  373. // If there is no title, then check to see if the
  374. // command file is stored in a folder with the same
  375. // name as some other command file (e.g. 'core') that
  376. // defines a title.
  377. $alternate = basename($candidate['path']);
  378. $alternate_title = drush_command_invoke_all('drush_help', "meta:$alternate:title");
  379. }
  380. if (!empty($alternate_title)) {
  381. $category_map[$category] = $alternate;
  382. $category = $alternate;
  383. $title = $alternate_title;
  384. }
  385. $command_categories[$category]['title'] = empty($title) ? '' : $title[0];
  386. $summary = drush_command_invoke_all('drush_help', "meta:$category:summary");
  387. if ($summary) {
  388. $command_categories[$category]['summary'] = $summary[0];
  389. }
  390. }
  391. $candidate['category'] = $category;
  392. $command_categories[$category]['commands'][$key] = $candidate;
  393. }
  394. }
  395. // Make sure that 'core' is always first in the list
  396. $core_category = array('core' => $command_categories['core']);
  397. unset($command_categories['core']);
  398. // Post-process the categories that have no title.
  399. // Any that have fewer than 4 commands go into a section called "other".
  400. $processed_categories = array();
  401. $misc_categories = array();
  402. $other_commands = array();
  403. $other_categories = array();
  404. foreach ($command_categories as $key => $info) {
  405. if (empty($info['title'])) {
  406. $one_category = $key;
  407. if (count($info['commands']) < 4) {
  408. $other_commands = array_merge($other_commands, $info['commands']);
  409. $other_categories[] = $one_category;
  410. }
  411. else {
  412. $info['title'] = dt("All commands in !category", array('!category' => $key));
  413. $misc_categories[$one_category] = $info;
  414. }
  415. }
  416. else {
  417. $processed_categories[$key] = $info;
  418. }
  419. }
  420. $other_category = array();
  421. if (!empty($other_categories)) {
  422. $other_category[implode(',', $other_categories)] = array('title' => dt("Other commands"), 'commands' => $other_commands);
  423. }
  424. asort($processed_categories);
  425. asort($misc_categories);
  426. $command_categories = array_merge($core_category, $processed_categories, $misc_categories, $other_category);
  427. // If the user specified --sort, then merge all of the remaining
  428. // categories together
  429. if (drush_get_option('sort', FALSE)) {
  430. $combined_commands = array();
  431. foreach ($command_categories as $key => $info) {
  432. $combined_commands = array_merge($combined_commands, $info['commands']);
  433. }
  434. $command_categories = array('all' => array('commands' => $combined_commands, 'title' => dt("Commands:")));
  435. }
  436. return $command_categories;
  437. }
  438. /**
  439. * Return an HTML page header that includes all global options.
  440. */
  441. function drush_help_html_header() {
  442. $output = "<html><head><title>drush help</title><style>dt {font-size: 110%; font-weight: bold}</style></head><body>\n";
  443. }
  444. function drush_help_html_global_options($global_options_help) {
  445. // Global options
  446. $global_option_rows = drush_format_help_section($global_options_help, 'options');
  447. $output = '<h3>Global Options (see `drush topic core-global-options` for the full list)</h3><table>';
  448. foreach ($global_option_rows as $row) {
  449. $output .= "<tr>";
  450. foreach ($row as $value) {
  451. $output .= "<td>" . htmlspecialchars($value) . "</td>\n";
  452. }
  453. $output .= "</tr>";
  454. }
  455. $output .= "</table>\n";
  456. return $output;
  457. }
  458. function drush_help_html_command_list($commands) {
  459. // Command table
  460. $output = "<table>\n";
  461. foreach ($commands as $key => $command) {
  462. $output .= " <tr><td><a href=\"#$key\">$key</a></td><td>" . $command['description'] . "</td></tr>\n";
  463. }
  464. $output .= "</table>\n";
  465. return $output;
  466. }
  467. /**
  468. * Return an HTML page documenting all available commands and global options.
  469. */
  470. function drush_help_html($commands) {
  471. // Command details
  472. $output = '<h3>Command detail</h3><dl>';
  473. foreach ($commands as $key => $command) {
  474. $output .= "\n<a name=\"$key\"></a><dt>$key</dt><dd><pre>\n";
  475. ob_start();
  476. drush_show_help($key);
  477. $output .= ob_get_clean();
  478. $output .= "</pre></dd>\n";
  479. }
  480. $output .= "</body></html>\n";
  481. return $output;
  482. }