Shell-agnostic, extensible CLI completion for Rust 💊
supplement is a Rust library that derives completion scaffolds as Rust code.
Give it a clap object, and instead of spitting out shell files that you later have to manually edit, it spits out Rust! supplement is:
- Shell-agnostic
- No more generated code - instead, derive them.
- Easy to test and debug - Functions and objects in a modern programming language, instead of some shell script black sorcery.
- Context-aware - Track the "parsed CLI values" for you so you don't have to.
- It's Rust 🦀
Install
Add one line in Cargo.toml. By default, it uses clap 4, but you can make it use clap 3 with features.
[]
= "0.2"
# Or, to use clap 3
= { = "0.2", = false, = ["clap-3"] }
Quick start
Say you have some awesome clap definition, and want to use supplement to make it even more awesome. Derive trait Supplement for your definitions.
// src/args.rs
use ;
use Supplement;
// more definition...
Implementation
Call function Supplement::supplement and start implementing your completion logic.
Note that, if you missed some scenario, it's a compile time error. So just relex and let Rust get your back 💪
use ;
Install to system
After implementing everything, compile it to binary file, and create a shell completion script to tell the shell how to use the binary. Here's an example for Fish shell:
# Put this to /usr/share/fish/completions/git.fish or ~/.config/fish/completions/git.fish
function __do_completion
set cmd (commandline -c)
set cmd_arr (string split ' ' $cmd)
set cur "$cmd_arr[-1]"
if [ -z "$cur" ]
# preserve the last white space
echo $cmd "''" | xargs path/to/your/binary
else
echo $cmd | xargs path/to/your/binary
end
if [ "$status" != "0" ]
# fall back to default completion
complete -C "'' $cur"
end
end
complete -k -c git -x -a "(__do_completion)"
The scripts for all supported shells can be found in examples/shell.
A complete example can be found in examples/README.md.