git.delta.rocks / jrsonnet / refs/commits / 4744c15d089b

difftreelog

feat generate completion scripts

Yaroslav Bolyukin2021-03-27parent: #ee34bba.patch.diff
in: master

2 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
before · cmds/jrsonnet/Cargo.toml
1[package]2name = "jrsonnet"3description = "Rust jsonnet implementation"4version = "0.3.4"5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]6license = "MIT"7edition = "2018"89[features]10default = []11# Use mimalloc as allocator12mimalloc = []1314[dependencies]15jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.4" }16jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.4" }17jrsonnet-cli = { path = "../../crates/jrsonnet-cli", version = "0.3.4" }18# TODO: Fix mimalloc compile errors, and use them19mimallocator = { version = "0.1.3", optional = true }20thiserror = "1.0"2122[dependencies.clap]23git = "https://github.com/clap-rs/clap"24rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"
after · cmds/jrsonnet/Cargo.toml
1[package]2name = "jrsonnet"3description = "Rust jsonnet implementation"4version = "0.3.4"5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]6license = "MIT"7edition = "2018"89[features]10default = []11# Use mimalloc as allocator12mimalloc = []1314[dependencies]15jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.4" }16jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.4" }17jrsonnet-cli = { path = "../../crates/jrsonnet-cli", version = "0.3.4" }18# TODO: Fix mimalloc compile errors, and use them19mimallocator = { version = "0.1.3", optional = true }20thiserror = "1.0"2122[dependencies.clap]23git = "https://github.com/clap-rs/clap"24rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"2526[dependencies.clap_generate]27git = "https://github.com/clap-rs/clap"28rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- 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<usize>,
+	/// Generate completions script
+	#[clap(long)]
+	generate: Option<GenerateTarget>,
+}
+
+enum GenerateTarget {
+	Bash,
+	Zsh,
+	Fish,
+	PowerShell,
 }
+impl FromStr for GenerateTarget {
+	type Err = &'static str;
 
+	fn from_str(s: &str) -> Result<Self, Self::Err> {
+		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::<generators::Bash, _>(app, bin, buf),
+			Zsh => generate::<generators::Zsh, _>(app, bin, buf),
+			Fish => generate::<generators::Fish, _>(app, bin, buf),
+			PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),
+		}
+		std::process::exit(0);
+	};
+
 	let success;
 	if let Some(size) = opts.debug.os_stack {
 		success = std::thread::Builder::new()