From 61e7556997178771737771f16d6af836852ff3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sat, 27 May 2023 13:26:35 +0200 Subject: [PATCH] feat: add clap_complete for generating completion files `ahab generate ` can now be used to generte completion files for this tool. --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/cli/ahab.rs | 7 +++++++ src/main.rs | 11 ++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index df6198a..c78d215 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "clap_complete", "dotenvy", ] @@ -102,6 +103,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a04ddfaacc3bc9e6ea67d024575fafc2a813027cf374b8f24f7bc233c6b6be12" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.3.0" diff --git a/Cargo.toml b/Cargo.toml index 08e2750..8708cdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ repository = "https://github.com/janezicmatej/ahab.git" [dependencies] clap = { version = "4.3.0", features = ["derive"] } +clap_complete = "4.3.0" anyhow = "1.0.71" dotenvy = "0.15.7" diff --git a/src/cli/ahab.rs b/src/cli/ahab.rs index e278295..d95b854 100644 --- a/src/cli/ahab.rs +++ b/src/cli/ahab.rs @@ -1,5 +1,6 @@ use super::{Django, Docker, DockerCompose, Postgres}; use clap::{Parser, Subcommand}; +use clap_complete::Shell; /// A program for interacting with various dockerized applications. #[derive(Parser, Debug)] @@ -11,6 +12,12 @@ pub struct Ahab { #[derive(Debug, Subcommand)] pub enum Commands { + /// Generate completion files + Completion { + #[arg(value_enum)] + shell: Shell, + }, + /// Docker related subcommands D { #[command(subcommand)] diff --git a/src/main.rs b/src/main.rs index a18e912..dbdcfb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use ahab::{cli, scripts}; use anyhow::Result; -use clap::Parser; +use clap::{CommandFactory, Parser}; fn main() -> Result<()> { // always load dotenv on start @@ -13,6 +13,15 @@ fn main() -> Result<()> { let args = cli::Ahab::parse(); match args.command { + cli::Commands::Completion { shell } => { + clap_complete::generate( + shell, + &mut cli::Ahab::command(), + "ahab", + &mut std::io::stdout(), + ); + Ok(()) + } cli::Commands::D { command } => match command { cli::Docker::StopAll => scripts::docker::stop_all(), },