-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
zsh autocompletion generator #107
Comments
we did the zsh completion generator in PHP command line framework: https://github.com/c9s/CLIFramework#zsh-completion-with-lazy-completion-values I think we can share some zsh functions for cobra :) |
Really wonderful, @c9s! And thank you for sharing! :-) |
Would love to have it. What's the status of that request? |
I would love to see that happening. Who needs some candy to kick the tires? |
Zsh completion based on bash completion has been implemented in Kubernetes : https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/completion.go . |
Ideally it wouldn't called bashcompinit. It's quite slow and zsh's autocomplete system can do a lot more than what can bash can do anyway. There any movement on this? If there's no movement I'd like to take a look. |
Unfortunately, there is no progress. But every PR is welcome! |
@derimagia, did you start working on it? |
I am working on it, but the code is not published yet ( my previous zsh completion generator was written in PHP, see https://github.com/c9s/CLIFramework ) |
@kongslund I barely started yesterday, was planning to do it this weekend/week. @c9s let me know if there's anything you need for it. |
@derimagia thanks! I'm wondering if there is a gateway in cobra that zsh can ask the program to complete at the runtime, like CLIFramework use the PHP closure to return the dynamic result. for example, when completing arguments, the cli app send some http request and use the response for argument completion. |
https://github.com/spf13/cobra/blob/4673102358fdd630e3bb0eb6dee96e4b533d53ec/bash_completions.md Look for "BashCompletionFunction" Here's one for kubes: https://github.com/kubernetes/kubernetes/blob/0f3403d3510847a86fcbf43840409e4e79ba11bb/pkg/kubectl/cmd/cmd.go#L264 I would suggest doing 2 things here
|
Thinking about it a little more an alternative to providing the command to get the arguments, we could just add a method that accepts a POSIX function name and then we also provide some static functions for them to call as a simple api - (One to add arguments, one to add flags, one to add files, etc) |
Please take a look at #497! |
Looks like some stuff has landed for implementing this already, but if more inspiration is needed, came across this that at least claims to have zsh support (haven't personally tried it yet): https://github.com/posener/complete |
That sounds like a bug with boolSlice... |
I see. Ok, I'll fix these two things later today and will try to look into |
Adopted the two suggested changes. I'll appreciate if someone can try this on an app larger then couple of subcommands and flags. Thanks. |
@eparis (or someone else), I have one more question (sorry for spamming). I found a way to indicate optional arguments for flags. Should I use them always? how does cobra flags behaves if no argument is supplied? |
@babysnakes I'm not sure what you mean. Any flag without a NoOptDefValue will use the next argument as a flag... If you have
and then you run
you will end up with
|
@eparis thanks. I worked with many CLI options parsing systems and some of them allow for the options to be specified without argument. So in your example ( I just wanted to ask for advice wether I should use the current enforcing way or use a way that allows optional arguments? |
Wouldn't this whole conversation work better in a pull request? |
@rfay in general you're right, however since I only tried it on a very small utility I wanted someone else to try it one a larger project before I submit a pull request. There is a gap between working in theory and in practice 😄 . But I'll follow your advice, do some last fixes and submit a PR. |
@babysnakes with pflag that's NoOptDefVal. If you set a NoOptDefVal on stringflag1 then the same arguments would parse differently.
calling with
Will cause a parse error.
calling with
will result in Note however that flags with NoOptDefVal can still use the
will result in ip=2468. pflag is very specific about flags with an optional default and the parsing forms. No guessing or heuristics. |
@eparis, I see. Thanks, I'll do some final checks and submit a pull request (asking for someone to test). |
I added argument support for my zsh-completion PR, I would appreciate some input if someone cares to test it: #646. |
Hello, I'm wondering what the current state of |
@eparis Looking at the recent commits, I see that you are an active contributor and also the person who added bash completion. What are your thoughts on this issue? And based on your thoughts, we can all probably work towards closing this issue. @ericvn I checked the PR. I have seen similar code in helm and used the same in a cli tool and it worked quite well, even though it looks like a interim solution |
I'm concerned that the current cobra Zsh completion file doesn't work anywhere, although there must be some subset that does to get by the initial commit. I see numerous CLIs like solo-io/gloo#654 where the cobra Zsh completion file fails to work. I did figure out how to make the current output file work by adding a small header and footer and indent the current output. This leaves the completion file a native Zsh completion file, but it is incomplete as it only supports nouns without any flag support. I have created a commit against our CLI using code similar to #828 and it works as a solution. |
This issue is being marked as stale due to a long period of inactivity |
With /cc @jharshman @jpmcb |
At least as long as one is using a commit later than the last release (admittedly, I haven't tried with that commit yet). |
There are desires among users (see gohugoio/hugo#1088) for an auto-completion generator for zsh, similar to what @eparis has made available for bash in his excellent work in #69.
So, here is a reminder so perhaps someone could use @eparis's excellent work for bash as the basis for the same for zsh. :-)
The text was updated successfully, but these errors were encountered: