From d8f71a5f2f0bd2c5f3433c11249c5e694c2bb73c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 15 Dec 2020 11:47:38 +0100 Subject: Add subcommand to generate CLI completion script Signed-off-by: Matthias Beyer --- src/main.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 0b80023..3c4090f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,7 @@ use std::path::PathBuf; use anyhow::Result; use anyhow::anyhow; +use clap::ArgMatches; use logcrate::debug; use walkdir::WalkDir; @@ -63,8 +64,8 @@ async fn main() -> Result<()> { let _ = env_logger::try_init()?; debug!("Debugging enabled"); - let cli = cli::cli(); - let cli = cli.get_matches(); + let app = cli::cli(); + let cli = app.get_matches(); let repo = git2::Repository::discover(PathBuf::from("."))?; let repo_path = repo.workdir() @@ -95,6 +96,7 @@ async fn main() -> Result<()> { let db_connection_config = crate::db::parse_db_connection_config(&config, &cli); match cli.subcommand() { + Some(("generate-completions", matches)) => generate_completions(matches)?, Some(("db", matches)) => crate::commands::db(db_connection_config, &config, matches)?, Some(("build", matches)) => { let conn = crate::db::establish_connection(db_connection_config)?; @@ -168,3 +170,27 @@ fn count_pkg_files(p: &Path) -> u64 { .count() as u64 } +fn generate_completions(matches: &ArgMatches) -> Result<()> { + use clap_generate::generate; + use clap_generate::generators::{Bash, Elvish, Fish, Zsh}; + + let appname = "butido"; + match matches.value_of("shell").unwrap() { // unwrap safe by clap + "bash" => { + generate::(&mut cli::cli(), appname, &mut std::io::stdout()); + }, + "elvish" => { + generate::(&mut cli::cli(), appname, &mut std::io::stdout()); + }, + "fish" => { + generate::(&mut cli::cli(), appname, &mut std::io::stdout()); + }, + "zsh" => { + generate::(&mut cli::cli(), appname, &mut std::io::stdout()); + }, + _ => unreachable!(), + } + + Ok(()) +} + -- cgit v1.2.3