Skip to content

Commit 0bef8ea

Browse files
committed
Print argument parse errors as JSON
1 parent cf4d64d commit 0bef8ea

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tmc-langs-cli/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,25 @@ pub fn run() {
7575
// sets up warning and progress reporting and calls run_app and does error handling for its result
7676
// returns Ok if we should exit with code 0, Err if we should exit with 1
7777
fn run_inner() -> Result<(), ()> {
78-
let matches = Opt::parse();
78+
let matches = match Opt::try_parse().context("Failed to parse arguments") {
79+
Ok(matches) => matches,
80+
Err(e) => {
81+
// CLI was called incorrectly
82+
let causes: Vec<String> = e.chain().map(|e| format!("Caused by: {}", e)).collect();
83+
let error_output = OutputKind::OutputData(Box::new(OutputData {
84+
status: Status::Finished,
85+
message: e.to_string(),
86+
result: OutputResult::Error,
87+
data: Some(DataKind::Error {
88+
kind: Kind::Generic,
89+
trace: causes,
90+
}),
91+
}));
92+
print_output(&error_output, false)
93+
.expect("failed to print output");
94+
return Err(());
95+
}
96+
};
7997
let pretty = matches.pretty;
8098

8199
notification_reporter::init(Box::new(move |warning| {

0 commit comments

Comments
 (0)