I'm trying to use .completion() with .commandDir(). I would like to keep the relevant code to each command in their respective files. I can't figure out how to define completion for an individual command. Is that currently possible?
The builder method is the only place I see that I have access to yargs itself. I tried using it there with no luck.
Any guidance would be appreciated.
@fearphage it's been a while since we looked at our completion functionality; I bet given all the work we've recently put into commands, it would make sense to add more command specific functionality.
I'd also love to add support for shells other than bash, e.g., Zsh.
@fearphage @bcoe I'm not _very_ familiar with how completion scripts work, but isn't it possible (though perhaps difficult?) to make this more magical? I'm thinking of yargs, after having fetched all commands (through .command, .commandDir etc...) to automatically provide completions for all commands.
Of course adding support for zsh and others would also be great, but I think a lot of people don't even know yargs has this completion feature, and making it available automatically (which shouldn't negatively impact anyone I think) would certainly make a few people out there happy.
Note: I dropped in lots of links to kubectl which influenced my request for this feature. Kubectl is a Go project, but there are also some bash links in there too.
Yeah, I'm not 100% familiar with the how either, but I know fetching dynamic data (things you didn't know when the bash/zsh rc was parsed) is definitely possible.
This is one such source of dynamic completion: kubectl
I also like the way you add the completion to your rc file.
It has several interesting things in it. Like their implementation of zsh completion is just bash completion with some added pieces and they run sed on the bash completion. That might ease development. I'm just throwing ideas out.
Here's the PR where they implemented zsh initially - kubernetes/kubernetes#23797
An early implementation of bash completion: kubernetes/kubernetes#1877
Here's an example of fetching dynamic data.
To answer my original question, you can set a per-command completion function. I was just doing it incorrectly. In case anyone else gets stumped:
exports.builder = function(yargs) {
yargs
.option(...)
.check(...)
.completion('completion', function(currentWord, argv) {
// current word is this command
// argv is the partially parsed results (it doesn't know about options above)
})
;
}
One thing missing is the ability to get a list of the available flags.
So if I have a command foo and I type foo -<tab>, I can't see a programmatic way to complete this action from the completion callback. Is that functionality missing or am I just failing to see it again?
Also it's inconvenient (but easily worked around) that top-level commands need to be pre-parsed before passing them to the completion function since they return command <param> <things> instead of single word responses as expected.
If I could find a way to address the flag handling (as mentioned above), I'll make a PR and clean up what I can.
I see multiple people are assigned to this. Are you working on it as well. I've done a good amount on looking into it now. I've manually edited the completion myself to make it work. Now I'm just working on figuring out how to automate that process. I just want to make sure I'm not duplicating/wasting any effort. /cc @JaKXz @maxrimue
Most helpful comment
@fearphage it's been a while since we looked at our completion functionality; I bet given all the work we've recently put into commands, it would make sense to add more command specific functionality.
I'd also love to add support for shells other than
bash, e.g.,Zsh.