--- a/cmds/jrsonnet/Cargo.toml +++ b/cmds/jrsonnet/Cargo.toml @@ -22,3 +22,7 @@ [dependencies.clap] git = "https://github.com/clap-rs/clap" rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a" + +[dependencies.clap_generate] +git = "https://github.com/clap-rs/clap" +rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a" --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -1,5 +1,4 @@ -use clap::AppSettings; -use clap::Clap; +use clap::{AppSettings, Clap, IntoApp}; use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts}; use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat}; use std::{ @@ -8,6 +7,7 @@ io::Write, path::PathBuf, rc::Rc, + str::FromStr, }; #[cfg(feature = "mimalloc")] @@ -21,8 +21,31 @@ /// This shouldn't be changed unless jrsonnet is failing with stack overflow error. #[clap(long, name = "size")] pub os_stack: Option, + /// Generate completions script + #[clap(long)] + generate: Option, +} + +enum GenerateTarget { + Bash, + Zsh, + Fish, + PowerShell, } +impl FromStr for GenerateTarget { + type Err = &'static str; + fn from_str(s: &str) -> Result { + match s { + "bash" => Ok(Self::Bash), + "zsh" => Ok(Self::Zsh), + "fish" => Ok(Self::Fish), + "powershell" => Ok(Self::PowerShell), + _ => Err("unknown target"), + } + } +} + #[derive(Clap)] #[clap( global_setting = AppSettings::ColoredHelp, @@ -43,6 +66,22 @@ fn main() { let opts: Opts = Opts::parse(); + + if let Some(target) = opts.debug.generate { + use clap_generate::{generate, generators}; + use GenerateTarget::*; + let app = &mut Opts::into_app(); + let buf = &mut std::io::stdout(); + let bin = "jrsonnet"; + match target { + Bash => generate::(app, bin, buf), + Zsh => generate::(app, bin, buf), + Fish => generate::(app, bin, buf), + PowerShell => generate::(app, bin, buf), + } + std::process::exit(0); + }; + let success; if let Some(size) = opts.debug.os_stack { success = std::thread::Builder::new()