From 4788b330ed0758c3e95cadb6ffe32a1c1d542bc3 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 17 Aug 2021 18:12:49 -0400 Subject: [PATCH] add shell completion generation --- src/main.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 825e4c7..2b57a60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate rpassword; -use clap::{crate_version, App, Arg}; +use clap::{crate_version, App, Arg, Shell}; use log::*; +use std::str::FromStr; use std::{ io::{stdout, Write}, path::Path, @@ -20,8 +21,8 @@ mod accountmanager; mod demos; mod tui; -fn main() { - let matches = App::new("steamguard-cli") +fn cli() -> App<'static, 'static> { + App::new("steamguard-cli") .version(crate_version!()) .bin_name("steamguard") .author("dyc3 (Carson McManus)") @@ -62,6 +63,16 @@ fn main() { .takes_value(false) .multiple(true) ) + .subcommand( + App::new("completion") + .about("Generate shell completions") + .arg( + Arg::with_name("shell") + .long("shell") + .takes_value(true) + .possible_values(&Shell::variants()) + ) + ) .subcommand( App::new("trade") .about("Interactive interface for trade confirmations") @@ -98,7 +109,10 @@ fn main() { .takes_value(false) ) ) - .get_matches(); +} + +fn main() { + let matches = cli().get_matches(); let verbosity = matches.occurrences_of("verbosity") as usize + 2; stderrlog::new() @@ -114,6 +128,14 @@ fn main() { } return; } + if let Some(completion_matches) = matches.subcommand_matches("completion") { + cli().gen_completions_to( + "steamguard", + Shell::from_str(completion_matches.value_of("shell").unwrap()).unwrap(), + &mut std::io::stdout(), + ); + return; + } let mafiles_dir = if matches.occurrences_of("mafiles-path") > 0 { matches.value_of("mafiles-path").unwrap().into()