diff --git a/Cargo.lock b/Cargo.lock index ecaf292..392c22d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,6 +127,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bffe91f06a11b4b9420f62103854e90867812cd5d01557f853c5ee8e791b12ae" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -201,6 +210,7 @@ name = "fblog" version = "4.5.0" dependencies = [ "clap", + "clap_complete", "handlebars", "lazy_static", "mlua", diff --git a/Cargo.toml b/Cargo.toml index 6bcbd21..edf7c47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ regex = "1" mlua = { version = "0.9", features = ["lua54", "vendored"] } lazy_static = "1.4.0" handlebars = "4" +clap_complete = "4.4.4" [dependencies.clap] version = "4" diff --git a/src/app.rs b/src/app.rs index 56d2467..71d4a06 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,8 @@ use crate::substitution::Substitution; use crate::template; -use clap::{crate_version, ArgAction}; +use clap::{crate_version, value_parser, ArgAction, ValueHint}; use clap::{Arg, Command}; +use clap_complete::Shell; pub fn app() -> Command { Command::new("fblog") @@ -17,6 +18,13 @@ pub fn app() -> Command { .conflicts_with("excluded-value") .help("adds additional values"), ) + .arg( + Arg::new("generate-completions") + .long("generate-completions") + .action(ArgAction::Set) + .hide(true) + .value_parser(value_parser!(Shell)), + ) .arg( Arg::new("message-key") .long("message-key") @@ -92,6 +100,7 @@ pub fn app() -> Command { Arg::new("INPUT") .help("Sets the input file to use, otherwise assumes stdin") .required(false) + .value_hint(ValueHint::AnyPath) .default_value("-"), ) .arg( diff --git a/src/main.rs b/src/main.rs index 607c3ae..22c5eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod substitution; mod template; use crate::log::LogSettings; +use clap_complete::{generate, Shell}; use std::fs; use substitution::Substitution; @@ -21,6 +22,14 @@ fn main() { let mut log_settings = LogSettings::new_default_settings(); + if let Some(generator) = matches.get_one::("generate-completions").copied() { + let mut app = app::app(); + let name = app.get_name().to_string(); + eprintln!("Generating completion file for {generator}..."); + generate(generator, &mut app, name, &mut io::stdout()); + return; + } + if let Some(values) = matches.get_many::("additional-value") { log_settings.add_additional_values(values.map(ToOwned::to_owned).collect()); }