4 * Implementation of hook_drush_command().
6 * In this hook, you specify which commands your
7 * drush module makes available, what it does and
10 * Notice how this structure closely resembles how
11 * you define menu hooks.
14 * An associative array describing your command(s).
16 function helpsingle_drush_command() {
18 $items['helpsingle'] = array(
19 'description' => 'Print help for a single command',
20 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
21 'allow-additional-options' => TRUE,
24 'command' => 'A command name, or command alias.',
27 'drush help pm-download' => 'Show help for one command.',
28 'drush help dl' => 'Show help for one command using an alias.',
30 'topics' => array('docs-readme'),
36 * Command callback. Show help for a single command.
38 function drush_core_helpsingle($commandstring) {
39 // First check and see if the command can already be found.
40 $commands = drush_get_commands();
41 if (!array_key_exists($commandstring, $commands)) {
42 // If the command cannot be found, then bootstrap so that
43 // additional commands will be brought in.
44 // TODO: We need to do a full bootstrap in order to find module service
45 // commands. We only need to do this for Drupal 8, though; 7 and earlier
46 // can stop at DRUSH_BOOTSTRAP_DRUPAL_SITE. Perhaps we could use command
47 // caching to avoid bootstrapping, if we have collected the commands for
48 // this site once already.
49 drush_bootstrap_max();
50 $commands = drush_get_commands();
52 if (array_key_exists($commandstring, $commands)) {
53 $command = $commands[$commandstring];
55 annotationcommand_adapter_add_hook_options($command);
57 drush_print_help($command);
60 $shell_aliases = drush_get_context('shell-aliases', array());
61 if (array_key_exists($commandstring, $shell_aliases)) {
62 $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]));
63 drush_log($msg, 'ok');
66 return drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command !command.', array('!command' => $commandstring)));
70 * Print the help for a single command to the screen.
72 * @param array $command
73 * A fully loaded $command array.
75 function drush_print_help($command) {
76 _drush_help_merge_subcommand_information($command);
78 if (!$help = drush_command_invoke_all('drush_help', 'drush:'. $command['command'])) {
79 $help = array($command['description']);
82 if ($command['strict-option-handling']) {
83 $command['topics'][] = 'docs-strict-options';
86 // Give commandfiles an opportunity to add examples and options to the command.
87 drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
88 drush_engine_add_help_topics($command);
89 drush_command_invoke_all_ref('drush_help_alter', $command);
91 drush_print(wordwrap(implode("\n", $help), drush_get_context('DRUSH_COLUMNS', 80)));
94 $global_options = drush_get_global_options();
95 foreach ($command['global-options'] as $global_option) {
96 $command['options'][$global_option] = $global_options[$global_option];
99 // Sort command options.
100 uksort($command['options'], '_drush_help_sort_command_options');
102 // Print command sections help.
103 foreach ($command['sections'] as $key => $value) {
104 if (!empty($command[$key])) {
105 $rows = drush_format_help_section($command, $key);
107 drush_print(dt($value) . ':');
108 drush_print_table($rows, FALSE, array('label' => 40));
115 // Append aliases if any.
116 if ($command['aliases']) {
117 drush_print(dt("Aliases: ") . implode(', ', $command['aliases']));
122 * Sort command options alphabetically. Engine options at the end.
124 function _drush_help_sort_command_options($a, $b) {
125 $engine_a = strpos($a, '=');
126 $engine_b = strpos($b, '=');
127 if ($engine_a && !$engine_b) {
130 else if (!$engine_a && $engine_b) {
133 elseif ($engine_a && $engine_b) {
134 if (substr($a, 0, $engine_a) == substr($b, 0, $engine_b)) {
138 return ($a < $b) ? -1 : 1;
142 * Check to see if the specified command contains an 'allow-additional-options'
143 * record. If it does, find the additional options that are allowed, and
144 * add in the help text for the options of all of the sub-commands.
146 function _drush_help_merge_subcommand_information(&$command) {
147 // 'allow-additional-options' will either be FALSE (default),
148 // TRUE ("allow anything"), or an array that lists subcommands
149 // that are or may be called via drush_invoke by this command.
150 if (is_array($command['allow-additional-options'])) {
151 $implemented = drush_get_commands();
152 foreach ($command['allow-additional-options'] as $subcommand_name) {
153 if (array_key_exists($subcommand_name, $implemented)) {
154 $command['options'] += $implemented[$subcommand_name]['options'];
155 $command['sub-options'] = array_merge_recursive($command['sub-options'], $implemented[$subcommand_name]['sub-options']);
156 if (empty($command['arguments'])) {
157 $command['arguments'] = $implemented[$subcommand_name]['arguments'];
159 $command['topics'] = array_merge($command['topics'], $implemented[$subcommand_name]['topics']);
166 * Format one named help section from a command record
169 * A command record with help information
171 * The name of the section to format ('options', 'topic', etc.)
173 * Formatted rows, suitable for printing via drush_print_table. The returned
174 * array can be empty.
176 function drush_format_help_section($command, $section) {
178 $formatter = (function_exists('drush_help_section_formatter_' . $section)) ? 'drush_help_section_formatter_' . $section : 'drush_help_section_default_formatter';
179 foreach ($command[$section] as $name => $help_attributes) {
180 if (!is_array($help_attributes)) {
181 $help_attributes = array('description' => $help_attributes);
183 $help_attributes['label'] = $name;
184 call_user_func_array($formatter, array($command, &$help_attributes));
185 if (empty($help_attributes['hidden'])) {
186 $rows[] = array('label' => $help_attributes['label'], 'description' => $help_attributes['description']);
187 // Process the subsections too, if any
188 if (!empty($command['sub-' . $section]) && array_key_exists($name, $command['sub-' . $section])) {
189 $rows = array_merge($rows, _drush_format_help_subsection($command, $section, $name, $formatter));
197 * Format one named portion of a subsection from a command record.
198 * Subsections allow related parts of a help record to be grouped
199 * together. For example, in the 'options' section, sub-options that
200 * are related to a particular primary option are stored in a 'sub-options'
201 * section whose name == the name of the primary option.
204 * A command record with help information
206 * The name of the section to format ('options', 'topic', etc.)
208 * The name of the subsection (e.g. the name of the primary option)
210 * The name of a function to use to format the rows of the subsection
212 * Characters to prefix to the front of the label (for indentation)
214 * Formatted rows, suitable for printing via drush_print_table.
216 function _drush_format_help_subsection($command, $section, $subsection, $formatter, $prefix = ' ') {
218 foreach ($command['sub-' . $section][$subsection] as $name => $help_attributes) {
219 if (!is_array($help_attributes)) {
220 $help_attributes = array('description' => $help_attributes);
222 $help_attributes['label'] = $name;
223 call_user_func_array($formatter, array($command, &$help_attributes));
224 if (!array_key_exists('hidden', $help_attributes)) {
225 $rows[] = array('label' => $prefix . $help_attributes['label'], 'description' => $help_attributes['description']);
226 // Process the subsections too, if any
227 if (!empty($command['sub-' . $section]) && array_key_exists($name, $command['sub-' . $section])) {
228 $rows = array_merge($rows, _drush_format_help_subsection($command, $section, $name, $formatter, $prefix . ' '));
236 * The options section formatter. Adds a "--" in front of each
237 * item label. Also handles short-form and example-value
238 * components in the help attributes.
240 function drush_help_section_formatter_options($command, &$help_attributes) {
241 if ($help_attributes['label'][0] == '-') {
242 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');
245 $help_attributes['label'] = '--' . $help_attributes['label'];
247 if (!empty($help_attributes['required'])) {
248 $help_attributes['description'] .= " " . dt("Required.");
253 if (array_key_exists('example-value', $help_attributes)) {
254 if (isset($help_attributes['value']) && $help_attributes['value'] == 'optional') {
258 $help_attributes['label'] .= '=' . $prefix . $help_attributes['example-value'] . $suffix;
260 if (array_key_exists('short-form', $help_attributes)) {
261 $help_attributes['short-form'] .= " $prefix" . $help_attributes['example-value'] . $suffix;
264 if (array_key_exists('short-form', $help_attributes)) {
265 $help_attributes['label'] = '-' . $help_attributes['short-form'] . ', ' . $help_attributes['label'];
267 drush_help_section_default_formatter($command, $help_attributes);
271 * The default section formatter. Replaces '[command]' with the
274 function drush_help_section_default_formatter($command, &$help_attributes) {
275 // '[command]' is a token representing the current command. @see pm_drush_engine_version_control().
276 $help_attributes['label'] = str_replace('[command]', $command['command'], $help_attributes['label']);