SymfonyWorld Online 2021 Winter Edition December 9 – 10, 2021 100% Online +20 talks and workshops

New in Symfony 5.4: Console Autocompletion

Symfony 5.4 is backed by Private Packagist. Private Packagist is a fast, reliable, and secure Composer repository for your private packages. It mirrors all your open-source dependencies for better availability and monitors them for security vulnerabilities.

Symfony 5.4 and Symfony 6.0 will be released simultaneously at the end of November 2021. According to the Symfony release process, both versions will have the same features, but Symfony 6.0 won't include any deprecated features.

This is the first article of the series that shows the most important new features introduced by Symfony 5.4 and 6.0 versions.


Contributed by
Wouter De Jong
in #42251.

Symfony Console is one of the favorite Symfony Components and the most downloaded one if we exclude the Polyfill components (368 million downloads as of October 2021). It's packed with features and it has become the standard way of creating CLI applications in PHP.

In Symfony 5.4 we've improved the Console component with the most important and most requested missing feature: autocompletion. When running a Symfony command you can now press the TAB key to see a list of contextual suggestions.

In the following GIF, you can see how pressing the TAB key first shows the list of commands available. After typing secrets:remove, when you press TAB again it autocompletes the argument value with one of the valid secret names of the application:

Symfony Console Autocompletion in Symfony applications

Autocompletion will not only boost your productivity, but it will also prevent many typos and issues when passing options and arguments to commands. This feature works for Bash shell, but we intend to support other shells such as Zsh and Fish in the near future. Also, this is not limited to Symfony applications. The following GIF shows the autocompletion of options and arguments in a Laravel command:

Symfony Console Autocompletion in Other Applications such as Laravel

Technically, autocompletion is enabled when your command class defines a complete() method. Inside this method you need to generate the appropriate suggestions for each input. For example, if your command defines an argument called format which only allows passing json and xml as its values, add this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;

class SomeCommand extends Command
{
    // ...

    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
    {
        if ($input->mustSuggestOptionValuesFor('format')) {
            $suggestions->suggestValues(['json', 'xml']);
        }
    }
}

Usually, the suggestions will be dynamic, so you'll need to call some other methods and services to generate those values instead of passing an array of values to suggestValues().

During these past weeks, the Symfony community has worked on a colossal joint effort to add autocompletion for all internal Symfony commands. Here's the list of commands updated so far:

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

Seeing such a response from the community at the time of the call for contributions was heartwarming! 🤩
Finally. Thanks ! 🙏
Only bash is supported for now, but fish is on the way. I had fun time testing backslash escaping for class names.
https://github.com/symfony/symfony/pull/43665

Thanks to all the contributors that spread completion to all core commands.
https://github.com/symfony/symfony/issues/43594
Jérôme, I've updated the blog post to add a link to the 43594 issue which coordinates the community effort to update all commands. Thanks!
Great Work! Why don't we use an interface? Otherwise we are loosely bound to the complete() method.

E.g. CompletableCommand or something else?
@Daniel, we had a CompletionInterface, but we had to remove it. See https://github.com/symfony/symfony/pull/43628
@Daniel we are not "loosely" bound, as the method is already defined in the base "Command" class that each command has to extend.
Besides providing forward compatibility, this also allows to add completion support to commands without class (directly using (new Command())).
Thank you for the response(s)! Now i understand 👍
Awesome
I tried to upgrade a project to Symfony 5.4.0-beta1 but I can't autocomplete any command... do I need to do something special? My $BASH_VERSION is 5.1.4(1)-release
Login with SymfonyConnect to post a comment