Support for completion in other shells (fish, sh, etc.)
Hello,
I was going through the repo (I am using cli for my new project) and I found that it supports generation of bash and zsh completion files automatically. That is pretty amazing. Is there a way to support other shells (e.g. fish, sh)? I am an avid fish shell user and have been using it since a long time.
I would like to propose implementation of EnableCompletion flag instead of EnableBashCompletion flag to accept a string for supported shells (like bash, zsh, fish, sh, ...) and generate autocomplete file for the mentioned shell.
If it is a desirable feature, I would like to brainstorm the implementation with the author and community members and take the initial steps towards its development.
Hi @hasit!
I think it would be great to support fish and other shells. I think your proposed change to the interface makes sense. I'd be happy to discuss further and review any pull requests introducing this.
Thank you @jszwedko.
To start off, I forked the repo and started reading the App interface and how it accepts parameters. My question is, how does EnableBashCompletion bool variable translate into generating autocomplete file? I do not see a function that is doing that. It'd be very helpful if you could explain the flow from the point of setting to generation of the autocomplete file.
To aid this enhancement, I would like to add
- In
type App struct, changeEnableBashCompletion bool -> EnableCompletion boolto accommodate all (supported) shells. - In
type App struct, addCompletionShells []stringto accept which shell completion scripts the user wants to generate. For example, when writing a program in Go usingcli, the user can write
...
app.EnableCompletion = true
app.CompletionShells = []string{"zsh", "fish", "bash"}
…
and cli will generate autocompletion scripts for all three shells.
Hi @hasit!
We'll want to maintain (but deprecate) EnableBashCompletion to support backwards compatibility for now, but I think your suggested fields make sense. We could simplify slightly and just do CompletionShells (if it is empty disable).
As far as for the current flow, it looks something like:
- Programmer enables bash completion via
EnableBashCompletion - User installs https://github.com/codegangsta/cli/blob/master/autocomplete/bash_autocomplete as like
/etc/bash_completion.d/<program name>(this would commonly be done by a package or install script) - When the user tries to tab complete, it will call the binary with
--generate-bash-completion -
codegangsta/cliintercepts this and callsBashCompletewhich defaults toDefaultAppComplete(https://github.com/codegangsta/cli/blob/bc465becccd1d527002fda095fc3c19d9c115029/help.go#L119-L125) - Bash interprets this result as a list of tokens that can appear next
Currently this only works for subcommands, but could be expanded to handle flags.
Let me know if this makes things clearer!
I would like to bring this topic back in the table, because I think a fish completion should be doable relatively straight forward via code generation. WDYT?
I absolutely think we can add fish completion! It should be an easy PR to add into the 1.X series 👍
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.
Bumping since this is low-lift to add 🙏
This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.
Closing this as it has become stale.
This should still be very relevant. Can I bump this?
This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.
It appears that fish completion was already added in 2.x. Do we want to keep the dynamic auto complete feature in cli or move to a generated one like what kubectl does.
kubectl completion fish | source