summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-12-15 11:47:38 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-12-15 11:47:43 +0100
commitd8f71a5f2f0bd2c5f3433c11249c5e694c2bb73c (patch)
tree5420b2062af40e272b7b3a64c02906a5927a13a0 /src/main.rs
parentf4592ab5d21f4e4dcd23f0153699b27b687bfc65 (diff)
Add subcommand to generate CLI completion script
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs30
1 files changed, 28 insertions, 2 deletions
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::<Bash, _>(&mut cli::cli(), appname, &mut std::io::stdout());
+ },
+ "elvish" => {
+ generate::<Elvish, _>(&mut cli::cli(), appname, &mut std::io::stdout());
+ },
+ "fish" => {
+ generate::<Fish, _>(&mut cli::cli(), appname, &mut std::io::stdout());
+ },
+ "zsh" => {
+ generate::<Zsh, _>(&mut cli::cli(), appname, &mut std::io::stdout());
+ },
+ _ => unreachable!(),
+ }
+
+ Ok(())
+}
+