difftreelog
build upgrade clap to v3.1
in: master
8 files changed
cmds/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" }
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use clap::{AppSettings, Clap, IntoApp};2use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};3use jrsonnet_evaluator::{error::LocError, EvaluationState};4use std::{5 fs::{create_dir_all, File},6 io::Read,7 io::Write,8 path::PathBuf,9 str::FromStr,10};1112#[cfg(feature = "mimalloc")]13#[global_allocator]14static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1516#[derive(Clap)]17#[clap(help_heading = "DEBUG")]18struct DebugOpts {19 /// Required OS stack size.20 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.21 #[clap(long, name = "size")]22 pub os_stack: Option<usize>,23 /// Generate completions script24 #[clap(long)]25 generate: Option<GenerateTarget>,26}2728enum GenerateTarget {29 Bash,30 Zsh,31 Fish,32 PowerShell,33}34impl FromStr for GenerateTarget {35 type Err = &'static str;3637 fn from_str(s: &str) -> Result<Self, Self::Err> {38 match s {39 "bash" => Ok(Self::Bash),40 "zsh" => Ok(Self::Zsh),41 "fish" => Ok(Self::Fish),42 "powershell" => Ok(Self::PowerShell),43 _ => Err("unknown target"),44 }45 }46}4748#[derive(Clap)]49#[clap(50 global_setting = AppSettings::ColoredHelp,51 global_setting = AppSettings::DeriveDisplayOrder,52)]53struct Opts {54 #[clap(flatten)]55 input: InputOpts,56 #[clap(flatten)]57 general: GeneralOpts,58 #[clap(flatten)]59 manifest: ManifestOpts,60 #[clap(flatten)]61 output: OutputOpts,62 #[clap(flatten)]63 debug: DebugOpts,64 #[clap(flatten)]65 gc: GcOpts,66}6768fn main() {69 let opts: Opts = Opts::parse();7071 if let Some(target) = opts.debug.generate {72 use clap_generate::{generate, generators};73 use GenerateTarget::*;74 let app = &mut Opts::into_app();75 let buf = &mut std::io::stdout();76 let bin = "jrsonnet";77 match target {78 Bash => generate::<generators::Bash, _>(app, bin, buf),79 Zsh => generate::<generators::Zsh, _>(app, bin, buf),80 Fish => generate::<generators::Fish, _>(app, bin, buf),81 PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),82 }83 std::process::exit(0);84 };8586 let success = if let Some(size) = opts.debug.os_stack {87 std::thread::Builder::new()88 .stack_size(size * 1024 * 1024)89 .spawn(|| main_catch(opts))90 .expect("new thread spawned")91 .join()92 .expect("thread finished successfully")93 } else {94 main_catch(opts)95 };96 if !success {97 std::process::exit(1);98 }99}100101#[derive(thiserror::Error, Debug)]102enum Error {103 // Handled differently104 #[error("evaluation error")]105 Evaluation(jrsonnet_evaluator::error::LocError),106 #[error("io error")]107 Io(#[from] std::io::Error),108 #[error("input is not utf8 encoded")]109 Utf8(#[from] std::str::Utf8Error),110}111impl From<LocError> for Error {112 fn from(e: LocError) -> Self {113 Self::Evaluation(e)114 }115}116117fn main_catch(opts: Opts) -> bool {118 let _printer = opts.gc.stats_printer();119 let state = EvaluationState::default();120 if let Err(e) = main_real(&state, opts) {121 if let Error::Evaluation(e) = e {122 eprintln!("{}", state.stringify_err(&e));123 } else {124 eprintln!("{}", e);125 }126 return false;127 }128 true129}130131fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> {132 opts.gc.configure_global();133 opts.general.configure(state)?;134 opts.manifest.configure(state)?;135136 let val = if opts.input.exec {137 state.evaluate_snippet_raw(138 PathBuf::from("<cmdline>").into(),139 (&opts.input.input as &str).into(),140 )?141 } else if opts.input.input == "-" {142 let mut input = Vec::new();143 std::io::stdin().read_to_end(&mut input)?;144 let input_str = std::str::from_utf8(&input)?.into();145 state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?146 } else {147 state.evaluate_file_raw(&PathBuf::from(opts.input.input))?148 };149150 let val = state.with_tla(val)?;151152 if let Some(multi) = opts.output.multi {153 if opts.output.create_output_dirs {154 let mut dir = multi.clone();155 dir.pop();156 create_dir_all(dir)?;157 }158 for (file, data) in state.manifest_multi(val)?.iter() {159 let mut path = multi.clone();160 path.push(file as &str);161 if opts.output.create_output_dirs {162 let mut dir = path.clone();163 dir.pop();164 create_dir_all(dir)?;165 }166 println!("{}", path.to_str().expect("path"));167 let mut file = File::create(path)?;168 writeln!(file, "{}", data)?;169 }170 } else if let Some(path) = opts.output.output_file {171 if opts.output.create_output_dirs {172 let mut dir = path.clone();173 dir.pop();174 create_dir_all(dir)?;175 }176 let mut file = File::create(path)?;177 writeln!(file, "{}", state.manifest(val)?)?;178 } else {179 let output = state.manifest(val)?;180 if !output.is_empty() {181 println!("{}", output);182 }183 }184185 Ok(())186}crates/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"] }
crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,5 +1,5 @@
use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState};
use std::{fs::read_to_string, str::FromStr};
@@ -53,8 +53,8 @@
}
}
-#[derive(Clap)]
-#[clap(help_heading = "EXTERNAL VARIABLES")]
+#[derive(Parser)]
+#[clap(next_help_heading = "EXTERNAL VARIABLES")]
pub struct ExtVarOpts {
/// Add string external variable.
/// External variables are globally available so it is preferred
crates/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)]
crates/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')]
crates/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.
crates/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