picocli
picocli copied to clipboard
[Feature request] Native zsh completion
Picocli currently can create bash completion files, and zsh has a compatibility tool to read bash completion files.
However, this only exposes basic autocompletion features (what bash itself support), and does not take advantage of the features zsh offer.
For instance, zsh can show a description for each command/option, as well as option aliases (short vs long options). There's a thousands pictures on the internet; here's one example for the git command:
This behavior cannot be obtained from a bash completion file.
Therefore, I propose that a new Autocomplete.zsh
static method be added, to output a zsh-native completion script that includes more details, like command and option description and aliases.
I think that's great idea and that would be a very cool feature! Will you be able to provide a pull request?
+1, this would be awesome
The other relevant thing is that the file should be in FPATH and start with the #compdef line so shell login doesn't involve running all the scripts
https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
@yschimke thanks for the tip!
The testing and debugging section of that doc is good also e.g.
| _complete_help | Ctrl+x h | displays information about context names, tags, and completion functions used when completing at the current cursor position |
It's super hacky, and only supports the bare minimum needed for one-level descriptions on my specific app, but I made a first attempt at this on https://github.com/byteit101/picocli/commit/7f9d1cfd5e48fa3729adb3e36098f2bf1e32cdd0
I do plan on eventually getting around to cleaning it up to make it into an integrateable PR, but unfortunately I'm rather busy with other things for the next month or so, if anyone else wants to run with it.
@byteit101 Thank you! I need a bit more time to look in detail, but this seems like a good start!
Going to play around with my specific script for now, but this would be high impact given it's the default shell in OSX now.
That would be great! PRs are always welcome!
@yschimke I saw your message on #502. Thanks! I am still working on some parser issues, but ZSH completion is high on my list of priorities. Once I make some progress on this your feedback will be very welcome!
UPDATE 2020/11/16
I was thinking about redesigning the logic for generating completion scripts to follow the Visitor pattern; I need to prototype this a bit to see how useful this would be. Something like this:
interface ModelVisitor<R,P> {
R visit(CommandSpec command, P p);
R visit(ArgGroupSpec group, P p);
R visit(OptionSpec option, P p);
R visit(PositionalParamSpec positional, P p);
}
Current call tree:
bash(String, CommandLine)
generateEntryPointFunction
generateFunctionCallsToArrContains
[generateFunctionCallsToArrContains...] - recursively for nested subcommands
generateFunctionForCommand - for all subcommands and nested descendant sub-subcmds
generateCompletionCandidates - options with completion candidates
generateOptionsSwitch - other options for which completions can be generated
generatePositionParamCompletionCandidates - positionals with completion candidates
generatePositionalParamsCases - other positionals for which completions can be generated
Reference material:
- https://blog.mads-hartmann.com/2017/08/06/writing-zsh-completion-scripts.html
- https://thevaluable.dev/zsh-completion-guide-examples/
- https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org