Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(json): name missing arguments in usage errors (#1459) (#1493)

πŸ„‚Κα΄‡α΄… πŸ„°Κ™α΄…α΄œΚŸ πŸ„°α΄α΄€πŸ„ ✧ccc21c4b11da
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 03b26ace..0f888664 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@
cell: `package` lays out npm packages only for the front door of a Node CLI (run as
`node node_modules/<package>/<file>`) and refuses any other, such as `python3 tool.py`. The test
of that refusal now checks its message (#1472, #1494 by @iyazici00).
+- A missing argument under `--json` names every missing argument in the error's message, as in
+ `the following required arguments were not provided: <REPO>, <UNIT>`, where it stopped at the
+ colon. Other usage errors keep their message (#1459, #1493 by @P-r-e-m-i-u-m).
- `analyze` maps `laravel/framework` to Laravel Rust (`laravel-rust`, beta), with fit `partial`.
Units that import `Illuminate\...` are no longer Blocked with "`laravel/framework` has no Rust
equivalent". `analyze --whole-app` on an app whose `composer.json` requires `laravel/framework`
diff --git a/src/main.rs b/src/main.rs
index 9432136e..99fb6514 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1305,7 +1305,7 @@ fn run() -> Result<ExitCode> {
let command = match Cli::try_parse() {
Ok(cli) => cli.command,
Err(e) => {
- use clap::error::ErrorKind::{DisplayHelp, DisplayVersion};
+ use clap::error::ErrorKind::{DisplayHelp, DisplayVersion, MissingRequiredArgument};
match e.kind() {
// Not errors: print exactly what `Cli::parse()` prints today.
DisplayHelp | DisplayVersion => {
@@ -1316,7 +1316,22 @@ fn run() -> Result<ExitCode> {
if wants_json() {
let rendered = e.to_string();
let first_line = rendered.lines().next().unwrap_or_default();
- let message = first_line.strip_prefix("error: ").unwrap_or(first_line);
+ let lead = first_line.strip_prefix("error: ").unwrap_or(first_line);
+ let message = if matches!(e.kind(), MissingRequiredArgument) {
+ let names: Vec<&str> = rendered
+ .lines()
+ .skip(1)
+ .map(str::trim)
+ .take_while(|line| !line.is_empty() && !line.starts_with("Usage:"))
+ .collect();
+ if names.is_empty() {
+ lead.to_string()
+ } else {
+ format!("{lead} {}", names.join(", "))
+ }
+ } else {
+ lead.to_string()
+ };
println!(
"{}",
serde_json::json!({
diff --git a/tests/json_output.rs b/tests/json_output.rs
index a1b06eca..cee7240b 100644
--- a/tests/json_output.rs
+++ b/tests/json_output.rs
@@ -190,6 +190,18 @@ fn a_failure_under_json_from_usage_is_json_on_stdout() {
assert_eq!(out.status.code(), Some(2));
assert!(out.stdout.is_empty(), "without --json stdout stays empty");
}
+
+ let out = run(&["app", "wire", "--json"], None);
+ assert_eq!(out.status.code(), Some(2));
+ let v = json(&out);
+ assert_eq!(v["schema"], "rust-anything/error@1");
+ assert_eq!(v["error"]["code"], "usage");
+ let message = v["error"]["message"].as_str().unwrap();
+ assert!(message.contains("<REPO>"), "{message}");
+ assert!(message.contains("<UNIT>"), "{message}");
+ let plain = run(&["app", "wire"], None);
+ assert_eq!(plain.status.code(), Some(2));
+ assert!(plain.stdout.is_empty(), "without --json stdout stays empty");
}
#[test]