function drush_early_complete

8.0.x drush_early_complete()
6.x drush_early_complete()
7.x drush_early_complete()
5.x drush_early_complete()
master drush_early_complete()

Produce autocomplete output.

Determine position (is there a site-alias or command set, and are we trying to complete an option). Then produce a list of completions for the last word and output them separated by newlines.


includes/, line 101
Provide completion output for shells.


function drush_early_complete() {
  // We use a distinct --complete-debug option to avoid unwanted debug messages
  // being printed when users use this option for other purposes in the command
  // they are trying to complete.
  drush_set_option('debug', FALSE);
  if (drush_get_option('complete-debug', FALSE)) {
    drush_set_context('DRUSH_DEBUG', TRUE);
  // Set up as if we were running the command, and attempt to parse.
  $argv = drush_complete_process_argv();
  if ($alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
    $set_sitealias_name = $alias;
    $set_sitealias = drush_sitealias_get_record($alias);

  // Arguments have now had site-aliases and options removed, so we take the
  // first item as our command. We need to know if the command is valid, so that
  // we know if we are supposed to complete an in-progress command name, or
  // arguments for a command. We do this by checking against our per-site cache
  // of command names (which will only bootstrap if the cache needs to be
  // regenerated), rather than drush_parse_command() which always requires a
  // site bootstrap.
  $arguments = drush_get_arguments();
  $set_command_name = NULL;
  if (isset($arguments[]) && in_array($arguments[] . ' ', drush_complete_get('command-names'))) {
    $set_command_name = $arguments[];
  // We unset the command if it is "help" but that is not explicitly found in
  // args, since Drush sets the command to "help" if no command is specified,
  // which prevents completion of global options.
  if ($set_command_name == 'help' && !array_search('help', $argv)) {
    $set_command_name = NULL;

  // Determine the word we are trying to complete, and if it is an option.
  $last_word = end($argv);
  $word_is_option = FALSE;
  if (!empty($last_word) && $last_word[] == '-') {
    $word_is_option = TRUE;
    $last_word = ltrim($last_word, '-');

  $completions = array();
  if (!$set_command_name) {
    // We have no command yet.
    if ($word_is_option) {
      // Include global option completions.
      $completions += drush_hyphenate_options(drush_complete_match($last_word, drush_complete_get('options')));
    else {
      if (empty($set_sitealias_name)) {
        // Include site alias completions.
        $completions += drush_complete_match($last_word, drush_complete_get('site-aliases'));
      // Include command completions.
      $completions += drush_complete_match($last_word, drush_complete_get('command-names'));
  else {
    if ($last_word == $set_command_name) {
      // The user just typed a valid command name, but we still do command
      // completion, as there may be other commands that start with the detected
      // command (e.g. "make" is a valid command, but so is "make-test").
      // If there is only the single matching command, this will include in the
      // completion list so they get a space inserted, confirming it is valid.
      $completions += drush_complete_match($last_word, drush_complete_get('command-names'));
    else if ($word_is_option) {
      // Include command option completions.
      $completions += drush_hyphenate_options(drush_complete_match($last_word, drush_complete_get('options', $set_command_name)));
    else {
      // Include command argument completions.
      $argument_completion = drush_complete_get('arguments', $set_command_name);
      if (isset($argument_completion['values'])) {
        $completions += drush_complete_match($last_word, $argument_completion['values']);
      if (isset($argument_completion['files'])) {
        $completions += drush_complete_match_file($last_word, $argument_completion['files']);

  if (!empty($completions)) {
    return implode("\n", $completions);
  return TRUE;