From c9132c3a360dc9c8f2435e5983d4130e9927876b Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Sat, 27 Nov 2021 19:11:31 -0500 Subject: [PATCH] feat(cli): Support for tab completion --- src/args.rs | 7 +++++++ src/main.rs | 31 ++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/args.rs b/src/args.rs index 30d0b217f..ee8bb1771 100644 --- a/src/args.rs +++ b/src/args.rs @@ -15,6 +15,7 @@ pub enum Command { setting = structopt::clap::AppSettings::DontCollapseArgsInUsage )] Release(ReleaseOpt), + Completions(CompletionsOpt), } #[derive(Debug, StructOpt)] @@ -222,3 +223,9 @@ fn resolve_bool_arg(yes: bool, no: bool) -> Option { (_, _) => unreachable!("StructOpt should make this impossible"), } } + +#[derive(StructOpt, Debug, Clone)] +pub struct CompletionsOpt { + #[structopt(long, short = "s", possible_values = &clap::Shell::variants(), case_insensitive = true)] + pub shell: clap::Shell, +} diff --git a/src/main.rs b/src/main.rs index fb3034c14..673fe3c65 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use std::collections::HashSet; use std::ffi::OsStr; -use std::io::Write; +use std::io::{self, Write}; use std::path::Path; use std::process::exit; @@ -27,16 +27,25 @@ use crate::replace::{do_file_replacements, Template}; use crate::version::VersionExt; fn main() { - let args::Command::Release(ref release_matches) = args::Command::from_args(); - - let mut builder = get_logging(release_matches.logging.log_level()); - builder.init(); - - match release_workspace(release_matches) { - Ok(code) => exit(code), - Err(e) => { - log::error!("Fatal: {}", e); - exit(128); + match args::Command::from_args() { + args::Command::Release(ref release_matches) => { + let mut builder = get_logging(release_matches.logging.log_level()); + builder.init(); + + match release_workspace(release_matches) { + Ok(code) => exit(code), + Err(e) => { + log::error!("Fatal: {}", e); + exit(128); + } + } + } + args::Command::Completions(ref completion_matches) => { + args::Command::clap().gen_completions_to( + "cargo-release", + completion_matches.shell, + &mut io::stdout(), + ); } } }