diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-04-18 00:29:45 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-04-18 00:29:45 -0400 |
commit | 4d2c581e3fec28a8f71e0bd76dc4431568b7e63c (patch) | |
tree | edd9ad7163c384e051d64babf1dd967ebb8d8ea1 /src/bin/rbw/main.rs | |
parent | 95c29fd68cda5748d4e1cb3f02c5c7a0b202454b (diff) | |
download | rbw-4d2c581e3fec28a8f71e0bd76dc4431568b7e63c.tar.gz rbw-4d2c581e3fec28a8f71e0bd76dc4431568b7e63c.zip |
add shell completion support
Diffstat (limited to 'src/bin/rbw/main.rs')
-rw-r--r-- | src/bin/rbw/main.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/bin/rbw/main.rs b/src/bin/rbw/main.rs index 4380372..6c6c33e 100644 --- a/src/bin/rbw/main.rs +++ b/src/bin/rbw/main.rs @@ -2,6 +2,7 @@ use anyhow::Context as _; use std::io::Write as _; +use structopt::StructOpt as _; mod actions; mod commands; @@ -200,6 +201,11 @@ enum Opt { about = "Terminate the background agent" )] StopAgent, + #[structopt( + name = "gen-completions", + about = "Generate completion script for the given shell" + )] + GenCompletions { shell: String }, } impl Opt { @@ -223,6 +229,7 @@ impl Opt { Self::Lock => "lock".to_string(), Self::Purge => "purge".to_string(), Self::StopAgent => "stop-agent".to_string(), + Self::GenCompletions { .. } => "gen-completions".to_string(), } } } @@ -353,6 +360,7 @@ fn main(opt: Opt) { Opt::Lock => commands::lock(), Opt::Purge => commands::purge(), Opt::StopAgent => commands::stop_agent(), + Opt::GenCompletions { shell } => gen_completions(&shell), } .context(format!("rbw {}", opt.subcommand_name())); @@ -361,3 +369,16 @@ fn main(opt: Opt) { std::process::exit(1); } } + +fn gen_completions(shell: &str) -> anyhow::Result<()> { + let shell = match shell { + "bash" => structopt::clap::Shell::Bash, + "zsh" => structopt::clap::Shell::Zsh, + "fish" => structopt::clap::Shell::Fish, + "powershell" => structopt::clap::Shell::PowerShell, + "elvish" => structopt::clap::Shell::Elvish, + _ => return Err(anyhow::anyhow!("unknown shell {}", shell)), + }; + Opt::clap().gen_completions_to("rbw", shell, &mut std::io::stdout()); + Ok(()) +} |