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