git.delta.rocks / jrsonnet / refs/commits / 9efbcad25b9d

difftreelog

build upgrade clap to v3.1

Yaroslav Bolyukin2022-03-06parent: #4ec6326.patch.diff
in: master

8 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -18,11 +18,5 @@
 mimallocator = { version = "0.1.3", optional = true }
 thiserror = "1.0"
 gcmodule = { git = "https://github.com/CertainLach/gcmodule", branch = "jrsonnet" }
-
-[dependencies.clap]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
-
-[dependencies.clap_generate]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+clap = { version = "3.1", features = ["derive"] }
+clap_complete = { version = "3.1" }
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,4 +1,5 @@
-use clap::{AppSettings, Clap, IntoApp};
+use clap::{AppSettings, IntoApp, Parser};
+use clap_complete::Shell;
 use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
 use jrsonnet_evaluator::{error::LocError, EvaluationState};
 use std::{
@@ -6,51 +7,39 @@
 	io::Read,
 	io::Write,
 	path::PathBuf,
-	str::FromStr,
 };
 
 #[cfg(feature = "mimalloc")]
 #[global_allocator]
 static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;
 
-#[derive(Clap)]
-#[clap(help_heading = "DEBUG")]
+#[derive(Parser)]
+enum SubOpts {
+	/// Generate completions for specified shell
+	Generate {
+		/// Target shell name
+		shell: Shell,
+	},
+}
+
+#[derive(Parser)]
+#[clap(next_help_heading = "DEBUG")]
 struct DebugOpts {
 	/// Required OS stack size.
 	/// 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)]
+#[derive(Parser)]
 #[clap(
-	global_setting = AppSettings::ColoredHelp,
 	global_setting = AppSettings::DeriveDisplayOrder,
+	// args_conflicts_with_subcommands = true,
 )]
 struct Opts {
+	#[clap(subcommand)]
+	sub: Option<SubOpts>,
+
 	#[clap(flatten)]
 	input: InputOpts,
 	#[clap(flatten)]
@@ -68,20 +57,17 @@
 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),
+	if let Some(sub) = opts.sub {
+		match sub {
+			SubOpts::Generate { shell } => {
+				use clap_complete::generate;
+				let app = &mut Opts::command();
+				let buf = &mut std::io::stdout();
+				generate(shell, app, "jrsonnet", buf);
+				std::process::exit(0)
+			}
 		}
-		std::process::exit(0);
-	};
+	}
 
 	let success = if let Some(size) = opts.debug.os_stack {
 		std::thread::Builder::new()
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -14,6 +14,4 @@
 jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.4.2" }
 gcmodule = { git = "https://github.com/CertainLach/gcmodule", branch = "jrsonnet" }
 
-[dependencies.clap]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+clap = { version = "3.1", features = ["derive"] }
modifiedcrates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth
before · crates/jrsonnet-cli/src/ext.rs
1use crate::ConfigureState;2use clap::Clap;3use jrsonnet_evaluator::{error::Result, EvaluationState};4use std::{fs::read_to_string, str::FromStr};56#[derive(Clone)]7pub struct ExtStr {8	pub name: String,9	pub value: String,10}1112impl FromStr for ExtStr {13	type Err = &'static str;14	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {15		let out: Vec<_> = s.split('=').collect();16		match out.len() {17			1 => Ok(ExtStr {18				name: out[0].to_owned(),19				value: std::env::var(out[0]).or(Err("missing env var"))?,20			}),21			2 => Ok(ExtStr {22				name: out[0].to_owned(),23				value: out[1].to_owned(),24			}),2526			_ => Err("bad ext-str syntax"),27		}28	}29}3031#[derive(Clone)]32pub struct ExtFile {33	pub name: String,34	pub value: String,35}3637impl FromStr for ExtFile {38	type Err = String;3940	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {41		let out: Vec<&str> = s.split('=').collect();42		if out.len() != 2 {43			return Err("bad ext-file syntax".to_owned());44		}45		let file = read_to_string(&out[1]);46		match file {47			Ok(content) => Ok(Self {48				name: out[0].into(),49				value: content,50			}),51			Err(e) => Err(format!("{}", e)),52		}53	}54}5556#[derive(Clap)]57#[clap(help_heading = "EXTERNAL VARIABLES")]58pub struct ExtVarOpts {59	/// Add string external variable.60	/// External variables are globally available so it is preferred61	/// to use top level arguments whenever it's possible.62	/// If [=data] is not set then it will be read from `name` env variable.63	/// Can be accessed from code via `std.extVar("name")`.64	#[clap(65		long,66		short = 'V',67		name = "name[=var data]",68		number_of_values = 1,69		multiple_occurrences = true70	)]71	ext_str: Vec<ExtStr>,72	/// Read string external variable from file.73	/// See also `--ext-str`74	#[clap(75		long,76		name = "name=var path",77		number_of_values = 1,78		multiple_occurrences = true79	)]80	ext_str_file: Vec<ExtFile>,81	/// Add external variable from code.82	/// See also `--ext-str`83	#[clap(84		long,85		name = "name[=var source]",86		number_of_values = 1,87		multiple_occurrences = true88	)]89	ext_code: Vec<ExtStr>,90	/// Read string external variable from file.91	/// See also `--ext-str`92	#[clap(93		long,94		name = "name=var code path",95		number_of_values = 1,96		multiple_occurrences = true97	)]98	ext_code_file: Vec<ExtFile>,99}100impl ConfigureState for ExtVarOpts {101	fn configure(&self, state: &EvaluationState) -> Result<()> {102		for ext in self.ext_str.iter() {103			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());104		}105		for ext in self.ext_str_file.iter() {106			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());107		}108		for ext in self.ext_code.iter() {109			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;110		}111		for ext in self.ext_code_file.iter() {112			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;113		}114		Ok(())115	}116}
after · crates/jrsonnet-cli/src/ext.rs
1use crate::ConfigureState;2use clap::Parser;3use jrsonnet_evaluator::{error::Result, EvaluationState};4use std::{fs::read_to_string, str::FromStr};56#[derive(Clone)]7pub struct ExtStr {8	pub name: String,9	pub value: String,10}1112impl FromStr for ExtStr {13	type Err = &'static str;14	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {15		let out: Vec<_> = s.split('=').collect();16		match out.len() {17			1 => Ok(ExtStr {18				name: out[0].to_owned(),19				value: std::env::var(out[0]).or(Err("missing env var"))?,20			}),21			2 => Ok(ExtStr {22				name: out[0].to_owned(),23				value: out[1].to_owned(),24			}),2526			_ => Err("bad ext-str syntax"),27		}28	}29}3031#[derive(Clone)]32pub struct ExtFile {33	pub name: String,34	pub value: String,35}3637impl FromStr for ExtFile {38	type Err = String;3940	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {41		let out: Vec<&str> = s.split('=').collect();42		if out.len() != 2 {43			return Err("bad ext-file syntax".to_owned());44		}45		let file = read_to_string(&out[1]);46		match file {47			Ok(content) => Ok(Self {48				name: out[0].into(),49				value: content,50			}),51			Err(e) => Err(format!("{}", e)),52		}53	}54}5556#[derive(Parser)]57#[clap(next_help_heading = "EXTERNAL VARIABLES")]58pub struct ExtVarOpts {59	/// Add string external variable.60	/// External variables are globally available so it is preferred61	/// to use top level arguments whenever it's possible.62	/// If [=data] is not set then it will be read from `name` env variable.63	/// Can be accessed from code via `std.extVar("name")`.64	#[clap(65		long,66		short = 'V',67		name = "name[=var data]",68		number_of_values = 1,69		multiple_occurrences = true70	)]71	ext_str: Vec<ExtStr>,72	/// Read string external variable from file.73	/// See also `--ext-str`74	#[clap(75		long,76		name = "name=var path",77		number_of_values = 1,78		multiple_occurrences = true79	)]80	ext_str_file: Vec<ExtFile>,81	/// Add external variable from code.82	/// See also `--ext-str`83	#[clap(84		long,85		name = "name[=var source]",86		number_of_values = 1,87		multiple_occurrences = true88	)]89	ext_code: Vec<ExtStr>,90	/// Read string external variable from file.91	/// See also `--ext-str`92	#[clap(93		long,94		name = "name=var code path",95		number_of_values = 1,96		multiple_occurrences = true97	)]98	ext_code_file: Vec<ExtFile>,99}100impl ConfigureState for ExtVarOpts {101	fn configure(&self, state: &EvaluationState) -> Result<()> {102		for ext in self.ext_str.iter() {103			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());104		}105		for ext in self.ext_str_file.iter() {106			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());107		}108		for ext in self.ext_code.iter() {109			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;110		}111		for ext in self.ext_code_file.iter() {112			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;113		}114		Ok(())115	}116}
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -8,7 +8,7 @@
 pub use tla::*;
 pub use trace::*;
 
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
 use std::{env, path::PathBuf};
 
@@ -16,24 +16,19 @@
 	fn configure(&self, state: &EvaluationState) -> Result<()>;
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "INPUT")]
+#[derive(Parser)]
+#[clap(next_help_heading = "INPUT")]
 pub struct InputOpts {
-	#[clap(
-		long,
-		short = 'e',
-		about = "Treat input as code, evaluate them instead of reading file"
-	)]
+	/// Treat input as code, evaluate them instead of reading file
+	#[clap(long, short = 'e')]
 	pub exec: bool,
 
-	#[clap(
-		about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"
-	)]
+	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself
 	pub input: String,
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "OPTIONS")]
+#[derive(Parser)]
+#[clap(next_help_heading = "OPTIONS")]
 pub struct MiscOpts {
 	/// Disable standard library.
 	/// By default standard library will be available via global `std` variable.
@@ -74,7 +69,7 @@
 }
 
 /// General configuration of jsonnet
-#[derive(Clap)]
+#[derive(Parser)]
 #[clap(name = "jrsonnet", version, author)]
 pub struct GeneralOpts {
 	#[clap(flatten)]
@@ -100,8 +95,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "GARBAGE COLLECTION")]
+#[derive(Parser)]
+#[clap(next_help_heading = "GARBAGE COLLECTION")]
 pub struct GcOpts {
 	/// Do not skip gc on exit
 	#[clap(long)]
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,5 +1,5 @@
 use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};
 use std::{path::PathBuf, str::FromStr};
 
@@ -22,8 +22,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "MANIFESTIFICATION OUTPUT")]
+#[derive(Parser)]
+#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]
 pub struct ManifestOpts {
 	/// Output format, wraps resulting value to corresponding std.manifest call.
 	/// If set to `string` then plain string value is expected to be returned,
@@ -66,7 +66,7 @@
 	}
 }
 
-#[derive(Clap)]
+#[derive(Parser)]
 pub struct OutputOpts {
 	/// Write to the output file rather than stdout
 	#[clap(long, short = 'o')]
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,9 +1,9 @@
 use crate::{ConfigureState, ExtFile, ExtStr};
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
 
-#[derive(Clap)]
-#[clap(help_heading = "TOP LEVEL ARGUMENTS")]
+#[derive(Parser)]
+#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
 pub struct TLAOpts {
 	/// Add top level string argument.
 	/// Top level arguments will be passed to function before manifestification stage.
modifiedcrates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -1,5 +1,5 @@
 use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{
 	error::Result,
 	trace::{CompactFormat, ExplainingFormat, PathResolver},
@@ -24,8 +24,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "STACK TRACE VISUAL")]
+#[derive(Parser)]
+#[clap(next_help_heading = "STACK TRACE VISUAL")]
 pub struct TraceOpts {
 	/// Format of stack traces' display in console.
 	/// `compact` format only shows `filename:line:column`s