Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement generic autocompletion #11

Merged
merged 2 commits into from
Jun 12, 2024

Conversation

tucksaun
Copy link
Member

@tucksaun tucksaun commented Jun 10, 2024

This PR provides generic autocompletion for every application using Symfony CLI's Console for Bash, ZSH, and Fish. To make it work locally one can use the instructions provided by symfony help completion.

Fix symfony-cli/symfony-cli#481

This autocompletion works on commands but also on flags. The flag autocompletion is fully automatic for some basic flag types (boolean and verbosity flags) and can be customized for any flag instance by specifying the ArgsPredictor property:

var myCommand = &Command{
	Name: "foo",
	Flags: []Flag{
		&StringFlag{
			Name: "plan",
			ArgsPredictor: func(*Context, string) []string {
				return []string{"free", "basic", "business", "enterprise"}
			},
		},
	},
}

One can also implement argument autocompletion for a command:

var myCommand = &Command{
	Name: "foo",
	ShellComplete: func(context *Context, string) []string {
		return []string{"foo", "bar", "baz"}
	},
}

Importantly, this PR also implements autocompletion forwarding to external commands we wrap such as console 😎

asciicast

(opening as a draft for now because there are a couple of things I want to discuss and we also need to figure out the patch required on Symfony's side - will open the PR tomorrow)

resources/completion.bash Outdated Show resolved Hide resolved
…ymfony CLI's Console for Bash, ZSH, and Fish. To make it work locally one can use the instructions provided by `symfony help completion`.

This autocompletion works on commands but also on flags. The flag autocompletion is fully automatic for some basic flag types (boolean and verbosity flags) and can be customized for any flag instance by specifying the `ArgsPredictor` property:
```golang
var myCommand = &Command{
	Name: "foo",
	Flags: []Flag{
		&StringFlag{
			Name: "plan",
			ArgsPredictor: func(*Context, complete.Args) []string {
				return []string{"free", "basic", "business", "enterprise"}
			},
		},
	},
}
```
One can also implement argument autocompletion for a command:
```golang
var myCommand = &Command{
	Name: "foo",
	ShellComplete: func(context *Context, c complete.Args) []string {
		return []string{"foo", "bar", "baz"}
	},
}
```

Importantly, this PR also implements autocompletion forwarding to external commands we wrap such as `console` 😎

[![asciicast](https://asciinema.org/a/lxPJuBAQr4NY2tFnB1co1PDu3.svg)](https://asciinema.org/a/lxPJuBAQr4NY2tFnB1co1PDu3)

(opening as a draft for now because there are a couple of things I want to discuss and we also need to figure out the patch required on Symfony's side)
@tucksaun tucksaun marked this pull request as ready for review June 11, 2024 15:47
@fabpot fabpot merged commit 3b6715d into symfony-cli:main Jun 12, 2024
1 check passed
@fabpot
Copy link
Contributor

fabpot commented Jun 12, 2024

Thank you @tucksaun! 🎊

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jul 22, 2024
This PR was submitted for the 7.1 branch but it was merged into the 5.4 branch instead.

Discussion
----------

Document Symfony CLI autocompletion

I recently implemented autocompletion for any tools based on [symfony-cli/console](https://github.com/symfony-cli/console) including Symfony CLI (see symfony-cli/console#11).
I also added completion forwarding for `composer` and `console` (see symfony-cli/symfony-cli#493).

This means that you can have the autocompletion for the 3 tools by following `symfony completion --help` instructions and that it will automatically use the right configuration (ie. PHP version, `php.ini`, etc) when running it.

Note: I opened the PR against `7.1` but technically this is not related to any `Symfony version`, let me know if you want me to change the target version.

Commits
-------

52f1f00 Document Symfony CLI autocompletion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Completion of symfony console
2 participants