Drush: includes/complete.inc Source File

  1. 7.x doxygen/html/complete_8inc_source.html
  2. master doxygen/html/complete_8inc_source.html
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  *
6  * Provide completion output for shells.
7  *
8  * This is not called directly, but by shell completion scripts specific to
9  * each shell (bash, csh etc). These run whenever the user triggers completion,
10  * typically when pressing <tab>. The shell completion scripts should call
11  * "drush complete <text>", where <text> is the full command line, which we take
12  * as input and use to produce a list of possible completions for the
13  * current/next word, separated by newlines. Typically, when multiple
14  * completions are returned the shell will display them to the user in a concise
15  * format - but when a single completion is returned it will autocomplete.
16  *
17  * We provide completion for site aliases, commands, shell aliases, options,
18  * engines and arguments. Displaying all of these when the last word has no
19  * characters yet is not useful, as there are too many items. Instead we filter
20  * the possible completions based on position, in a similar way to git.
21  * For example:
22  * - We only display site aliases and commands if one is not already present.
23  * - We only display options if the user has already entered a hyphen.
24  * - We only display global options before a command is entered, and we only
25  * display command specific options after the command (Drush itself does not
26  * care about option placement, but this approach keeps things more concise).
27  *
28  * Below is typical output of complete in different situations. Tokens in square
29  * brackets are optional, and [word] will filter available options that start
30  * with the same characters, or display all listed options if empty.
31  * drush --[word] : Output global options
32  * drush [word] : Output site aliases, sites, commands and shell aliases
33  * drush [@alias] [word] : Output commands
34  * drush [@alias] command [word] : Output command specific arguments
35  * drush [@alias] command --[word] : Output command specific options
36  *
37  * Because the purpose of autocompletion is to make the command line more
38  * efficient for users we need to respond quickly with the list of completions.
39  * To do this, we call drush_complete() early in the Drush bootstrap, and
40  * implement a simple caching system.
41  *
42  * To generate the list of completions, we set up the Drush environment as if
43  * the command was called on it's own, parse the command using the standard
44  * Drush functions, bootstrap the site (if any) and collect available
45  * completions from various sources. Because this can be somewhat slow, we cache
46  * the results. The cache strategy aims to balance accuracy and responsiveness:
47  * - We cache per site, if a site is available.
48  * - We generate (and cache) everything except arguments at the same time, so
49  * subsequent completions on the site don't need any bootstrap.
50  * - We generate and cache arguments on-demand, since these can often be
51  * expensive to generate. Arguments are also cached per-site.
52  *
53  * For argument completions, commandfiles can implement
54  * COMMANDFILE_COMMAND_complete() returning an array containing a key 'values'
55  * containing an array of all possible argument completions for that command.
56  * For example, return array('values' => array('aardvark', 'aardwolf')) offers
57  * the words 'aardvark' and 'aardwolf', or will complete to 'aardwolf' if the
58  * letters 'aardw' are already present. Since command arguments are cached,
59  * commandfiles can bootstrap a site or perform other somewhat time consuming
60  * activities to retrieve the list of possible arguments. Commands can also
61  * clear the cache (or just the "arguments" cache for their command) when the
62  * completion results have likely changed - see drush_complete_cache_clear().
63  *
64  * Commandfiles can also return a special optional element in their array with
65  * the key 'files' that contains an array of patterns/flags for the glob()
66  * function. These are used to produce file and directory completions (the
67  * results of these are not cached, since this is a fast operation).
68  * See http://php.net/glob for details of valid patterns and flags.
69  * For example the following will complete the command arguments on all
70  * directories, as well as files ending in tar.gz:
71  * return array(
72  * 'files' => array(
73  * 'directories' => array(
74  * 'pattern' => '*',
75  * 'flags' => GLOB_ONLYDIR,
76  * ),
77  * 'tar' => array(
78  * 'pattern' => '*.tar.gz',
79  * ),
80  * ),
81  * );
82  *
83  * To check completion results without needing to actually trigger shell
84  * completion, you can call this manually using a command like:
85  *
86  * drush --early=includes/complete.inc [--complete-debug] drush [@alias] [command]...
87  *
88  * If you want to simulate the results of pressing tab after a space (i.e.
89  * and empty last word, include '' on the end of your command:
90  *
91  * drush --early=includes/complete.inc [--complete-debug] drush ''
92  */
93 
94 /**
95  * Produce autocomplete output.
96  *
97  * Determine position (is there a site-alias or command set, and are we trying
98  * to complete an option). Then produce a list of completions for the last word
99  * and output them separated by newlines.
100  */
102  // We use a distinct --complete-debug option to avoid unwanted debug messages
103  // being printed when users use this option for other purposes in the command
104  // they are trying to complete.
105  drush_set_option('debug', FALSE);
106  if (drush_get_option('complete-debug', FALSE)) {
107  drush_set_context('DRUSH_DEBUG', TRUE);
108  }
109  // Set up as if we were running the command, and attempt to parse.
110  $argv = drush_complete_process_argv();
111  if ($alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
112  $set_sitealias_name = $alias;
113  $set_sitealias = drush_sitealias_get_record($alias);
114  }
115 
116  // Arguments have now had site-aliases and options removed, so we take the
117  // first item as our command. We need to know if the command is valid, so that
118  // we know if we are supposed to complete an in-progress command name, or
119  // arguments for a command. We do this by checking against our per-site cache
120  // of command names (which will only bootstrap if the cache needs to be
121  // regenerated), rather than drush_parse_command() which always requires a
122  // site bootstrap.
123  $arguments = drush_get_arguments();
124  $set_command_name = NULL;
125  if (isset($arguments[0]) && in_array($arguments[0] . ' ', drush_complete_get('command-names'))) {
126  $set_command_name = $arguments[0];
127  }
128  // We unset the command if it is "help" but that is not explicitly found in
129  // args, since Drush sets the command to "help" if no command is specified,
130  // which prevents completion of global options.
131  if ($set_command_name == 'help' && !array_search('help', $argv)) {
132  $set_command_name = NULL;
133  }
134 
135  // Determine the word we are trying to complete, and if it is an option.
136  $last_word = end($argv);
137  $word_is_option = FALSE;
138  if (!empty($last_word) && $last_word[0] == '-') {
139  $word_is_option = TRUE;
140  $last_word = ltrim($last_word, '-');
141  }
142 
143  $completions = array();
144  if (!$set_command_name) {
145  // We have no command yet.
146  if ($word_is_option) {
147  // Include global option completions.
148  $completions += drush_hyphenate_options(drush_complete_match($last_word, drush_complete_get('options')));
149  }
150  else {
151  if (empty($set_sitealias_name)) {
152  // Include site alias completions.
153  $completions += drush_complete_match($last_word, drush_complete_get('site-aliases'));
154  }
155  // Include command completions.
156  $completions += drush_complete_match($last_word, drush_complete_get('command-names'));
157  }
158  }
159  else {
160  if ($last_word == $set_command_name) {
161  // The user just typed a valid command name, but we still do command
162  // completion, as there may be other commands that start with the detected
163  // command (e.g. "make" is a valid command, but so is "make-test").
164  // If there is only the single matching command, this will include in the
165  // completion list so they get a space inserted, confirming it is valid.
166  $completions += drush_complete_match($last_word, drush_complete_get('command-names'));
167  }
168  else if ($word_is_option) {
169  // Include command option completions.
170  $completions += drush_hyphenate_options(drush_complete_match($last_word, drush_complete_get('options', $set_command_name)));
171  }
172  else {
173  // Include command argument completions.
174  $argument_completion = drush_complete_get('arguments', $set_command_name);
175  if (isset($argument_completion['values'])) {
176  $completions += drush_complete_match($last_word, $argument_completion['values']);
177  }
178  if (isset($argument_completion['files'])) {
179  $completions += drush_complete_match_file($last_word, $argument_completion['files']);
180  }
181  }
182  }
183 
184  if (!empty($completions)) {
185  sort($completions);
186  return implode("\n", $completions);
187  }
188  return TRUE;
189 }
190 
191 /**
192  * This function resets the raw arguments so that Drush can parse the command as
193  * if it was run directly. The shell complete command passes the
194  * full command line as an argument, and the --early and --complete-debug
195  * options have to come before that, and the "drush" bash script will add a
196  * --php option on the end, so we end up with something like this:
197  *
198  * /path/to/drush.php --early=includes/complete.inc [--complete-debug] drush [@alias] [command]... --php=/usr/bin/php
199  *
200  * Note that "drush" occurs twice, and also that the second occurrence could be
201  * an alias, so we can't easily use it as to detect the start of the actual
202  * command. Hence our approach is to remove the initial "drush" and then any
203  * options directly following that - what remains is then the command we need
204  * to complete - i.e.:
205  *
206  * drush [@alias] [command]...
207  *
208  * Note that if completion is initiated following a space an empty argument is
209  * added to argv. So in that case argv looks something like this:
210  * array (
211  * '0' => '/path/to/drush.php',
212  * '1' => '--early=includes/complete.inc',
213  * '2' => 'drush',
214  * '3' => 'topic',
215  * '4' => '',
216  * '5' => '--php=/usr/bin/php',
217  * );
218  *
219  * @return $args
220  * Array of arguments (argv), excluding the initial command and options
221  * associated with the complete call.
222  * array (
223  * '0' => 'drush',
224  * '1' => 'topic',
225  * '2' => '',
226  * );
227  */
229  $argv = drush_get_context('argv');
230  // Remove the first argument, which will be the "drush" command.
231  array_shift($argv);
232  while (substr($arg = array_shift($argv), 0, 2) == '--') {
233  // We remove all options, until we get to a non option, which
234  // marks the start of the actual command we are trying to complete.
235  }
236  // Replace the initial argument.
237  array_unshift($argv, $arg);
238  // Remove the --php option at the end if exists (added by the "drush" shell
239  // script that is called when completion is requested).
240  if (substr(end($argv), 0, 6) == '--php=') {
241  array_pop($argv);
242  }
243  drush_set_context('argv', $argv);
244  drush_set_command(NULL);
245  // Reparse arguments, site alias, and command.
247  // Ensure the base environment is configured, so tests look in the correct
248  // places.
250  // Check for and record any site alias.
253 
254  // Return the new argv for easy reference.
255  return $argv;
256 }
257 
258 /**
259  * Retrieves the appropriate list of candidate completions, then filters this
260  * list using the last word that we are trying to complete.
261  *
262  * @param string $last_word
263  * The last word in the argument list (i.e. the subject of completion).
264  * @param array $values
265  * Array of possible completion values to filter.
266  *
267  * @return array
268  * Array of candidate completions that start with the same characters as the
269  * last word. If the last word is empty, return all candidates.
270  */
271 function drush_complete_match($last_word, $values) {
272  // Using preg_grep appears to be faster that strpos with array_filter/loop.
273  return preg_grep('/^' . preg_quote($last_word, '/') . '/', $values);
274 }
275 
276 /**
277  * Retrieves the appropriate list of candidate file/directory completions,
278  * filtered by the last word that we are trying to complete.
279  *
280  * @param string $last_word
281  * The last word in the argument list (i.e. the subject of completion).
282  * @param array $files
283  * Array of file specs, each with a pattern and flags subarray.
284  *
285  * @return array
286  * Array of candidate file/directory completions that start with the same
287  * characters as the last word. If the last word is empty, return all
288  * candidates.
289  */
290 function drush_complete_match_file($last_word, $files) {
291  $return = array();
292  if ($last_word[0] == '~') {
293  // Complete does not do tilde expansion, so we do it here.
294  // We shell out (unquoted) to expand the tilde.
295  drush_shell_exec('echo ' . $last_word);
296  return drush_shell_exec_output();
297  }
298 
299  $dir = '';
300  if (substr($last_word, -1) == '/' && is_dir($last_word)) {
301  // If we exactly match a trailing directory, then we use that as the base
302  // for the listing. We only do this if a trailing slash is present, since at
303  // this stage it is still possible there are other directories that start
304  // with this string.
305  $dir = $last_word;
306  }
307  else {
308  // Otherwise we discard the last part of the path (this is matched against
309  // the list later), and use that as our base.
310  $dir = dirname($last_word);
311  if (empty($dir) || $dir == '.' && $last_word != '.' && substr($last_word, 0, 2) != './') {
312  // We are looking at the current working directory, so unless the user is
313  // actually specifying a leading dot we leave the path empty.
314  $dir = '';
315  }
316  else {
317  // In all other cases we need to add a trailing slash.
318  $dir .= '/';
319  }
320  }
321 
322  foreach ($files as $spec) {
323  // We always include GLOB_MARK, as an easy way to detect directories.
324  $flags = GLOB_MARK;
325  if (isset($spec['flags'])) {
326  $flags = $spec['flags'] | GLOB_MARK;
327  }
328  $listing = glob($dir . $spec['pattern'], $flags);
329  $return = array_merge($return, drush_complete_match($last_word, $listing));
330  }
331  // If we are returning a single item (which will become part of the final
332  // command), we need to use the full path, and we need to escape it
333  // appropriately.
334  if (count($return) == 1) {
335  // Escape common shell metacharacters (we don't use escapeshellarg as it
336  // single quotes everything, even when unnecessary).
337  $item = array_pop($return);
338  $item = preg_replace('/[ |&;()<>]/', "\\\\$0", $item);
339  if (substr($item, -1) !== '/') {
340  // Insert a space after files, since the argument is complete.
341  $item = $item . ' ';
342  }
343  $return = array($item);
344  }
345  else {
346  $firstchar = TRUE;
347  if ($last_word[0] == '/') {
348  // If we are working with absolute paths, we need to check if the first
349  // character of all the completions matches. If it does, then we pass a
350  // full path for each match, so the shell completes as far as it can,
351  // matching the behaviour with relative paths.
352  $pos = strlen($last_word);
353  foreach ($return as $id => $item) {
354  if ($item[$pos] !== $return[0][$pos]) {
355  $firstchar = FALSE;
356  continue;
357  }
358  }
359  }
360  foreach ($return as $id => $item) {
361  // For directories we leave the path alone.
362  $slash_pos = strpos($last_word, '/');
363  if ($slash_pos === 0 && $firstchar) {
364  // With absolute paths where completions share initial characters, we
365  // pass in a resolved path.
366  $return[$id] = realpath($item);
367  }
368  else if ($slash_pos !== FALSE && $dir != './') {
369  // For files, we pass only the file name, ignoring the false match when
370  // the user is using a single dot relative path.
371  $return[$id] = basename($item);
372  }
373  }
374  }
375  return $return;
376 }
377 
378 /**
379  * Simple helper function to ensure options are properly hyphenated before we
380  * return them to the user (we match against the non-hyphenated versions
381  * internally).
382  *
383  * @param array $options
384  * Array of unhyphenated option names.
385  *
386  * @return array
387  * Array of hyphenated option names.
388  */
389 function drush_hyphenate_options($options) {
390  foreach ($options as $key => $option) {
391  $options[$key] = '--' . ltrim($option, '--');
392  }
393  return $options;
394 }
395 
396 /**
397  * Retrieves from cache, or generates a listing of completion candidates of a
398  * specific type (and optionally, command).
399  *
400  * @param string $type
401  * String indicating type of completions to return.
402  * See drush_complete_rebuild() for possible keys.
403  * @param string $command
404  * An optional command name if command specific completion is needed.
405  *
406  * @return array
407  * List of candidate completions.
408  */
409 function drush_complete_get($type, $command = NULL) {
410  static $complete;
411  if (empty($command)) {
412  // Quick return if we already have a complete static cache.
413  if (!empty($complete[$type])) {
414  return $complete[$type];
415  }
416  // Retrieve global items from a non-command specific cache, or rebuild cache
417  // if needed.
418  $cache = drush_cache_get(drush_complete_cache_cid($type), 'complete');
419  if (isset($cache->data)) {
420  return $cache->data;
421  }
422  $complete = drush_complete_rebuild();
423  return $complete[$type];
424  }
425  // Retrieve items from a command specific cache.
426  $cache = drush_cache_get(drush_complete_cache_cid($type, $command), 'complete');
427  if (isset($cache->data)) {
428  return $cache->data;
429  }
430  // Build argument cache - built only on demand.
431  if ($type == 'arguments') {
432  return drush_complete_rebuild_arguments($command);
433  }
434  // Rebuild cache of general command specific items.
435  if (empty($complete)) {
436  $complete = drush_complete_rebuild();
437  }
438  if (!empty($complete['commands'][$command][$type])) {
439  return $complete['commands'][$command][$type];
440  }
441  return array();
442 }
443 
444 /**
445  * Rebuild and cache completions for everything except command arguments.
446  *
447  * @return array
448  * Structured array of completion types, commands and candidate completions.
449  */
451  $complete = array();
452  // Bootstrap to the site level (if possible) - commands may need to check
453  // the bootstrap level, and perhaps bootstrap higher in extraordinary cases.
455  $commands = drush_get_commands();
456  foreach ($commands as $command_name => $command) {
457  // Add command options and suboptions.
458  $options = array_keys($command['options']);
459  foreach ($command['sub-options'] as $option => $sub_options) {
460  $options = array_merge($options, array_keys($sub_options));
461  }
462  $complete['commands'][$command_name]['options'] = $options;
463  }
464  // We treat shell aliases as commands for the purposes of completion.
465  $complete['command-names'] = array_merge(array_keys($commands), array_keys(drush_get_context('shell-aliases', array())));
466  $site_aliases = _drush_sitealias_all_list();
467  // TODO: Figure out where this dummy @0 alias is introduced.
468  unset($site_aliases['@0']);
469  $complete['site-aliases'] = array_keys($site_aliases);
470  $complete['options'] = array_keys(drush_get_global_options());
471 
472  // We add a space following all completes. Eventually there may be some
473  // items (e.g. options that we know need values) where we don't add a space.
474  array_walk_recursive($complete, 'drush_complete_trailing_space');
475  drush_complete_cache_set($complete);
476  return $complete;
477 }
478 
479 /**
480  * Helper callback function that adds a trailing space to completes in an array.
481  */
482 function drush_complete_trailing_space(&$item, $key) {
483  if (!is_array($item)) {
484  $item = (string)$item . ' ';
485  }
486 }
487 
488 /**
489  * Rebuild and cache completions for command arguments.
490  *
491  * @param string $command
492  * A specific command to retrieve and cache arguments for.
493  *
494  * @return array
495  * Structured array of candidate completion arguments, keyed by the command.
496  */
498  // Bootstrap to the site level (if possible) - commands may need to check
499  // the bootstrap level, and perhaps bootstrap higher in extraordinary cases.
501  $commands = drush_get_commands();
502  $hook = str_replace("-", "_", $commands[$command]['command-hook']);
503  $result = drush_command_invoke_all($hook . '_complete');
504  if (isset($result['values'])) {
505  // We add a space following all completes. Eventually there may be some
506  // items (e.g. comma separated arguments) where we don't add a space.
507  array_walk($result['values'], 'drush_complete_trailing_space');
508  }
509 
510  $complete = array(
511  'commands' => array(
512  $command => array(
513  'arguments' => $result,
514  )
515  )
516  );
517  drush_complete_cache_set($complete);
518  return $complete['commands'][$command]['arguments'];
519 }
520 
521 /**
522  * Stores caches for completions.
523  *
524  * @param $complete
525  * A structured array of completions, keyed by type, including a 'commands'
526  * type that contains all commands with command specific completions keyed by
527  * type. The array does not need to include all types - used by
529  */
530 function drush_complete_cache_set($complete) {
531  foreach ($complete as $type => $values) {
532  if ($type == 'commands') {
533  foreach ($values as $command_name => $command) {
534  foreach ($command as $command_type => $command_values) {
535  drush_cache_set(drush_complete_cache_cid($command_type, $command_name), $command_values, 'complete', DRUSH_CACHE_TEMPORARY);
536  }
537  }
538  }
539  else {
540  drush_cache_set(drush_complete_cache_cid($type), $values, 'complete', DRUSH_CACHE_TEMPORARY);
541  }
542  }
543 }
544 
545 /**
546  * Generate a cache id.
547  *
548  * @param $type
549  * The completion type.
550  * @param $command
551  * The command name (optional), if completions are command specific.
552  *
553  * @return string
554  * Cache id.
555  */
556 function drush_complete_cache_cid($type, $command = NULL) {
557  // For per-site caches, we include the site root and uri/path in the cache id
558  // hash. These are quick to determine, and prevents a bootstrap to site just
559  // to get a validated root and URI. Because these are not validated, there is
560  // the possibility of cache misses/ but they should be rare, since sites are
561  // normally referred to the same way (e.g. a site alias, or using the current
562  // directory), at least within a single command completion session.
563  // We also static cache them, since we may get differing results after
564  // bootstrap, which prevents the caches from being found on the next call.
565  static $root, $site;
566  if (empty($root)) {
567  $root = drush_get_option(array('r', 'root'), drush_locate_root());
568  $site = drush_get_option(array('l', 'uri'), drush_site_path());
569  }
570  return drush_get_cid('complete', array(), array($type, $command, $root, $site));
571 }

File

doxygen/html/complete_8inc_source.html
View source
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Drush: includes/complete.inc Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">Drush
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.1.2 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_09e761304027c904456130627fd4dcf5.html">includes</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">complete.inc</div>  </div>
</div><!--header-->
<div class="contents">
<a href="complete_8inc.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;&lt;?php</div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * @file</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * Provide completion output for shells.</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> * This is not called directly, but by shell completion scripts specific to</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * each shell (bash, csh etc). These run whenever the user triggers completion,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * typically when pressing &lt;tab&gt;. The shell completion scripts should call</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * &quot;drush complete &lt;text&gt;&quot;, where &lt;text&gt; is the full command line, which we take</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * as input and use to produce a list of possible completions for the</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> * current/next word, separated by newlines. Typically, when multiple</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * completions are returned the shell will display them to the user in a concise</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * format - but when a single completion is returned it will autocomplete.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * We provide completion for site aliases, commands, shell aliases, options,</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> * engines and arguments. Displaying all of these when the last word has no</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="comment"> * characters yet is not useful, as there are too many items. Instead we filter</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="comment"> * the possible completions based on position, in a similar way to git.</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="comment"> * For example:</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="comment"> * - We only display site aliases and commands if one is not already present.</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="comment"> * - We only display options if the user has already entered a hyphen.</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="comment"> * - We only display global options before a command is entered, and we only</span></div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="comment"> *   display command specific options after the command (Drush itself does not</span></div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="comment"> *   care about option placement, but this approach keeps things more concise).</span></div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="comment"> * Below is typical output of complete in different situations. Tokens in square</span></div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="comment"> * brackets are optional, and [word] will filter available options that start</span></div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="comment"> * with the same characters, or display all listed options if empty.</span></div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;<span class="comment"> * drush --[word] : Output global options</span></div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="comment"> * drush [word] : Output site aliases, sites, commands and shell aliases</span></div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;<span class="comment"> * drush [@alias] [word] : Output commands</span></div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="comment"> * drush [@alias] command [word] : Output command specific arguments</span></div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;<span class="comment"> * drush [@alias] command --[word] : Output command specific options</span></div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;<span class="comment"> * Because the purpose of autocompletion is to make the command line more</span></div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;<span class="comment"> * efficient for users we need to respond quickly with the list of completions.</span></div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;<span class="comment"> * To do this, we call drush_complete() early in the Drush bootstrap, and</span></div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;<span class="comment"> * implement a simple caching system.</span></div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="comment"> * To generate the list of completions, we set up the Drush environment as if</span></div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="comment"> * the command was called on it&#39;s own, parse the command using the standard</span></div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;<span class="comment"> * Drush functions, bootstrap the site (if any) and collect available</span></div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;<span class="comment"> * completions from various sources. Because this can be somewhat slow, we cache</span></div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;<span class="comment"> * the results. The cache strategy aims to balance accuracy and responsiveness:</span></div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;<span class="comment"> * - We cache per site, if a site is available.</span></div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;<span class="comment"> * - We generate (and cache) everything except arguments at the same time, so</span></div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;<span class="comment"> *   subsequent completions on the site don&#39;t need any bootstrap.</span></div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;<span class="comment"> * - We generate and cache arguments on-demand, since these can often be</span></div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;<span class="comment"> *   expensive to generate. Arguments are also cached per-site.</span></div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;<span class="comment"> * For argument completions, commandfiles can implement</span></div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;<span class="comment"> * COMMANDFILE_COMMAND_complete() returning an array containing a key &#39;values&#39;</span></div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;<span class="comment"> * containing an array of all possible argument completions for that command.</span></div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;<span class="comment"> * For example, return array(&#39;values&#39; =&gt; array(&#39;aardvark&#39;, &#39;aardwolf&#39;)) offers</span></div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;<span class="comment"> * the words &#39;aardvark&#39; and &#39;aardwolf&#39;, or will complete to &#39;aardwolf&#39; if the</span></div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;<span class="comment"> * letters &#39;aardw&#39; are already present. Since command arguments are cached,</span></div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;<span class="comment"> * commandfiles can bootstrap a site or perform other somewhat time consuming</span></div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;<span class="comment"> * activities to retrieve the list of possible arguments. Commands can also</span></div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;<span class="comment"> * clear the cache (or just the &quot;arguments&quot; cache for their command) when the</span></div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;<span class="comment"> * completion results have likely changed - see drush_complete_cache_clear().</span></div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;<span class="comment"> * Commandfiles can also return a special optional element in their array with</span></div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;<span class="comment"> * the key &#39;files&#39; that contains an array of patterns/flags for the glob()</span></div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="comment"> * function. These are used to produce file and directory completions (the</span></div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;<span class="comment"> * results of these are not cached, since this is a fast operation).</span></div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;<span class="comment"> * See http://php.net/glob for details of valid patterns and flags.</span></div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;<span class="comment"> * For example the following will complete the command arguments on all</span></div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;<span class="comment"> * directories, as well as files ending in tar.gz:</span></div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;<span class="comment"> *   return array(</span></div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;<span class="comment"> *         &#39;files&#39; =&gt; array(</span></div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;<span class="comment"> *           &#39;directories&#39; =&gt; array(</span></div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;<span class="comment"> *             &#39;pattern&#39; =&gt; &#39;*&#39;,</span></div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;<span class="comment"> *             &#39;flags&#39; =&gt; GLOB_ONLYDIR,</span></div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;<span class="comment"> *           ),</span></div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;<span class="comment"> *           &#39;tar&#39; =&gt; array(</span></div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;<span class="comment"> *             &#39;pattern&#39; =&gt; &#39;*.tar.gz&#39;,</span></div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;<span class="comment"> *           ),</span></div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;<span class="comment"> *         ),</span></div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;<span class="comment"> *       );</span></div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;<span class="comment"> * To check completion results without needing to actually trigger shell</span></div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;<span class="comment"> * completion, you can call this manually using a command like:</span></div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;<span class="comment"> * drush --early=includes/complete.inc [--complete-debug] drush [@alias] [command]...</span></div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;<span class="comment"> * If you want to simulate the results of pressing tab after a space (i.e.</span></div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;<span class="comment"> * and empty last word, include &#39;&#39; on the end of your command:</span></div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;<span class="comment"> * drush --early=includes/complete.inc [--complete-debug] drush &#39;&#39;</span></div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;<span class="comment"> * Produce autocomplete output.</span></div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;<span class="comment"> * Determine position (is there a site-alias or command set, and are we trying</span></div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;<span class="comment"> * to complete an option). Then produce a list of completions for the last word</span></div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;<span class="comment"> * and output them separated by newlines.</span></div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00101"></a><span class="lineno"><a class="code" href="complete_8inc.html#adbec292adabdfe3772d4ede574ab91c9">  101</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#adbec292adabdfe3772d4ede574ab91c9">drush_early_complete</a>() {</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;  <span class="comment">// We use a distinct --complete-debug option to avoid unwanted debug messages</span></div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;  <span class="comment">// being printed when users use this option for other purposes in the command</span></div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;  <span class="comment">// they are trying to complete.</span></div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;  <a class="code" href="context_8inc.html#aa4b55e2f6174c8b6efaa8f23a0f9e0af">drush_set_option</a>(<span class="stringliteral">&#39;debug&#39;</span>, FALSE);</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;  <span class="keywordflow">if</span> (<a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(<span class="stringliteral">&#39;complete-debug&#39;</span>, FALSE)) {</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;    <a class="code" href="context_8inc.html#af983acfe90e7d7bbdbd67c57f93708ba">drush_set_context</a>(<span class="stringliteral">&#39;DRUSH_DEBUG&#39;</span>, TRUE);</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;  }</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;  <span class="comment">// Set up as if we were running the command, and attempt to parse.</span></div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;  $argv = <a class="code" href="complete_8inc.html#a9249f2a38d16e008506147243482b39c">drush_complete_process_argv</a>();</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  <span class="keywordflow">if</span> ($alias = <a class="code" href="context_8inc.html#ad25559f8d7d753f636fbeac347c29fc5">drush_get_context</a>(<span class="stringliteral">&#39;DRUSH_TARGET_SITE_ALIAS&#39;</span>)) {</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;    $set_sitealias_name = $alias;</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;    $set_sitealias = <a class="code" href="sitealias_8inc.html#ac2f57b80441374522c4e4cdc3b7e0c2b">drush_sitealias_get_record</a>($alias);</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;  }</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;  <span class="comment">// Arguments have now had site-aliases and options removed, so we take the</span></div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;  <span class="comment">// first item as our command. We need to know if the command is valid, so that</span></div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;  <span class="comment">// we know if we are supposed to complete an in-progress command name, or</span></div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;  <span class="comment">// arguments for a command. We do this by checking against our per-site cache</span></div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;  <span class="comment">// of command names (which will only bootstrap if the cache needs to be</span></div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;  <span class="comment">// regenerated), rather than drush_parse_command() which always requires a</span></div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;  <span class="comment">// site bootstrap.</span></div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;  $arguments = <a class="code" href="context_8inc.html#afaf9361f1297f29d10402b075985985a">drush_get_arguments</a>();</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;  $set_command_name = NULL;</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;  <span class="keywordflow">if</span> (isset($arguments[0]) &amp;&amp; in_array($arguments[0] . <span class="charliteral">&#39; &#39;</span>, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;command-names&#39;</span>))) {</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;    $set_command_name = $arguments[0];</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;  }</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;  <span class="comment">// We unset the command if it is &quot;help&quot; but that is not explicitly found in</span></div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;  <span class="comment">// args, since Drush sets the command to &quot;help&quot; if no command is specified,</span></div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;  <span class="comment">// which prevents completion of global options.</span></div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;  <span class="keywordflow">if</span> ($set_command_name == <span class="stringliteral">&#39;help&#39;</span> &amp;&amp; !array_search(<span class="stringliteral">&#39;help&#39;</span>, $argv)) {</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;    $set_command_name = NULL;</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;  }</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;  <span class="comment">// Determine the word we are trying to complete, and if it is an option.</span></div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;  $last_word = end($argv);</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;  $word_is_option = FALSE;</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;  <span class="keywordflow">if</span> (!empty($last_word) &amp;&amp; $last_word[0] == <span class="charliteral">&#39;-&#39;</span>) {</div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;    $word_is_option = TRUE;</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;    $last_word = ltrim($last_word, <span class="charliteral">&#39;-&#39;</span>);</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;  }</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;  $completions = array();</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;  <span class="keywordflow">if</span> (!$set_command_name) {</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;    <span class="comment">// We have no command yet.</span></div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;    <span class="keywordflow">if</span> ($word_is_option) {</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;      <span class="comment">// Include global option completions.</span></div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;      $completions += <a class="code" href="complete_8inc.html#a39836b96551005ba8cf4ec0f4df3d603">drush_hyphenate_options</a>(<a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;options&#39;</span>)));</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;    }</div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;      <span class="keywordflow">if</span> (empty($set_sitealias_name)) {</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;        <span class="comment">// Include site alias completions.</span></div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;        $completions += <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;site-aliases&#39;</span>));</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;      }</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;      <span class="comment">// Include command completions.</span></div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;      $completions += <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;command-names&#39;</span>));</div>
<div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;    }</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;  }</div>
<div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;  <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;    <span class="keywordflow">if</span> ($last_word == $set_command_name) {</div>
<div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;      <span class="comment">// The user just typed a valid command name, but we still do command</span></div>
<div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;      <span class="comment">// completion, as there may be other commands that start with the detected</span></div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;      <span class="comment">// command (e.g. &quot;make&quot; is a valid command, but so is &quot;make-test&quot;).</span></div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;      <span class="comment">// If there is only the single matching command, this will include in the</span></div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;      <span class="comment">// completion list so they get a space inserted, confirming it is valid.</span></div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;      $completions += <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;command-names&#39;</span>));</div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;    }</div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;    <span class="keywordflow">else</span> <span class="keywordflow">if</span> ($word_is_option) {</div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;      <span class="comment">// Include command option completions.</span></div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;      $completions += <a class="code" href="complete_8inc.html#a39836b96551005ba8cf4ec0f4df3d603">drush_hyphenate_options</a>(<a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;options&#39;</span>, $set_command_name)));</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;    }</div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;      <span class="comment">// Include command argument completions.</span></div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;      $argument_completion = <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>(<span class="stringliteral">&#39;arguments&#39;</span>, $set_command_name);</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;      <span class="keywordflow">if</span> (isset($argument_completion[<span class="stringliteral">&#39;values&#39;</span>])) {</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;        $completions += <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, $argument_completion[<span class="stringliteral">&#39;values&#39;</span>]);</div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;      }</div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;      <span class="keywordflow">if</span> (isset($argument_completion[<span class="stringliteral">&#39;files&#39;</span>])) {</div>
<div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;        $completions += <a class="code" href="complete_8inc.html#a7d912e55f6aa1d293bcacdd6568eeaf1">drush_complete_match_file</a>($last_word, $argument_completion[<span class="stringliteral">&#39;files&#39;</span>]);</div>
<div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;      }</div>
<div class="line"><a name="l00181"></a><span class="lineno">  181</span>&#160;    }</div>
<div class="line"><a name="l00182"></a><span class="lineno">  182</span>&#160;  }</div>
<div class="line"><a name="l00183"></a><span class="lineno">  183</span>&#160;</div>
<div class="line"><a name="l00184"></a><span class="lineno">  184</span>&#160;  <span class="keywordflow">if</span> (!empty($completions)) {</div>
<div class="line"><a name="l00185"></a><span class="lineno">  185</span>&#160;    sort($completions);</div>
<div class="line"><a name="l00186"></a><span class="lineno">  186</span>&#160;    <span class="keywordflow">return</span> implode(<span class="stringliteral">&quot;\n&quot;</span>, $completions);</div>
<div class="line"><a name="l00187"></a><span class="lineno">  187</span>&#160;  }</div>
<div class="line"><a name="l00188"></a><span class="lineno">  188</span>&#160;  <span class="keywordflow">return</span> TRUE;</div>
<div class="line"><a name="l00189"></a><span class="lineno">  189</span>&#160;}</div>
<div class="line"><a name="l00190"></a><span class="lineno">  190</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00191"></a><span class="lineno">  191</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;<span class="comment"> * This function resets the raw arguments so that Drush can parse the command as</span></div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;<span class="comment"> * if it was run directly. The shell complete command passes the</span></div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;<span class="comment"> * full command line as an argument, and the --early and --complete-debug</span></div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;<span class="comment"> * options have to come before that, and the &quot;drush&quot; bash script will add a</span></div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;<span class="comment"> * --php option on the end, so we end up with something like this:</span></div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;<span class="comment"> * /path/to/drush.php --early=includes/complete.inc [--complete-debug] drush [@alias] [command]... --php=/usr/bin/php</span></div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00200"></a><span class="lineno">  200</span>&#160;<span class="comment"> * Note that &quot;drush&quot; occurs twice, and also that the second occurrence could be</span></div>
<div class="line"><a name="l00201"></a><span class="lineno">  201</span>&#160;<span class="comment"> * an alias, so we can&#39;t easily use it as to detect the start of the actual</span></div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;<span class="comment"> * command. Hence our approach is to remove the initial &quot;drush&quot; and then any</span></div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;<span class="comment"> * options directly following that - what remains is then the command we need</span></div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;<span class="comment"> * to complete - i.e.:</span></div>
<div class="line"><a name="l00205"></a><span class="lineno">  205</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00206"></a><span class="lineno">  206</span>&#160;<span class="comment"> * drush [@alias] [command]...</span></div>
<div class="line"><a name="l00207"></a><span class="lineno">  207</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00208"></a><span class="lineno">  208</span>&#160;<span class="comment"> * Note that if completion is initiated following a space an empty argument is</span></div>
<div class="line"><a name="l00209"></a><span class="lineno">  209</span>&#160;<span class="comment"> * added to argv. So in that case argv looks something like this:</span></div>
<div class="line"><a name="l00210"></a><span class="lineno">  210</span>&#160;<span class="comment"> * array (</span></div>
<div class="line"><a name="l00211"></a><span class="lineno">  211</span>&#160;<span class="comment"> *  &#39;0&#39; =&gt; &#39;/path/to/drush.php&#39;,</span></div>
<div class="line"><a name="l00212"></a><span class="lineno">  212</span>&#160;<span class="comment"> *  &#39;1&#39; =&gt; &#39;--early=includes/complete.inc&#39;,</span></div>
<div class="line"><a name="l00213"></a><span class="lineno">  213</span>&#160;<span class="comment"> *  &#39;2&#39; =&gt; &#39;drush&#39;,</span></div>
<div class="line"><a name="l00214"></a><span class="lineno">  214</span>&#160;<span class="comment"> *  &#39;3&#39; =&gt; &#39;topic&#39;,</span></div>
<div class="line"><a name="l00215"></a><span class="lineno">  215</span>&#160;<span class="comment"> *  &#39;4&#39; =&gt; &#39;&#39;,</span></div>
<div class="line"><a name="l00216"></a><span class="lineno">  216</span>&#160;<span class="comment"> *  &#39;5&#39; =&gt; &#39;--php=/usr/bin/php&#39;,</span></div>
<div class="line"><a name="l00217"></a><span class="lineno">  217</span>&#160;<span class="comment"> * );</span></div>
<div class="line"><a name="l00218"></a><span class="lineno">  218</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00219"></a><span class="lineno">  219</span>&#160;<span class="comment"> * @return $args</span></div>
<div class="line"><a name="l00220"></a><span class="lineno">  220</span>&#160;<span class="comment"> *   Array of arguments (argv), excluding the initial command and options</span></div>
<div class="line"><a name="l00221"></a><span class="lineno">  221</span>&#160;<span class="comment"> *   associated with the complete call.</span></div>
<div class="line"><a name="l00222"></a><span class="lineno">  222</span>&#160;<span class="comment"> *   array (</span></div>
<div class="line"><a name="l00223"></a><span class="lineno">  223</span>&#160;<span class="comment"> *    &#39;0&#39; =&gt; &#39;drush&#39;,</span></div>
<div class="line"><a name="l00224"></a><span class="lineno">  224</span>&#160;<span class="comment"> *    &#39;1&#39; =&gt; &#39;topic&#39;,</span></div>
<div class="line"><a name="l00225"></a><span class="lineno">  225</span>&#160;<span class="comment"> *    &#39;2&#39; =&gt; &#39;&#39;,</span></div>
<div class="line"><a name="l00226"></a><span class="lineno">  226</span>&#160;<span class="comment"> *   );</span></div>
<div class="line"><a name="l00227"></a><span class="lineno">  227</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00228"></a><span class="lineno"><a class="code" href="complete_8inc.html#a9249f2a38d16e008506147243482b39c">  228</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a9249f2a38d16e008506147243482b39c">drush_complete_process_argv</a>() {</div>
<div class="line"><a name="l00229"></a><span class="lineno">  229</span>&#160;  $argv = <a class="code" href="context_8inc.html#ad25559f8d7d753f636fbeac347c29fc5">drush_get_context</a>(<span class="stringliteral">&#39;argv&#39;</span>);</div>
<div class="line"><a name="l00230"></a><span class="lineno">  230</span>&#160;  <span class="comment">// Remove the first argument, which will be the &quot;drush&quot; command.</span></div>
<div class="line"><a name="l00231"></a><span class="lineno">  231</span>&#160;  array_shift($argv);</div>
<div class="line"><a name="l00232"></a><span class="lineno">  232</span>&#160;  <span class="keywordflow">while</span> (substr($arg = array_shift($argv), 0, 2) == <span class="stringliteral">&#39;--&#39;</span>) {</div>
<div class="line"><a name="l00233"></a><span class="lineno">  233</span>&#160;    <span class="comment">// We remove all options, until we get to a non option, which</span></div>
<div class="line"><a name="l00234"></a><span class="lineno">  234</span>&#160;    <span class="comment">// marks the start of the actual command we are trying to complete.</span></div>
<div class="line"><a name="l00235"></a><span class="lineno">  235</span>&#160;  }</div>
<div class="line"><a name="l00236"></a><span class="lineno">  236</span>&#160;  <span class="comment">// Replace the initial argument.</span></div>
<div class="line"><a name="l00237"></a><span class="lineno">  237</span>&#160;  array_unshift($argv, $arg);</div>
<div class="line"><a name="l00238"></a><span class="lineno">  238</span>&#160;  <span class="comment">// Remove the --php option at the end if exists (added by the &quot;drush&quot; shell</span></div>
<div class="line"><a name="l00239"></a><span class="lineno">  239</span>&#160;  <span class="comment">// script that is called when completion is requested).</span></div>
<div class="line"><a name="l00240"></a><span class="lineno">  240</span>&#160;  <span class="keywordflow">if</span> (substr(end($argv), 0, 6) == <span class="stringliteral">&#39;--php=&#39;</span>) {</div>
<div class="line"><a name="l00241"></a><span class="lineno">  241</span>&#160;    array_pop($argv);</div>
<div class="line"><a name="l00242"></a><span class="lineno">  242</span>&#160;  }</div>
<div class="line"><a name="l00243"></a><span class="lineno">  243</span>&#160;  <a class="code" href="context_8inc.html#af983acfe90e7d7bbdbd67c57f93708ba">drush_set_context</a>(<span class="stringliteral">&#39;argv&#39;</span>, $argv);</div>
<div class="line"><a name="l00244"></a><span class="lineno">  244</span>&#160;  <a class="code" href="context_8inc.html#af37b8df12a8fc3bf25b173d420a11203">drush_set_command</a>(NULL);</div>
<div class="line"><a name="l00245"></a><span class="lineno">  245</span>&#160;  <span class="comment">// Reparse arguments, site alias, and command.</span></div>
<div class="line"><a name="l00246"></a><span class="lineno">  246</span>&#160;  <a class="code" href="includes_2command_8inc.html#a5c7d0d064d4cb56bfe2b2311e5643e11">drush_parse_args</a>();</div>
<div class="line"><a name="l00247"></a><span class="lineno">  247</span>&#160;  <span class="comment">// Ensure the base environment is configured, so tests look in the correct</span></div>
<div class="line"><a name="l00248"></a><span class="lineno">  248</span>&#160;  <span class="comment">// places.</span></div>
<div class="line"><a name="l00249"></a><span class="lineno">  249</span>&#160;  <a class="code" href="preflight_8inc.html#a5e1ac7503465559379713fc92ce232dd">_drush_preflight_base_environment</a>();</div>
<div class="line"><a name="l00250"></a><span class="lineno">  250</span>&#160;  <span class="comment">// Check for and record any site alias.</span></div>
<div class="line"><a name="l00251"></a><span class="lineno">  251</span>&#160;  <a class="code" href="sitealias_8inc.html#aea0a6ddf95752b4958fc14698a8db017">drush_sitealias_check_arg</a>();</div>
<div class="line"><a name="l00252"></a><span class="lineno">  252</span>&#160;  drush_sitealias_check_site_env();</div>
<div class="line"><a name="l00253"></a><span class="lineno">  253</span>&#160;</div>
<div class="line"><a name="l00254"></a><span class="lineno">  254</span>&#160;  <span class="comment">// Return the new argv for easy reference.</span></div>
<div class="line"><a name="l00255"></a><span class="lineno">  255</span>&#160;  <span class="keywordflow">return</span> $argv;</div>
<div class="line"><a name="l00256"></a><span class="lineno">  256</span>&#160;}</div>
<div class="line"><a name="l00257"></a><span class="lineno">  257</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00258"></a><span class="lineno">  258</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00259"></a><span class="lineno">  259</span>&#160;<span class="comment"> * Retrieves the appropriate list of candidate completions, then filters this</span></div>
<div class="line"><a name="l00260"></a><span class="lineno">  260</span>&#160;<span class="comment"> * list using the last word that we are trying to complete.</span></div>
<div class="line"><a name="l00261"></a><span class="lineno">  261</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00262"></a><span class="lineno">  262</span>&#160;<span class="comment"> * @param string $last_word</span></div>
<div class="line"><a name="l00263"></a><span class="lineno">  263</span>&#160;<span class="comment"> *   The last word in the argument list (i.e. the subject of completion).</span></div>
<div class="line"><a name="l00264"></a><span class="lineno">  264</span>&#160;<span class="comment"> * @param array $values</span></div>
<div class="line"><a name="l00265"></a><span class="lineno">  265</span>&#160;<span class="comment"> *   Array of possible completion values to filter.</span></div>
<div class="line"><a name="l00266"></a><span class="lineno">  266</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00267"></a><span class="lineno">  267</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00268"></a><span class="lineno">  268</span>&#160;<span class="comment"> *   Array of candidate completions that start with the same characters as the</span></div>
<div class="line"><a name="l00269"></a><span class="lineno">  269</span>&#160;<span class="comment"> *   last word. If the last word is empty, return all candidates.</span></div>
<div class="line"><a name="l00270"></a><span class="lineno">  270</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00271"></a><span class="lineno"><a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">  271</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, $values) {</div>
<div class="line"><a name="l00272"></a><span class="lineno">  272</span>&#160;  <span class="comment">// Using preg_grep appears to be faster that strpos with array_filter/loop.</span></div>
<div class="line"><a name="l00273"></a><span class="lineno">  273</span>&#160;  <span class="keywordflow">return</span> preg_grep(<span class="stringliteral">&#39;/^&#39;</span> . preg_quote($last_word, <span class="charliteral">&#39;/&#39;</span>) . <span class="charliteral">&#39;/&#39;</span>, $values);</div>
<div class="line"><a name="l00274"></a><span class="lineno">  274</span>&#160;}</div>
<div class="line"><a name="l00275"></a><span class="lineno">  275</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00276"></a><span class="lineno">  276</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00277"></a><span class="lineno">  277</span>&#160;<span class="comment"> * Retrieves the appropriate list of candidate file/directory completions,</span></div>
<div class="line"><a name="l00278"></a><span class="lineno">  278</span>&#160;<span class="comment"> * filtered by the last word that we are trying to complete.</span></div>
<div class="line"><a name="l00279"></a><span class="lineno">  279</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00280"></a><span class="lineno">  280</span>&#160;<span class="comment"> * @param string $last_word</span></div>
<div class="line"><a name="l00281"></a><span class="lineno">  281</span>&#160;<span class="comment"> *   The last word in the argument list (i.e. the subject of completion).</span></div>
<div class="line"><a name="l00282"></a><span class="lineno">  282</span>&#160;<span class="comment"> * @param array $files</span></div>
<div class="line"><a name="l00283"></a><span class="lineno">  283</span>&#160;<span class="comment"> *   Array of file specs, each with a pattern and flags subarray.</span></div>
<div class="line"><a name="l00284"></a><span class="lineno">  284</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00285"></a><span class="lineno">  285</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00286"></a><span class="lineno">  286</span>&#160;<span class="comment"> *   Array of candidate file/directory completions that start with the same</span></div>
<div class="line"><a name="l00287"></a><span class="lineno">  287</span>&#160;<span class="comment"> *   characters as the last word. If the last word is empty, return all</span></div>
<div class="line"><a name="l00288"></a><span class="lineno">  288</span>&#160;<span class="comment"> *   candidates.</span></div>
<div class="line"><a name="l00289"></a><span class="lineno">  289</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00290"></a><span class="lineno"><a class="code" href="complete_8inc.html#a7d912e55f6aa1d293bcacdd6568eeaf1">  290</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a7d912e55f6aa1d293bcacdd6568eeaf1">drush_complete_match_file</a>($last_word, $files) {</div>
<div class="line"><a name="l00291"></a><span class="lineno">  291</span>&#160;  $return = array();</div>
<div class="line"><a name="l00292"></a><span class="lineno">  292</span>&#160;  <span class="keywordflow">if</span> ($last_word[0] == <span class="charliteral">&#39;~&#39;</span>) {</div>
<div class="line"><a name="l00293"></a><span class="lineno">  293</span>&#160;    <span class="comment">// Complete does not do tilde expansion, so we do it here.</span></div>
<div class="line"><a name="l00294"></a><span class="lineno">  294</span>&#160;    <span class="comment">// We shell out (unquoted) to expand the tilde.</span></div>
<div class="line"><a name="l00295"></a><span class="lineno">  295</span>&#160;    <a class="code" href="group__commandwrappers.html#gafd358ceac1286dbdd141ff2b8eb7d556">drush_shell_exec</a>(<span class="stringliteral">&#39;echo &#39;</span> . $last_word);</div>
<div class="line"><a name="l00296"></a><span class="lineno">  296</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="group__commandwrappers.html#gadba8c59ba260dd39f615bc22ca78e1af">drush_shell_exec_output</a>();</div>
<div class="line"><a name="l00297"></a><span class="lineno">  297</span>&#160;  }</div>
<div class="line"><a name="l00298"></a><span class="lineno">  298</span>&#160;</div>
<div class="line"><a name="l00299"></a><span class="lineno">  299</span>&#160;  $dir = <span class="stringliteral">&#39;&#39;</span>;</div>
<div class="line"><a name="l00300"></a><span class="lineno">  300</span>&#160;  <span class="keywordflow">if</span> (substr($last_word, -1) == <span class="charliteral">&#39;/&#39;</span> &amp;&amp; is_dir($last_word)) {</div>
<div class="line"><a name="l00301"></a><span class="lineno">  301</span>&#160;    <span class="comment">// If we exactly match a trailing directory, then we use that as the base</span></div>
<div class="line"><a name="l00302"></a><span class="lineno">  302</span>&#160;    <span class="comment">// for the listing. We only do this if a trailing slash is present, since at</span></div>
<div class="line"><a name="l00303"></a><span class="lineno">  303</span>&#160;    <span class="comment">// this stage it is still possible there are other directories that start</span></div>
<div class="line"><a name="l00304"></a><span class="lineno">  304</span>&#160;    <span class="comment">// with this string.</span></div>
<div class="line"><a name="l00305"></a><span class="lineno">  305</span>&#160;    $dir = $last_word;</div>
<div class="line"><a name="l00306"></a><span class="lineno">  306</span>&#160;  }</div>
<div class="line"><a name="l00307"></a><span class="lineno">  307</span>&#160;  <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00308"></a><span class="lineno">  308</span>&#160;    <span class="comment">// Otherwise we discard the last part of the path (this is matched against</span></div>
<div class="line"><a name="l00309"></a><span class="lineno">  309</span>&#160;    <span class="comment">// the list later), and use that as our base.</span></div>
<div class="line"><a name="l00310"></a><span class="lineno">  310</span>&#160;    $dir = dirname($last_word);</div>
<div class="line"><a name="l00311"></a><span class="lineno">  311</span>&#160;    <span class="keywordflow">if</span> (empty($dir) || $dir == <span class="charliteral">&#39;.&#39;</span> &amp;&amp; $last_word != <span class="charliteral">&#39;.&#39;</span> &amp;&amp; substr($last_word, 0, 2) != <span class="stringliteral">&#39;./&#39;</span>) {</div>
<div class="line"><a name="l00312"></a><span class="lineno">  312</span>&#160;      <span class="comment">// We are looking at the current working directory, so unless the user is</span></div>
<div class="line"><a name="l00313"></a><span class="lineno">  313</span>&#160;      <span class="comment">// actually specifying a leading dot we leave the path empty.</span></div>
<div class="line"><a name="l00314"></a><span class="lineno">  314</span>&#160;      $dir = <span class="stringliteral">&#39;&#39;</span>;</div>
<div class="line"><a name="l00315"></a><span class="lineno">  315</span>&#160;    }</div>
<div class="line"><a name="l00316"></a><span class="lineno">  316</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00317"></a><span class="lineno">  317</span>&#160;      <span class="comment">// In all other cases we need to add a trailing slash.</span></div>
<div class="line"><a name="l00318"></a><span class="lineno">  318</span>&#160;      $dir .= <span class="charliteral">&#39;/&#39;</span>;</div>
<div class="line"><a name="l00319"></a><span class="lineno">  319</span>&#160;    }</div>
<div class="line"><a name="l00320"></a><span class="lineno">  320</span>&#160;  }</div>
<div class="line"><a name="l00321"></a><span class="lineno">  321</span>&#160;</div>
<div class="line"><a name="l00322"></a><span class="lineno">  322</span>&#160;  <span class="keywordflow">foreach</span> ($files as $spec) {</div>
<div class="line"><a name="l00323"></a><span class="lineno">  323</span>&#160;    <span class="comment">// We always include GLOB_MARK, as an easy way to detect directories.</span></div>
<div class="line"><a name="l00324"></a><span class="lineno">  324</span>&#160;    $flags = GLOB_MARK;</div>
<div class="line"><a name="l00325"></a><span class="lineno">  325</span>&#160;    <span class="keywordflow">if</span> (isset($spec[<span class="stringliteral">&#39;flags&#39;</span>])) {</div>
<div class="line"><a name="l00326"></a><span class="lineno">  326</span>&#160;      $flags = $spec[<span class="stringliteral">&#39;flags&#39;</span>] | GLOB_MARK;</div>
<div class="line"><a name="l00327"></a><span class="lineno">  327</span>&#160;    }</div>
<div class="line"><a name="l00328"></a><span class="lineno">  328</span>&#160;    $listing = glob($dir . $spec[<span class="stringliteral">&#39;pattern&#39;</span>], $flags);</div>
<div class="line"><a name="l00329"></a><span class="lineno">  329</span>&#160;    $return = array_merge($return, <a class="code" href="complete_8inc.html#abb270b35c688f2a2d842b954ab92bd51">drush_complete_match</a>($last_word, $listing));</div>
<div class="line"><a name="l00330"></a><span class="lineno">  330</span>&#160;  }</div>
<div class="line"><a name="l00331"></a><span class="lineno">  331</span>&#160;  <span class="comment">// If we are returning a single item (which will become part of the final</span></div>
<div class="line"><a name="l00332"></a><span class="lineno">  332</span>&#160;  <span class="comment">// command), we need to use the full path, and we need to escape it</span></div>
<div class="line"><a name="l00333"></a><span class="lineno">  333</span>&#160;  <span class="comment">// appropriately.</span></div>
<div class="line"><a name="l00334"></a><span class="lineno">  334</span>&#160;  <span class="keywordflow">if</span> (count($return) == 1) {</div>
<div class="line"><a name="l00335"></a><span class="lineno">  335</span>&#160;    <span class="comment">// Escape common shell metacharacters (we don&#39;t use escapeshellarg as it</span></div>
<div class="line"><a name="l00336"></a><span class="lineno">  336</span>&#160;    <span class="comment">// single quotes everything, even when unnecessary).</span></div>
<div class="line"><a name="l00337"></a><span class="lineno">  337</span>&#160;    $item = array_pop($return);</div>
<div class="line"><a name="l00338"></a><span class="lineno">  338</span>&#160;    $item = preg_replace(<span class="stringliteral">&#39;/[ |&amp;;()&lt;&gt;]/&#39;</span>, <span class="stringliteral">&quot;\\\\$0&quot;</span>, $item);</div>
<div class="line"><a name="l00339"></a><span class="lineno">  339</span>&#160;    <span class="keywordflow">if</span> (substr($item, -1) !== <span class="charliteral">&#39;/&#39;</span>) {</div>
<div class="line"><a name="l00340"></a><span class="lineno">  340</span>&#160;      <span class="comment">// Insert a space after files, since the argument is complete.</span></div>
<div class="line"><a name="l00341"></a><span class="lineno">  341</span>&#160;      $item = $item . <span class="charliteral">&#39; &#39;</span>;</div>
<div class="line"><a name="l00342"></a><span class="lineno">  342</span>&#160;    }</div>
<div class="line"><a name="l00343"></a><span class="lineno">  343</span>&#160;    $return = array($item);</div>
<div class="line"><a name="l00344"></a><span class="lineno">  344</span>&#160;  }</div>
<div class="line"><a name="l00345"></a><span class="lineno">  345</span>&#160;  <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00346"></a><span class="lineno">  346</span>&#160;    $firstchar = TRUE;</div>
<div class="line"><a name="l00347"></a><span class="lineno">  347</span>&#160;    <span class="keywordflow">if</span> ($last_word[0] == <span class="charliteral">&#39;/&#39;</span>) {</div>
<div class="line"><a name="l00348"></a><span class="lineno">  348</span>&#160;      <span class="comment">// If we are working with absolute paths, we need to check if the first</span></div>
<div class="line"><a name="l00349"></a><span class="lineno">  349</span>&#160;      <span class="comment">// character of all the completions matches. If it does, then we pass a</span></div>
<div class="line"><a name="l00350"></a><span class="lineno">  350</span>&#160;      <span class="comment">// full path for each match, so the shell completes as far as it can,</span></div>
<div class="line"><a name="l00351"></a><span class="lineno">  351</span>&#160;      <span class="comment">// matching the behaviour with relative paths.</span></div>
<div class="line"><a name="l00352"></a><span class="lineno">  352</span>&#160;      $pos = strlen($last_word);</div>
<div class="line"><a name="l00353"></a><span class="lineno">  353</span>&#160;      <span class="keywordflow">foreach</span> ($return as $id =&gt; $item) {</div>
<div class="line"><a name="l00354"></a><span class="lineno">  354</span>&#160;        <span class="keywordflow">if</span> ($item[$pos] !== $return[0][$pos]) {</div>
<div class="line"><a name="l00355"></a><span class="lineno">  355</span>&#160;          $firstchar = FALSE;</div>
<div class="line"><a name="l00356"></a><span class="lineno">  356</span>&#160;          <span class="keywordflow">continue</span>;</div>
<div class="line"><a name="l00357"></a><span class="lineno">  357</span>&#160;        }</div>
<div class="line"><a name="l00358"></a><span class="lineno">  358</span>&#160;      }</div>
<div class="line"><a name="l00359"></a><span class="lineno">  359</span>&#160;    }</div>
<div class="line"><a name="l00360"></a><span class="lineno">  360</span>&#160;    <span class="keywordflow">foreach</span> ($return as $id =&gt; $item) {</div>
<div class="line"><a name="l00361"></a><span class="lineno">  361</span>&#160;      <span class="comment">// For directories we leave the path alone.</span></div>
<div class="line"><a name="l00362"></a><span class="lineno">  362</span>&#160;      $slash_pos = strpos($last_word, <span class="charliteral">&#39;/&#39;</span>);</div>
<div class="line"><a name="l00363"></a><span class="lineno">  363</span>&#160;      <span class="keywordflow">if</span> ($slash_pos === 0 &amp;&amp; $firstchar) {</div>
<div class="line"><a name="l00364"></a><span class="lineno">  364</span>&#160;        <span class="comment">// With absolute paths where completions share initial characters, we</span></div>
<div class="line"><a name="l00365"></a><span class="lineno">  365</span>&#160;        <span class="comment">// pass in a resolved path.</span></div>
<div class="line"><a name="l00366"></a><span class="lineno">  366</span>&#160;        $return[$id] = realpath($item);</div>
<div class="line"><a name="l00367"></a><span class="lineno">  367</span>&#160;      }</div>
<div class="line"><a name="l00368"></a><span class="lineno">  368</span>&#160;      <span class="keywordflow">else</span> <span class="keywordflow">if</span> ($slash_pos !== FALSE &amp;&amp; $dir != <span class="stringliteral">&#39;./&#39;</span>) {</div>
<div class="line"><a name="l00369"></a><span class="lineno">  369</span>&#160;        <span class="comment">// For files, we pass only the file name, ignoring the false match when</span></div>
<div class="line"><a name="l00370"></a><span class="lineno">  370</span>&#160;        <span class="comment">// the user is using a single dot relative path.</span></div>
<div class="line"><a name="l00371"></a><span class="lineno">  371</span>&#160;        $return[$id] = basename($item);</div>
<div class="line"><a name="l00372"></a><span class="lineno">  372</span>&#160;      }</div>
<div class="line"><a name="l00373"></a><span class="lineno">  373</span>&#160;    }</div>
<div class="line"><a name="l00374"></a><span class="lineno">  374</span>&#160;  }</div>
<div class="line"><a name="l00375"></a><span class="lineno">  375</span>&#160;  <span class="keywordflow">return</span> $return;</div>
<div class="line"><a name="l00376"></a><span class="lineno">  376</span>&#160;}</div>
<div class="line"><a name="l00377"></a><span class="lineno">  377</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00378"></a><span class="lineno">  378</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00379"></a><span class="lineno">  379</span>&#160;<span class="comment"> * Simple helper function to ensure options are properly hyphenated before we</span></div>
<div class="line"><a name="l00380"></a><span class="lineno">  380</span>&#160;<span class="comment"> * return them to the user (we match against the non-hyphenated versions</span></div>
<div class="line"><a name="l00381"></a><span class="lineno">  381</span>&#160;<span class="comment"> * internally).</span></div>
<div class="line"><a name="l00382"></a><span class="lineno">  382</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00383"></a><span class="lineno">  383</span>&#160;<span class="comment"> * @param array $options</span></div>
<div class="line"><a name="l00384"></a><span class="lineno">  384</span>&#160;<span class="comment"> *   Array of unhyphenated option names.</span></div>
<div class="line"><a name="l00385"></a><span class="lineno">  385</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00386"></a><span class="lineno">  386</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00387"></a><span class="lineno">  387</span>&#160;<span class="comment"> *   Array of hyphenated option names.</span></div>
<div class="line"><a name="l00388"></a><span class="lineno">  388</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00389"></a><span class="lineno"><a class="code" href="complete_8inc.html#a39836b96551005ba8cf4ec0f4df3d603">  389</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a39836b96551005ba8cf4ec0f4df3d603">drush_hyphenate_options</a>($options) {</div>
<div class="line"><a name="l00390"></a><span class="lineno">  390</span>&#160;  <span class="keywordflow">foreach</span> ($options as $key =&gt; $option) {</div>
<div class="line"><a name="l00391"></a><span class="lineno">  391</span>&#160;    $options[$key] = <span class="stringliteral">&#39;--&#39;</span> . ltrim($option, <span class="stringliteral">&#39;--&#39;</span>);</div>
<div class="line"><a name="l00392"></a><span class="lineno">  392</span>&#160;  }</div>
<div class="line"><a name="l00393"></a><span class="lineno">  393</span>&#160;  <span class="keywordflow">return</span> $options;</div>
<div class="line"><a name="l00394"></a><span class="lineno">  394</span>&#160;}</div>
<div class="line"><a name="l00395"></a><span class="lineno">  395</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00396"></a><span class="lineno">  396</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00397"></a><span class="lineno">  397</span>&#160;<span class="comment"> * Retrieves from cache, or generates a listing of completion candidates of a</span></div>
<div class="line"><a name="l00398"></a><span class="lineno">  398</span>&#160;<span class="comment"> * specific type (and optionally, command).</span></div>
<div class="line"><a name="l00399"></a><span class="lineno">  399</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00400"></a><span class="lineno">  400</span>&#160;<span class="comment"> * @param string $type</span></div>
<div class="line"><a name="l00401"></a><span class="lineno">  401</span>&#160;<span class="comment"> *   String indicating type of completions to return.</span></div>
<div class="line"><a name="l00402"></a><span class="lineno">  402</span>&#160;<span class="comment"> *   See drush_complete_rebuild() for possible keys.</span></div>
<div class="line"><a name="l00403"></a><span class="lineno">  403</span>&#160;<span class="comment"> * @param string $command</span></div>
<div class="line"><a name="l00404"></a><span class="lineno">  404</span>&#160;<span class="comment"> *   An optional command name if command specific completion is needed.</span></div>
<div class="line"><a name="l00405"></a><span class="lineno">  405</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00406"></a><span class="lineno">  406</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00407"></a><span class="lineno">  407</span>&#160;<span class="comment"> *   List of candidate completions.</span></div>
<div class="line"><a name="l00408"></a><span class="lineno">  408</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00409"></a><span class="lineno"><a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">  409</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#ac5c1cad8930371b383f35f1baf65692e">drush_complete_get</a>($type, $command = NULL) {</div>
<div class="line"><a name="l00410"></a><span class="lineno">  410</span>&#160;  <span class="keyword">static</span> $complete;</div>
<div class="line"><a name="l00411"></a><span class="lineno">  411</span>&#160;  <span class="keywordflow">if</span> (empty($command)) {</div>
<div class="line"><a name="l00412"></a><span class="lineno">  412</span>&#160;    <span class="comment">// Quick return if we already have a complete static cache.</span></div>
<div class="line"><a name="l00413"></a><span class="lineno">  413</span>&#160;    <span class="keywordflow">if</span> (!empty($complete[$type])) {</div>
<div class="line"><a name="l00414"></a><span class="lineno">  414</span>&#160;      <span class="keywordflow">return</span> $complete[$type];</div>
<div class="line"><a name="l00415"></a><span class="lineno">  415</span>&#160;    }</div>
<div class="line"><a name="l00416"></a><span class="lineno">  416</span>&#160;    <span class="comment">// Retrieve global items from a non-command specific cache, or rebuild cache</span></div>
<div class="line"><a name="l00417"></a><span class="lineno">  417</span>&#160;    <span class="comment">// if needed.</span></div>
<div class="line"><a name="l00418"></a><span class="lineno">  418</span>&#160;    $cache = <a class="code" href="includes_2cache_8inc.html#adc36ac3b562c67cbf1d7aab89c029694">drush_cache_get</a>(<a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">drush_complete_cache_cid</a>($type), <span class="stringliteral">&#39;complete&#39;</span>);</div>
<div class="line"><a name="l00419"></a><span class="lineno">  419</span>&#160;    <span class="keywordflow">if</span> (isset($cache-&gt;data)) {</div>
<div class="line"><a name="l00420"></a><span class="lineno">  420</span>&#160;      <span class="keywordflow">return</span> $cache-&gt;data;</div>
<div class="line"><a name="l00421"></a><span class="lineno">  421</span>&#160;    }</div>
<div class="line"><a name="l00422"></a><span class="lineno">  422</span>&#160;    $complete = <a class="code" href="complete_8inc.html#af377cea6e3311628f37a738698e1fb4c">drush_complete_rebuild</a>();</div>
<div class="line"><a name="l00423"></a><span class="lineno">  423</span>&#160;    <span class="keywordflow">return</span> $complete[$type];</div>
<div class="line"><a name="l00424"></a><span class="lineno">  424</span>&#160;  }</div>
<div class="line"><a name="l00425"></a><span class="lineno">  425</span>&#160;  <span class="comment">// Retrieve items from a command specific cache.</span></div>
<div class="line"><a name="l00426"></a><span class="lineno">  426</span>&#160;  $cache = <a class="code" href="includes_2cache_8inc.html#adc36ac3b562c67cbf1d7aab89c029694">drush_cache_get</a>(<a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">drush_complete_cache_cid</a>($type, $command), <span class="stringliteral">&#39;complete&#39;</span>);</div>
<div class="line"><a name="l00427"></a><span class="lineno">  427</span>&#160;  <span class="keywordflow">if</span> (isset($cache-&gt;data)) {</div>
<div class="line"><a name="l00428"></a><span class="lineno">  428</span>&#160;    <span class="keywordflow">return</span> $cache-&gt;data;</div>
<div class="line"><a name="l00429"></a><span class="lineno">  429</span>&#160;  }</div>
<div class="line"><a name="l00430"></a><span class="lineno">  430</span>&#160;  <span class="comment">// Build argument cache - built only on demand.</span></div>
<div class="line"><a name="l00431"></a><span class="lineno">  431</span>&#160;  <span class="keywordflow">if</span> ($type == <span class="stringliteral">&#39;arguments&#39;</span>) {</div>
<div class="line"><a name="l00432"></a><span class="lineno">  432</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="complete_8inc.html#a4bbb8faee318ca6f3b8d70710d0406ba">drush_complete_rebuild_arguments</a>($command);</div>
<div class="line"><a name="l00433"></a><span class="lineno">  433</span>&#160;  }</div>
<div class="line"><a name="l00434"></a><span class="lineno">  434</span>&#160;  <span class="comment">// Rebuild cache of general command specific items.</span></div>
<div class="line"><a name="l00435"></a><span class="lineno">  435</span>&#160;  <span class="keywordflow">if</span> (empty($complete)) {</div>
<div class="line"><a name="l00436"></a><span class="lineno">  436</span>&#160;    $complete = <a class="code" href="complete_8inc.html#af377cea6e3311628f37a738698e1fb4c">drush_complete_rebuild</a>();</div>
<div class="line"><a name="l00437"></a><span class="lineno">  437</span>&#160;  }</div>
<div class="line"><a name="l00438"></a><span class="lineno">  438</span>&#160;  <span class="keywordflow">if</span> (!empty($complete[<span class="stringliteral">&#39;commands&#39;</span>][$command][$type])) {</div>
<div class="line"><a name="l00439"></a><span class="lineno">  439</span>&#160;    <span class="keywordflow">return</span> $complete[<span class="stringliteral">&#39;commands&#39;</span>][$command][$type];</div>
<div class="line"><a name="l00440"></a><span class="lineno">  440</span>&#160;  }</div>
<div class="line"><a name="l00441"></a><span class="lineno">  441</span>&#160;  <span class="keywordflow">return</span> array();</div>
<div class="line"><a name="l00442"></a><span class="lineno">  442</span>&#160;}</div>
<div class="line"><a name="l00443"></a><span class="lineno">  443</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00444"></a><span class="lineno">  444</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00445"></a><span class="lineno">  445</span>&#160;<span class="comment"> * Rebuild and cache completions for everything except command arguments.</span></div>
<div class="line"><a name="l00446"></a><span class="lineno">  446</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00447"></a><span class="lineno">  447</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00448"></a><span class="lineno">  448</span>&#160;<span class="comment"> *   Structured array of completion types, commands and candidate completions.</span></div>
<div class="line"><a name="l00449"></a><span class="lineno">  449</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00450"></a><span class="lineno"><a class="code" href="complete_8inc.html#af377cea6e3311628f37a738698e1fb4c">  450</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#af377cea6e3311628f37a738698e1fb4c">drush_complete_rebuild</a>() {</div>
<div class="line"><a name="l00451"></a><span class="lineno">  451</span>&#160;  $complete = array();</div>
<div class="line"><a name="l00452"></a><span class="lineno">  452</span>&#160;  <span class="comment">// Bootstrap to the site level (if possible) - commands may need to check</span></div>
<div class="line"><a name="l00453"></a><span class="lineno">  453</span>&#160;  <span class="comment">// the bootstrap level, and perhaps bootstrap higher in extraordinary cases.</span></div>
<div class="line"><a name="l00454"></a><span class="lineno">  454</span>&#160;  <a class="code" href="lib_2Drush_2Boot_2bootstrap_8inc.html#adb325e52092d83c685acdee827af1085">drush_bootstrap_max</a>(<a class="code" href="lib_2Drush_2Boot_2bootstrap_8inc.html#a2a8b6da6ee211bee51b64b4aed18db72">DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION</a>);</div>
<div class="line"><a name="l00455"></a><span class="lineno">  455</span>&#160;  $commands = <a class="code" href="includes_2command_8inc.html#af0c377881e5c9c772f0cad80ea016452">drush_get_commands</a>();</div>
<div class="line"><a name="l00456"></a><span class="lineno">  456</span>&#160;  <span class="keywordflow">foreach</span> ($commands as $command_name =&gt; $command) {</div>
<div class="line"><a name="l00457"></a><span class="lineno">  457</span>&#160;    <span class="comment">// Add command options and suboptions.</span></div>
<div class="line"><a name="l00458"></a><span class="lineno">  458</span>&#160;    $options = array_keys($command[<span class="stringliteral">&#39;options&#39;</span>]);</div>
<div class="line"><a name="l00459"></a><span class="lineno">  459</span>&#160;    <span class="keywordflow">foreach</span> ($command[<span class="stringliteral">&#39;sub-options&#39;</span>] as $option =&gt; $sub_options) {</div>
<div class="line"><a name="l00460"></a><span class="lineno">  460</span>&#160;      $options = array_merge($options, array_keys($sub_options));</div>
<div class="line"><a name="l00461"></a><span class="lineno">  461</span>&#160;    }</div>
<div class="line"><a name="l00462"></a><span class="lineno">  462</span>&#160;    $complete[<span class="stringliteral">&#39;commands&#39;</span>][$command_name][<span class="stringliteral">&#39;options&#39;</span>] = $options;</div>
<div class="line"><a name="l00463"></a><span class="lineno">  463</span>&#160;  }</div>
<div class="line"><a name="l00464"></a><span class="lineno">  464</span>&#160;  <span class="comment">// We treat shell aliases as commands for the purposes of completion.</span></div>
<div class="line"><a name="l00465"></a><span class="lineno">  465</span>&#160;  $complete[<span class="stringliteral">&#39;command-names&#39;</span>] = array_merge(array_keys($commands), array_keys(<a class="code" href="context_8inc.html#ad25559f8d7d753f636fbeac347c29fc5">drush_get_context</a>(<span class="stringliteral">&#39;shell-aliases&#39;</span>, array())));</div>
<div class="line"><a name="l00466"></a><span class="lineno">  466</span>&#160;  $site_aliases = <a class="code" href="sitealias_8drush_8inc.html#ab84f25812ecd477686e6fa349503ed8d">_drush_sitealias_all_list</a>();</div>
<div class="line"><a name="l00467"></a><span class="lineno">  467</span>&#160;  <span class="comment">// TODO: Figure out where this dummy @0 alias is introduced.</span></div>
<div class="line"><a name="l00468"></a><span class="lineno">  468</span>&#160;  unset($site_aliases[<span class="stringliteral">&#39;@0&#39;</span>]);</div>
<div class="line"><a name="l00469"></a><span class="lineno">  469</span>&#160;  $complete[<span class="stringliteral">&#39;site-aliases&#39;</span>] = array_keys($site_aliases);</div>
<div class="line"><a name="l00470"></a><span class="lineno">  470</span>&#160;  $complete[<span class="stringliteral">&#39;options&#39;</span>] = array_keys(<a class="code" href="drush_8inc.html#acb0a26e79bcbdd1152d59a6916a35aae">drush_get_global_options</a>());</div>
<div class="line"><a name="l00471"></a><span class="lineno">  471</span>&#160;</div>
<div class="line"><a name="l00472"></a><span class="lineno">  472</span>&#160;  <span class="comment">// We add a space following all completes. Eventually there may be some</span></div>
<div class="line"><a name="l00473"></a><span class="lineno">  473</span>&#160;  <span class="comment">// items (e.g. options that we know need values) where we don&#39;t add a space.</span></div>
<div class="line"><a name="l00474"></a><span class="lineno">  474</span>&#160;  array_walk_recursive($complete, <span class="stringliteral">&#39;drush_complete_trailing_space&#39;</span>);</div>
<div class="line"><a name="l00475"></a><span class="lineno">  475</span>&#160;  <a class="code" href="complete_8inc.html#a520dd6cb68367ab70b710431d76c85df">drush_complete_cache_set</a>($complete);</div>
<div class="line"><a name="l00476"></a><span class="lineno">  476</span>&#160;  <span class="keywordflow">return</span> $complete;</div>
<div class="line"><a name="l00477"></a><span class="lineno">  477</span>&#160;}</div>
<div class="line"><a name="l00478"></a><span class="lineno">  478</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00479"></a><span class="lineno">  479</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00480"></a><span class="lineno">  480</span>&#160;<span class="comment"> * Helper callback function that adds a trailing space to completes in an array.</span></div>
<div class="line"><a name="l00481"></a><span class="lineno">  481</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00482"></a><span class="lineno"><a class="code" href="complete_8inc.html#ac4f7cc5aff0b9d5f2fd8b956152ed424">  482</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#ac4f7cc5aff0b9d5f2fd8b956152ed424">drush_complete_trailing_space</a>(&amp;$item, $key) {</div>
<div class="line"><a name="l00483"></a><span class="lineno">  483</span>&#160;  <span class="keywordflow">if</span> (!is_array($item)) {</div>
<div class="line"><a name="l00484"></a><span class="lineno">  484</span>&#160;    $item = (string)$item . <span class="charliteral">&#39; &#39;</span>;</div>
<div class="line"><a name="l00485"></a><span class="lineno">  485</span>&#160;  }</div>
<div class="line"><a name="l00486"></a><span class="lineno">  486</span>&#160;}</div>
<div class="line"><a name="l00487"></a><span class="lineno">  487</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00488"></a><span class="lineno">  488</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00489"></a><span class="lineno">  489</span>&#160;<span class="comment"> * Rebuild and cache completions for command arguments.</span></div>
<div class="line"><a name="l00490"></a><span class="lineno">  490</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00491"></a><span class="lineno">  491</span>&#160;<span class="comment"> * @param string $command</span></div>
<div class="line"><a name="l00492"></a><span class="lineno">  492</span>&#160;<span class="comment"> *   A specific command to retrieve and cache arguments for.</span></div>
<div class="line"><a name="l00493"></a><span class="lineno">  493</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00494"></a><span class="lineno">  494</span>&#160;<span class="comment"> * @return array</span></div>
<div class="line"><a name="l00495"></a><span class="lineno">  495</span>&#160;<span class="comment"> *   Structured array of candidate completion arguments, keyed by the command.</span></div>
<div class="line"><a name="l00496"></a><span class="lineno">  496</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00497"></a><span class="lineno"><a class="code" href="complete_8inc.html#a4bbb8faee318ca6f3b8d70710d0406ba">  497</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a4bbb8faee318ca6f3b8d70710d0406ba">drush_complete_rebuild_arguments</a>($command) {</div>
<div class="line"><a name="l00498"></a><span class="lineno">  498</span>&#160;  <span class="comment">// Bootstrap to the site level (if possible) - commands may need to check</span></div>
<div class="line"><a name="l00499"></a><span class="lineno">  499</span>&#160;  <span class="comment">// the bootstrap level, and perhaps bootstrap higher in extraordinary cases.</span></div>
<div class="line"><a name="l00500"></a><span class="lineno">  500</span>&#160;  <a class="code" href="lib_2Drush_2Boot_2bootstrap_8inc.html#adb325e52092d83c685acdee827af1085">drush_bootstrap_max</a>(<a class="code" href="lib_2Drush_2Boot_2bootstrap_8inc.html#aef87695d12c5885f75e515f200c079a8">DRUSH_BOOTSTRAP_DRUPAL_SITE</a>);</div>
<div class="line"><a name="l00501"></a><span class="lineno">  501</span>&#160;  $commands = <a class="code" href="includes_2command_8inc.html#af0c377881e5c9c772f0cad80ea016452">drush_get_commands</a>();</div>
<div class="line"><a name="l00502"></a><span class="lineno">  502</span>&#160;  $hook = str_replace(<span class="stringliteral">&quot;-&quot;</span>, <span class="stringliteral">&quot;_&quot;</span>, $commands[$command][<span class="stringliteral">&#39;command-hook&#39;</span>]);</div>
<div class="line"><a name="l00503"></a><span class="lineno">  503</span>&#160;  $result = <a class="code" href="includes_2command_8inc.html#a19eba9666aba673a64fbd78a2c5ef4f9">drush_command_invoke_all</a>($hook . <span class="stringliteral">&#39;_complete&#39;</span>);</div>
<div class="line"><a name="l00504"></a><span class="lineno">  504</span>&#160;  <span class="keywordflow">if</span> (isset($result[<span class="stringliteral">&#39;values&#39;</span>])) {</div>
<div class="line"><a name="l00505"></a><span class="lineno">  505</span>&#160;    <span class="comment">// We add a space following all completes. Eventually there may be some</span></div>
<div class="line"><a name="l00506"></a><span class="lineno">  506</span>&#160;    <span class="comment">// items (e.g. comma separated arguments) where we don&#39;t add a space.</span></div>
<div class="line"><a name="l00507"></a><span class="lineno">  507</span>&#160;    array_walk($result[<span class="stringliteral">&#39;values&#39;</span>], <span class="stringliteral">&#39;drush_complete_trailing_space&#39;</span>);</div>
<div class="line"><a name="l00508"></a><span class="lineno">  508</span>&#160;  }</div>
<div class="line"><a name="l00509"></a><span class="lineno">  509</span>&#160;</div>
<div class="line"><a name="l00510"></a><span class="lineno">  510</span>&#160;  $complete = array(</div>
<div class="line"><a name="l00511"></a><span class="lineno">  511</span>&#160;    <span class="stringliteral">&#39;commands&#39;</span> =&gt; array(</div>
<div class="line"><a name="l00512"></a><span class="lineno">  512</span>&#160;      $command =&gt; array(</div>
<div class="line"><a name="l00513"></a><span class="lineno">  513</span>&#160;        <span class="stringliteral">&#39;arguments&#39;</span> =&gt; $result,</div>
<div class="line"><a name="l00514"></a><span class="lineno">  514</span>&#160;      )</div>
<div class="line"><a name="l00515"></a><span class="lineno">  515</span>&#160;    )</div>
<div class="line"><a name="l00516"></a><span class="lineno">  516</span>&#160;  );</div>
<div class="line"><a name="l00517"></a><span class="lineno">  517</span>&#160;  <a class="code" href="complete_8inc.html#a520dd6cb68367ab70b710431d76c85df">drush_complete_cache_set</a>($complete);</div>
<div class="line"><a name="l00518"></a><span class="lineno">  518</span>&#160;  <span class="keywordflow">return</span> $complete[<span class="stringliteral">&#39;commands&#39;</span>][$command][<span class="stringliteral">&#39;arguments&#39;</span>];</div>
<div class="line"><a name="l00519"></a><span class="lineno">  519</span>&#160;}</div>
<div class="line"><a name="l00520"></a><span class="lineno">  520</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00521"></a><span class="lineno">  521</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00522"></a><span class="lineno">  522</span>&#160;<span class="comment"> * Stores caches for completions.</span></div>
<div class="line"><a name="l00523"></a><span class="lineno">  523</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00524"></a><span class="lineno">  524</span>&#160;<span class="comment"> * @param $complete</span></div>
<div class="line"><a name="l00525"></a><span class="lineno">  525</span>&#160;<span class="comment"> *   A structured array of completions, keyed by type, including a &#39;commands&#39;</span></div>
<div class="line"><a name="l00526"></a><span class="lineno">  526</span>&#160;<span class="comment"> *   type that contains all commands with command specific completions keyed by</span></div>
<div class="line"><a name="l00527"></a><span class="lineno">  527</span>&#160;<span class="comment"> *   type. The array does not need to include all types - used by</span></div>
<div class="line"><a name="l00528"></a><span class="lineno">  528</span>&#160;<span class="comment"> *   drush_complete_rebuild_arguments().</span></div>
<div class="line"><a name="l00529"></a><span class="lineno">  529</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00530"></a><span class="lineno"><a class="code" href="complete_8inc.html#a520dd6cb68367ab70b710431d76c85df">  530</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a520dd6cb68367ab70b710431d76c85df">drush_complete_cache_set</a>($complete) {</div>
<div class="line"><a name="l00531"></a><span class="lineno">  531</span>&#160;  <span class="keywordflow">foreach</span> ($complete as $type =&gt; $values) {</div>
<div class="line"><a name="l00532"></a><span class="lineno">  532</span>&#160;    <span class="keywordflow">if</span> ($type == <span class="stringliteral">&#39;commands&#39;</span>) {</div>
<div class="line"><a name="l00533"></a><span class="lineno">  533</span>&#160;      <span class="keywordflow">foreach</span> ($values as $command_name =&gt; $command) {</div>
<div class="line"><a name="l00534"></a><span class="lineno">  534</span>&#160;        <span class="keywordflow">foreach</span> ($command as $command_type =&gt; $command_values) {</div>
<div class="line"><a name="l00535"></a><span class="lineno">  535</span>&#160;          <a class="code" href="includes_2cache_8inc.html#a27a40af68c2a61fdf94b980b953236bf">drush_cache_set</a>(<a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">drush_complete_cache_cid</a>($command_type, $command_name), $command_values, <span class="stringliteral">&#39;complete&#39;</span>, <a class="code" href="includes_2cache_8inc.html#a594d24dfbddcc6a2c2200ce328d5a8d2">DRUSH_CACHE_TEMPORARY</a>);</div>
<div class="line"><a name="l00536"></a><span class="lineno">  536</span>&#160;        }</div>
<div class="line"><a name="l00537"></a><span class="lineno">  537</span>&#160;      }</div>
<div class="line"><a name="l00538"></a><span class="lineno">  538</span>&#160;    }</div>
<div class="line"><a name="l00539"></a><span class="lineno">  539</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00540"></a><span class="lineno">  540</span>&#160;      <a class="code" href="includes_2cache_8inc.html#a27a40af68c2a61fdf94b980b953236bf">drush_cache_set</a>(<a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">drush_complete_cache_cid</a>($type), $values, <span class="stringliteral">&#39;complete&#39;</span>, <a class="code" href="includes_2cache_8inc.html#a594d24dfbddcc6a2c2200ce328d5a8d2">DRUSH_CACHE_TEMPORARY</a>);</div>
<div class="line"><a name="l00541"></a><span class="lineno">  541</span>&#160;    }</div>
<div class="line"><a name="l00542"></a><span class="lineno">  542</span>&#160;  }</div>
<div class="line"><a name="l00543"></a><span class="lineno">  543</span>&#160;}</div>
<div class="line"><a name="l00544"></a><span class="lineno">  544</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00545"></a><span class="lineno">  545</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00546"></a><span class="lineno">  546</span>&#160;<span class="comment"> * Generate a cache id.</span></div>
<div class="line"><a name="l00547"></a><span class="lineno">  547</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00548"></a><span class="lineno">  548</span>&#160;<span class="comment"> * @param $type</span></div>
<div class="line"><a name="l00549"></a><span class="lineno">  549</span>&#160;<span class="comment"> *   The completion type.</span></div>
<div class="line"><a name="l00550"></a><span class="lineno">  550</span>&#160;<span class="comment"> * @param $command</span></div>
<div class="line"><a name="l00551"></a><span class="lineno">  551</span>&#160;<span class="comment"> *   The command name (optional), if completions are command specific.</span></div>
<div class="line"><a name="l00552"></a><span class="lineno">  552</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00553"></a><span class="lineno">  553</span>&#160;<span class="comment"> * @return string</span></div>
<div class="line"><a name="l00554"></a><span class="lineno">  554</span>&#160;<span class="comment"> *   Cache id.</span></div>
<div class="line"><a name="l00555"></a><span class="lineno">  555</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00556"></a><span class="lineno"><a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">  556</a></span>&#160;<span class="keyword">function</span> <a class="code" href="complete_8inc.html#a5cacfdc5ec07fac0d484c64c7368c511">drush_complete_cache_cid</a>($type, $command = NULL) {</div>
<div class="line"><a name="l00557"></a><span class="lineno">  557</span>&#160;  <span class="comment">// For per-site caches, we include the site root and uri/path in the cache id</span></div>
<div class="line"><a name="l00558"></a><span class="lineno">  558</span>&#160;  <span class="comment">// hash. These are quick to determine, and prevents a bootstrap to site just</span></div>
<div class="line"><a name="l00559"></a><span class="lineno">  559</span>&#160;  <span class="comment">// to get a validated root and URI. Because these are not validated, there is</span></div>
<div class="line"><a name="l00560"></a><span class="lineno">  560</span>&#160;  <span class="comment">// the possibility of cache misses/ but they should be rare, since sites are</span></div>
<div class="line"><a name="l00561"></a><span class="lineno">  561</span>&#160;  <span class="comment">// normally referred to the same way (e.g. a site alias, or using the current</span></div>
<div class="line"><a name="l00562"></a><span class="lineno">  562</span>&#160;  <span class="comment">// directory), at least within a single command completion session.</span></div>
<div class="line"><a name="l00563"></a><span class="lineno">  563</span>&#160;  <span class="comment">// We also static cache them, since we may get differing results after</span></div>
<div class="line"><a name="l00564"></a><span class="lineno">  564</span>&#160;  <span class="comment">// bootstrap, which prevents the caches from being found on the next call.</span></div>
<div class="line"><a name="l00565"></a><span class="lineno">  565</span>&#160;  <span class="keyword">static</span> $root, $site;</div>
<div class="line"><a name="l00566"></a><span class="lineno">  566</span>&#160;  <span class="keywordflow">if</span> (empty($root)) {</div>
<div class="line"><a name="l00567"></a><span class="lineno">  567</span>&#160;    $root = <a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(array(<span class="charliteral">&#39;r&#39;</span>, <span class="stringliteral">&#39;root&#39;</span>), <a class="code" href="includes_2environment_8inc.html#abd11f38a3202eab6206d1bfda1e11b6f">drush_locate_root</a>());</div>
<div class="line"><a name="l00568"></a><span class="lineno">  568</span>&#160;    $site = <a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(array(<span class="charliteral">&#39;l&#39;</span>, <span class="stringliteral">&#39;uri&#39;</span>), <a class="code" href="includes_2environment_8inc.html#a35bbe20a952598e022399912d89f97f0">drush_site_path</a>());</div>
<div class="line"><a name="l00569"></a><span class="lineno">  569</span>&#160;  }</div>
<div class="line"><a name="l00570"></a><span class="lineno">  570</span>&#160;  <span class="keywordflow">return</span> <a class="code" href="includes_2cache_8inc.html#a70613777c5e1a0e258bd0d8e213e0156">drush_get_cid</a>(<span class="stringliteral">&#39;complete&#39;</span>, array(), array($type, $command, $root, $site));</div>
<div class="line"><a name="l00571"></a><span class="lineno">  571</span>&#160;}</div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Wed Oct 8 2014 09:04:54 for Drush by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.1.2
</small></address>
</body>
</html>