cli icon indicating copy to clipboard operation
cli copied to clipboard

Support for completion in other shells (fish, sh, etc.)

Open hasit opened this issue 9 years ago • 12 comments

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.

hasit avatar Apr 08 '16 09:04 hasit

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.

jszwedko avatar Apr 09 '16 18:04 jszwedko

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, change EnableBashCompletion bool -> EnableCompletion bool to accommodate all (supported) shells.
  • In type App struct, add CompletionShells []string to accept which shell completion scripts the user wants to generate. For example, when writing a program in Go using cli, the user can write
...
app.EnableCompletion = true
app.CompletionShells = []string{"zsh", "fish", "bash"}
…

and cli will generate autocompletion scripts for all three shells.

hasit avatar Apr 10 '16 22:04 hasit

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/cli intercepts this and calls BashComplete which defaults to DefaultAppComplete (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!

jszwedko avatar Apr 20 '16 04:04 jszwedko

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?

saschagrunert avatar Aug 03 '19 13:08 saschagrunert

I absolutely think we can add fish completion! It should be an easy PR to add into the 1.X series 👍

coilysiren avatar Aug 03 '19 17:08 coilysiren

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.

stale[bot] avatar Feb 25 '20 08:02 stale[bot]

Bumping since this is low-lift to add 🙏

coilysiren avatar Feb 26 '20 05:02 coilysiren

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.

stale[bot] avatar Feb 26 '20 05:02 stale[bot]

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.

stale[bot] avatar May 26 '20 05:05 stale[bot]

Closing this as it has become stale.

stale[bot] avatar Jun 25 '20 09:06 stale[bot]

This should still be very relevant. Can I bump this?

tionis avatar Jun 14 '21 23:06 tionis

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.

stale[bot] avatar Jun 15 '21 23:06 stale[bot]

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

dearchap avatar Nov 29 '22 13:11 dearchap