aopt-shell
Shell completion support for aopt framework.
Example
use std::ffi::OsString;
use std::path::PathBuf;
use cote::prelude::*;
use cote::shell::get_complete_cli;
use cote::shell::shell::Complete;
use cote::shell::value::once_values;
#[derive(Debug, Cote)]
pub struct Cli {
debug: bool,
count: Option<i64>,
files: Vec<PathBuf>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
match get_complete_cli() {
Ok(cli) => {
if cli.write_stdout("fput", "fput").is_err() {
cli.complete(|shell| {
let mut ctx = cli.get_context()?;
let parser = Cli::into_parser()?;
shell.set_buff(std::io::stdout());
ctx.set_values("--count", ["42", "56"]);
ctx.set_values(
"--files",
once_values(|_| {
Ok(["files/a.txt", "files/b.txt", "files/c.txt"]
.map(OsString::from)
.into_iter()
.collect())
}),
);
parser.complete(shell, &mut ctx)?;
Ok(())
})?;
}
}
Err(_) => {
let cli = Cli::parse_env()?;
println!("doing normal cli things..: {cli:?}");
}
}
Ok(())
}
Deploy shell completions
PROGRAM is the name of binary
bash
echo 'source <(PROGRAM --_shell bash)' >> ~/.bashrc
fish
echo 'PROGRAM --_shell fish | source' >> ~/.config/fish/config.fish
Zsh
echo 'source <(PROGRAM --_shell zsh)' >> ~/.zshrc
Powershell
Add-Content $PROFILE "`nPROGRAM --_shell powershell | Out-String | Invoke-Expression"
More
A simple file search tools, try it using cargo install --path simple-find-file.
Get the follow count of stock in xueqiu.com, try it using cargo install --path snowball-follow
LICENSE
MPL-2.0