function drush_complete_match_file

8.0.x drush_complete_match_file($last_word, $files)
6.x drush_complete_match_file($last_word, $files)
7.x drush_complete_match_file($last_word, $files)
5.x drush_complete_match_file($last_word, $files)
master drush_complete_match_file($last_word, $files)

Retrieves the appropriate list of candidate file/directory completions, filtered by the last word that we are trying to complete.


string $last_word: The last word in the argument list (i.e. the subject of completion).

array $files: Array of file specs, each with a pattern and flags subarray.

Return value

array Array of candidate file/directory completions that start with the same characters as the last word. If the last word is empty, return all candidates.

1 call to drush_complete_match_file()
drush_early_complete in includes/
Produce autocomplete output.


includes/, line 290
Provide completion output for shells.


function drush_complete_match_file($last_word, $files) {
  $return = array();
  $firstchar = '';
  $full_paths = TRUE;
  if (isset($last_word) && $last_word[0] == '~') {
    // Complete does not do tilde expansion, so we do it here.
    $parts = explode('/', $last_word);
    // We shell out (unquoted) to expand the tilde.
    drush_shell_exec('echo ' . $parts[0]);
    $output = drush_shell_exec_output();
    $parts[0] = $output[0];
    $last_word = implode('/', $parts);
  foreach ($files as $spec) {
    // We always include GLOB_MARK, as an easy way to detect directories.
    $flags = GLOB_MARK;
    if (isset($spec['flags'])) {
      $flags = $spec['flags'] | GLOB_MARK;
    $listing = glob($last_word . $spec['pattern'], $flags);
    foreach ($listing as $item) {
      // Detect if the initial characters of the file/dirs to be listing differ.
      // If they do, we return a list of just their names. If they all have the
      // same first character we return full paths, to prevent the shell
      // replacing the current path with just the matching character(s).
      $char = $item[strrpos($last_word, '/') + 1];
      if (empty($firstchar)) {
        $firstchar = $char;
      else if ($firstchar !== $char) {
        $full_paths = FALSE;
      $return[] = $item;
  // If we don't need to return full paths, shorten them appropriately.
  if ($full_paths == FALSE) {
    foreach ($return as $id => $item) {
      $return[$id] = substr($return[$id], strrpos($last_word, '/') + 1);
  // If we are returning a single item (which will become part of the final
  // command), we need to use the full path, and we need to escape it
  // appropriately.
  if (count($return) == 1) {
    // Escape common shell metacharacters (we don't use escapeshellarg as it
    // single quotes everything, even when unnecessary).
    $item = preg_replace('/[ |&;()<>]/', "\\\\$0", $item);
    if (substr($item, -1) !== '/') {
      // Insert a space after files, since the argument is complete.
      $item = $item . ' ';
    $return = array($item);
  return $return;