From bc8f1101926350ad66a29d9a170219d91c617ad2 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Wed, 24 Apr 2024 15:34:36 +0900 Subject: [PATCH] Generate shell completions at build time --- b3sum/Cargo.lock | 10 ++++++++++ b3sum/Cargo.toml | 1 + b3sum/build.rs | 18 ++++++++++++++++++ b3sum/src/cli.rs | 3 ++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/b3sum/Cargo.lock b/b3sum/Cargo.lock index d9043f1..e5d2322 100644 --- a/b3sum/Cargo.lock +++ b/b3sum/Cargo.lock @@ -75,6 +75,7 @@ dependencies = [ "anyhow", "blake3", "clap", + "clap_complete", "clap_mangen", "duct", "hex", @@ -137,6 +138,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79504325bf38b10165b02e89b4347300f855f273c4cb30c4a3209e6583275e" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.0" diff --git a/b3sum/Cargo.toml b/b3sum/Cargo.toml index 4bc7649..436ded5 100644 --- a/b3sum/Cargo.toml +++ b/b3sum/Cargo.toml @@ -28,4 +28,5 @@ tempfile = "3.1.0" [build-dependencies] blake3 = { version = "1", path = ".." } clap = { version = "4.0.8", features = ["derive"] } +clap_complete = "4.5.2" clap_mangen = "0.2.20" diff --git a/b3sum/build.rs b/b3sum/build.rs index 844edc1..b169b9b 100644 --- a/b3sum/build.rs +++ b/b3sum/build.rs @@ -2,6 +2,23 @@ use clap::CommandFactory; include!("src/cli.rs"); +fn generate_completions(out_dir: &std::path::Path) -> std::io::Result<()> { + fn generate_to( + gen: impl clap_complete::Generator, + out_dir: &std::path::Path, + ) -> std::io::Result { + let mut command = Inner::command(); + clap_complete::generate_to(gen, &mut command, "b3sum", out_dir) + } + + generate_to(clap_complete::Shell::Bash, out_dir)?; + generate_to(clap_complete::Shell::Elvish, out_dir)?; + generate_to(clap_complete::Shell::Fish, out_dir)?; + generate_to(clap_complete::Shell::PowerShell, out_dir)?; + generate_to(clap_complete::Shell::Zsh, out_dir)?; + Ok(()) +} + fn generate_man_page(out_dir: &std::path::Path) -> std::io::Result<()> { let command = Inner::command(); @@ -54,6 +71,7 @@ fn main() -> std::io::Result<()> { let out_dir = std::env::var("OUT_DIR").expect("environment variable `OUT_DIR` not defined"); let out_dir = std::path::PathBuf::from(out_dir); + generate_completions(&out_dir)?; generate_man_page(&out_dir)?; Ok(()) } diff --git a/b3sum/src/cli.rs b/b3sum/src/cli.rs index 9a5bf2e..8cb104f 100644 --- a/b3sum/src/cli.rs +++ b/b3sum/src/cli.rs @@ -1,4 +1,4 @@ -use clap::Parser; +use clap::{Parser, ValueHint}; use std::path::PathBuf; const DERIVE_KEY_ARG: &str = "derive_key"; @@ -17,6 +17,7 @@ pub struct Inner { /// Files to hash, or checkfiles to check /// /// When no file is given, or when - is given, read standard input. + #[arg(value_hint(ValueHint::FilePath))] pub file: Vec, /// Use the keyed mode, reading the 32-byte key from stdin