New in Symfony 5.4: Console Autocompletion

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:

Autocompletion not only will 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:

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 and list commands (PR 43596) debug:firewall, debug:form, debug:messenger, debug:router (PR 43598) cache:pool:clear, cache:pool:delete (PR 43621) secrets:set, secrets:remove (PR 43626) ulid:generate, uuid:generate (PR 43639) messenger:setup-transports (PR 43640) debug:translation (PR 43644) security:hash-password (PR 43653) translation:pull, translation:push (PR 43672) translation:update (PR 43676) lint:twig, lint:xliff, lint:yaml (PR 43680) config:dump-reference (PR 43682) server:dump (PR 43683) debug:twig (PR 43846)

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/KwjmS_ZQirk/new-in-symfony-5-4-console-autocompletion

Created 4y | Nov 2, 2021, 10:20:08 AM


Login to add comment

Other posts in this group

SymfonyOnline June 2025: Inside a Financial App Breach: Debugging a Million-Dollar Bug

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 22, 2025, 1:50:03 PM | Symfony
New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

Apr 22, 2025, 9:10:36 AM | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

Apr 20, 2025, 8:30:06 AM | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 16, 2025, 4:30:02 PM | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

Apr 15, 2025, 2:50:24 PM | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

Apr 13, 2025, 9:40:02 AM | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 10, 2025, 4:40:34 PM | Symfony