New in Symfony 6.1: Improved Console Autocompletion
May 19, 2022 • Published by Javier Eguiluz
Symfony 6.1 is backed by:
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
In Symfony 5.4 we introduced console autocompletion for the argument names, option names and option values of any command created with the Symfony Console component. In Symfony 6.1 we're improving it with new features.
Autocompletion for Fish Shell
Autocompletion works differently depending on your shell. Previously we only supported autocompletion in Bash shell. Starting from Symfony 6.1 we also support Fish shell, which is popular among many developers.
To enable autocompletion in your Fish shell, you only need to run the following command once and then source the file:
1
$ php bin/console completion fish >> ~/.config/fish/completions/sf_console.fish
Completion Values in Input Definitions
Autocompletion is currently based on defining a method called complete()
in
your command. There you can provide the possible values of any of the command
arguments and options. In Symfony 6.1 you can also define autocompletion values
directly in the command input definition:
1 2 3 4 5 6 7 8 9 10 11 12
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help', function () {
// here we return the name of all application commands
return array_keys((new ApplicationDescription($this->getApplication()))->getCommands());
}),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', function () {
// here we return an array of possible values defined somewhere
return (new DescriptorHelper())->getFormats();
}),
// when using addOption() and addArgument() you can also provide autocompletion
->addArgument('shell', InputArgument::OPTIONAL, '...', null, fn () => $this->getSupportedShells())
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 are closed.
To ensure that comments stay relevant, they are closed for old posts.
Note that you only need to source the file the first time. When starting Fish shell, Fish automatically discovers the completion functions in the "~/.config/fish/completions/" directory.
Also great addition to be able to define this directly when adding arguments and options. This makes it much easier to not forget it when adding a new option or argument.
By reading the related post, it looks like you don't need to do anything special to get it work, so I'm wondering what could I miss...