You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR was squashed before being merged into the 5.4 branch.
Discussion
----------
[Console] Bash completion integration
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #38275
| License | MIT
| Doc PR | -
This is a first shot at implementing interactive bash completion support in the Console component. Besides completing option and command names, commands can override `Command::complete()` to implement completing values.
I've added an example code to the `secrets:remove` command, which now supports autocompletion quite nicely: 
And support for other applications using `symfony/console` is automatically included (if the autocompletion script is installed for that specific application):

This PR only implements Bash completion. Zsh and Fish have much more sophisticated completion systems, but I propose to keep those for a future PR if there is a need for this.
### How it works
1. A bash completion function (`_console`) is defined by `bin/console completion bash` for the `console` command (name of the "binary" file)
2. This completion function calls the local `bin/console _complete` command to come up with suggestions
3. Bash parses these suggestions and shows them to the user.
This has one drawback: the `_console` function is defined globally only once. This means we cannot easily change it, as it would break if you run different Symfony versions. We should probably add versioning (e.g. `bin/console _complete --version=1.0`) and don't suggest anything if the version doesn't match.
<s> **Maybe it would be safer to mark this feature as experimental and target 6.0, to allow us to fine tune the shell related sides over the lifespan of 6.x?** </s> symfony/symfony#42251 (comment)
### Steps to test yourself
Load this PR in your project, open a bash shell and run this command to "install" completion for this project:
```
bin/console completion bash > /etc/bash_completion.d/console
````
Then reload the bash shell and enjoy autocompletion.
### TODO
* [x] Autocompleting in the middle of the input doesn't work yet (i.e. `bin/console --en<TAB> cache:clear`)
* [x] Better error handling
* [x] Add a `bin/console completion` command to dump the `_console` file, so users can install this locally
* [x] Add some versioning, to allow us to change the `_console` file in the future
* [x] <s>See how we can better support standalone usage (e.g. Composer)</s> Tested on Laravel's artisan, works flawlessly
Commits
-------
e0a174f877 [FrameworkBundle] Add CLI completion to secrets:remove
82ef399de3 [Console] Bash completion integration
0 commit comments