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

How would I create a custom Zsh Completion, but for another shell? #771

Closed
dfinke opened this issue Oct 19, 2018 · 5 comments
Closed

How would I create a custom Zsh Completion, but for another shell? #771

dfinke opened this issue Oct 19, 2018 · 5 comments

Comments

@dfinke
Copy link

dfinke commented Oct 19, 2018

I would like to take my commands and provide the user a way to auto gen completions in a format for another shell. Are there examples on how to do this?

Thank you

@goshlanguage
Copy link

This is a bit of a non-answer, so apologies for that;

Here's an example of how helm does it, which is another project using Cobra:
https://github.com/helm/helm/blob/master/scripts/completions.bash

If you notice, there's not really an intersection of this bash completion script, and go or cobra. The commands and arg sets look to be hardcoded in. Not only that, but for me, it's a bit hard to follow.

Being curious since you mentioned it, I wondered how it works for zsh since that's my shell of choice, and I stumbled on this very helpful guide:
https://github.com/ecliptik/dotfiles/blob/master/.zprezto/modules/completion/external/zsh-completions-howto.org

I think this guide articulates how to go about writing a completion script, but still falls short of providing a simple, succinct example. I think that this article provides a good starting point, where you can craft a simple completion script, then turn to the in-depth how to for more advance methods/functionality:
https://askql.wordpress.com/2011/01/11/zsh-writing-own-completion/

@dfinke
Copy link
Author

dfinke commented Oct 21, 2018

Thanks @ryanhartje. I'm new to golang, I saw in the the cobra code base the zsh completion code they use. I don't know enough about pointer receivers yet. My plan was to take that code and modify it to what I need. Coming from an OOP background, it looked to me that I would need to add my code to the base cobra code in order to traverse the commands in my code.

Is that true? Or can I iterate over the commands another way?

@github-actions
Copy link

github-actions bot commented Apr 8, 2020

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator

@dfinke Cobra now supports auto-completion for 4 different shells: bash, zsh, fish, and powershell. If you still need to add support for a new shell, it has become much easier because Cobra now provides a hidden command which can be used to obtain completions from different shell scripts, the __complete command. Both zsh and fish use this command as their sole source of completions. An example of bash using that same technique can be seen in #1146 .

If this addresses your need, can this issue be closed?

@johnSchnake
Copy link
Collaborator

Closing due to inactivity. Feel free to reopen if you still need help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants