From 4d2c581e3fec28a8f71e0bd76dc4431568b7e63c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 18 Apr 2021 00:29:45 -0400 Subject: add shell completion support --- src/bin/rbw/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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(()) +} -- cgit v1.2.3