New in Symfony 4.3: Better console autocomplete

Contributed by
Mikkel Paulson
in #30997.

The Console component provides an utility to autocomplete the answers to questions created with the Question Helper. This is used for example to autocomplete classes and entities in the MakerBundle to boost your productivity.

However, autocomplete requires to know all the possible suggestions beforehand. This is impossible if the suggestions depend on the user input and not practical if the completion is highly dynamic (e.g. when autocompleting the contents of directories).

In Symfony 4.3 we improved this feature with a new method called setAutocompleterCallback() that allows to use a callback function to generate the autocomplete suggestions dynamically. For example, this example shows how to autocomplete the user input with valid directory contents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use Symfony\Component\Console\Question\Question;

// ...
public function execute(InputInterface $input, OutputInterface $output)
{
    // This function is called whenever the input changes and new
    // suggestions are needed.
    $callback = function (string $userInput): array {
        // Strip any characters from the last slash to the end of the string
        // to keep only the last directory and generate suggestions for it
        $inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $userInput);
        $inputPath = '' === $inputPath ? '.' : $inputPath;

        // CAUTION - this example code allows unrestricted access to the
        // entire filesystem. In real applications, restrict the directories
        // where files and dirs can be found
        $foundFilesAndDirs = @scandir($inputPath) ?: [];

        return array_map(function ($dirOrFile) use ($inputPath) {
            return $inputPath.$dirOrFile;
        }, $foundFilesAndDirs);
    };

    $question = new Question('Please provide the full path of a file to parse');
    $question->setAutocompleterCallback($callback);

    $filePath = $helper->ask($input, $output, $question);
}

Comments

Really interesting feature, it can probably ease a lot of actual commands (mostly the one which requires to remember database information, external services, etc ...).

Maybe it could be a good idea to make it available for configuration generation (when building an external bundle or dedicated configuration) by providing an autocomplete on the available keys?
That's interesting, but it seems like the callable *must* return an array, and cannot return a generator / yield values.

I could have read it wrong, but it would be awesome to support generators for such a callable IMO.

Otherwise, it's great. :}
awesome...awesome......
Login with SymfonyConnect to post a comment