Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / helpsingle.drush.inc
1 <?php
2
3 /**
4  * Implementation of hook_drush_command().
5  *
6  * In this hook, you specify which commands your
7  * drush module makes available, what it does and
8  * description.
9  *
10  * Notice how this structure closely resembles how
11  * you define menu hooks.
12  *
13  * @return
14  *   An associative array describing your command(s).
15  */
16 function helpsingle_drush_command() {
17   $items = array();
18   $items['helpsingle'] = array(
19     'description' => 'Print help for a single command',
20     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
21     'allow-additional-options' => TRUE,
22     'hidden' => TRUE,
23     'arguments' => array(
24       'command' => 'A command name, or command alias.',
25     ),
26     'examples' => array(
27       'drush help pm-download' => 'Show help for one command.',
28       'drush help dl' => 'Show help for one command using an alias.',
29     ),
30     'topics' => array('docs-readme'),
31   );
32   return $items;
33 }
34
35 /**
36  * Command callback. Show help for a single command.
37  */
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();
51   }
52   if (array_key_exists($commandstring, $commands)) {
53     $command = $commands[$commandstring];
54
55     annotationcommand_adapter_add_hook_options($command);
56
57     drush_print_help($command);
58     return TRUE;
59   }
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');
64     return TRUE;
65   }
66   return drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command !command.', array('!command' => $commandstring)));
67 }
68
69 /**
70  * Print the help for a single command to the screen.
71  *
72  * @param array $command
73  *   A fully loaded $command array.
74  */
75 function drush_print_help($command) {
76   _drush_help_merge_subcommand_information($command);
77
78   if (!$help = drush_command_invoke_all('drush_help', 'drush:'. $command['command'])) {
79     $help = array($command['description']);
80   }
81
82   if ($command['strict-option-handling']) {
83     $command['topics'][] = 'docs-strict-options';
84   }
85
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);
90
91   drush_print(wordwrap(implode("\n", $help), drush_get_context('DRUSH_COLUMNS', 80)));
92   drush_print();
93
94   $global_options = drush_get_global_options();
95   foreach ($command['global-options'] as $global_option) {
96     $command['options'][$global_option] = $global_options[$global_option];
97   }
98
99   // Sort command options.
100   uksort($command['options'], '_drush_help_sort_command_options');
101
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);
106       if ($rows) {
107         drush_print(dt($value) . ':');
108         drush_print_table($rows, FALSE, array('label' => 40));
109         unset($rows);
110         drush_print();
111       }
112     }
113   }
114
115   // Append aliases if any.
116   if ($command['aliases']) {
117     drush_print(dt("Aliases: ") . implode(', ', $command['aliases']));
118   }
119 }
120
121 /**
122  * Sort command options alphabetically. Engine options at the end.
123  */
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) {
128     return 1;
129   }
130   else if (!$engine_a && $engine_b) {
131     return -1;
132   }
133   elseif ($engine_a && $engine_b) {
134     if (substr($a, 0, $engine_a) == substr($b, 0, $engine_b)) {
135       return 0;
136     }
137   }
138   return ($a < $b) ? -1 : 1;
139 }
140
141 /**
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.
145  */
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'];
158         }
159         $command['topics'] = array_merge($command['topics'], $implemented[$subcommand_name]['topics']);
160       }
161     }
162   }
163 }
164
165 /**
166  * Format one named help section from a command record
167  *
168  * @param $command
169  *   A command record with help information
170  * @param $section
171  *   The name of the section to format ('options', 'topic', etc.)
172  * @returns array
173  *   Formatted rows, suitable for printing via drush_print_table. The returned
174  *   array can be empty.
175  */
176 function drush_format_help_section($command, $section) {
177   $rows = array();
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);
182     }
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));
190       }
191     }
192   }
193   return $rows;
194 }
195
196 /**
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.
202  *
203  * @param $command
204  *   A command record with help information
205  * @param $section
206  *   The name of the section to format ('options', 'topic', etc.)
207  * @param $subsection
208  *   The name of the subsection (e.g. the name of the primary option)
209  * @param $formatter
210  *   The name of a function to use to format the rows of the subsection
211  * @param $prefix
212  *   Characters to prefix to the front of the label (for indentation)
213  * @returns array
214  *   Formatted rows, suitable for printing via drush_print_table.
215  */
216 function _drush_format_help_subsection($command, $section, $subsection, $formatter, $prefix = '  ') {
217   $rows = array();
218   foreach ($command['sub-' . $section][$subsection] as $name => $help_attributes) {
219     if (!is_array($help_attributes)) {
220       $help_attributes = array('description' => $help_attributes);
221     }
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 . '  '));
229       }
230     }
231   }
232   return $rows;
233 }
234
235 /**
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.
239  */
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');
243   }
244   else {
245     $help_attributes['label'] = '--' . $help_attributes['label'];
246   }
247   if (!empty($help_attributes['required'])) {
248     $help_attributes['description'] .= " " . dt("Required.");
249   }
250
251   $prefix = '<';
252   $suffix = '>';
253   if (array_key_exists('example-value', $help_attributes)) {
254     if (isset($help_attributes['value']) && $help_attributes['value'] == 'optional') {
255       $prefix = '[';
256       $suffix = ']';
257     }
258     $help_attributes['label'] .= '=' . $prefix . $help_attributes['example-value'] . $suffix;
259
260     if (array_key_exists('short-form', $help_attributes)) {
261       $help_attributes['short-form'] .= " $prefix" . $help_attributes['example-value'] . $suffix;
262     }
263   }
264   if (array_key_exists('short-form', $help_attributes)) {
265     $help_attributes['label'] = '-' . $help_attributes['short-form'] . ', ' . $help_attributes['label'];
266   }
267   drush_help_section_default_formatter($command, $help_attributes);
268 }
269
270 /**
271  * The default section formatter.  Replaces '[command]' with the
272  * command name.
273  */
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']);
277 }