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())
Very cool. I'm enjoying the completion in my Fish shell for some Symfony 6.1 demo projects! 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.
Can't wait to have this also in zsh. 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.
I'm still struggling to get autocompletion working on 5.4 :-( 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...
According to fish documentation "a completion file must have a filename consisting of the name of the command to complete and the suffix .fish" (https://fishshell.com/docs/current/completions.html). So the command should be "bin/console completion fish >> ~/.config/fish/completions/console.fish". For me it did not work with "sf_console.fish". With "console.fish" I get autocompletion.