fix(rome_cli): print the correct subcommand in error messages
MODIFY
crates/rome_cli/src/commands/format.rs
crates/rome_cli/src/commands/format.rs
@@ -36,7 +36,10 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
Some((path, input_code))
} else {
// we provided the argument without a piped stdin, we bail
- return Err(Termination::MissingArgument { argument: "stdin" });
+ return Err(Termination::MissingArgument {
+ subcommand: "format",
+ argument: "stdin",
+ });
}
} else {
None

MODIFY
crates/rome_cli/src/execute.rs
crates/rome_cli/src/execute.rs
@@ -117,6 +117,15 @@ impl Execution {
_ => None,
}
}
+
+ /// Returns the subcommand of the [traversal mode](TraversalMode) execution
+ pub(crate) fn traversal_mode_subcommand(&self) -> &'static str {
+ match self.traversal_mode {
+ TraversalMode::Check { .. } => "check",
+ TraversalMode::CI { .. } => "ci",
+ TraversalMode::Format { .. } => "format",
+ }
+ }
}
/// Based on the [mode](ExecutionMode), the function might launch a traversal of the file system

MODIFY
crates/rome_cli/src/termination.rs
crates/rome_cli/src/termination.rs
@@ -27,17 +27,23 @@ pub enum Termination {
/// Returned when the CLI doesn't recognize a command line argument
#[error(
- "unrecognized option {argument:?}. Type '{} format --help' for more information.",
+ "unrecognized option {argument:?}. Type '{} {subcommand} --help' for more information.",
command_name()
)]
- UnexpectedArgument { argument: OsString },
+ UnexpectedArgument {
+ subcommand: &'static str,
+ argument: OsString,
+ },
/// Returned when a required argument is not present in the command line
#[error(
- "missing argument '{argument}'. Type '{} format --help' for more information.",
+ "missing argument '{argument}'. Type '{} {subcommand} --help' for more information.",
command_name()
)]
- MissingArgument { argument: &'static str },
+ MissingArgument {
+ subcommand: &'static str,
+ argument: &'static str,
+ },
/// Returned when a subcommand is called without any arguments
#[error("empty arguments")]

MODIFY
crates/rome_cli/src/traversal.rs
crates/rome_cli/src/traversal.rs
@@ -47,7 +47,10 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result<
}
// `--<some character>` or `-<some character>`
if without_dashes != input {
- return Err(Termination::UnexpectedArgument { argument: input });
+ return Err(Termination::UnexpectedArgument {
+ subcommand: execution.traversal_mode_subcommand(),
+ argument: input,
+ });
}
}
inputs.push(input);
@@ -55,6 +58,7 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result<
if inputs.is_empty() && execution.as_stdin_file().is_none() {
return Err(Termination::MissingArgument {
+ subcommand: execution.traversal_mode_subcommand(),
argument: "<INPUT>",
});
}

MODIFY
crates/rome_cli/tests/commands/format.rs
crates/rome_cli/tests/commands/format.rs
@@ -723,7 +723,13 @@ fn format_stdin_with_errors() {
assert!(result.is_err(), "run_cli returned {result:?}");
match result {
- Err(Termination::MissingArgument { argument }) => assert_eq!(argument, "stdin"),
+ Err(Termination::MissingArgument {
+ subcommand,
+ argument,
+ }) => {
+ assert_eq!(subcommand, "format");
+ assert_eq!(argument, "stdin")
+ }
_ => {
panic!("run_cli returned {result:?} for an unknown command help, expected an error")
}

MODIFY
crates/rome_cli/tests/main.rs
crates/rome_cli/tests/main.rs
@@ -86,7 +86,11 @@ mod main {
);
match result {
- Err(Termination::UnexpectedArgument { argument, .. }) => {
+ Err(Termination::UnexpectedArgument {
+ subcommand,
+ argument,
+ }) => {
+ assert_eq!(subcommand, "format");
assert_eq!(argument, OsString::from("--unknown"))
}
_ => panic!("run_cli returned {result:?} for an unknown argument, expected an error"),
@@ -122,7 +126,13 @@ mod main {
);
match result {
- Err(Termination::MissingArgument { argument }) => assert_eq!(argument, "<INPUT>"),
+ Err(Termination::MissingArgument {
+ subcommand,
+ argument,
+ }) => {
+ assert_eq!(subcommand, "format");
+ assert_eq!(argument, "<INPUT>")
+ }
_ => panic!("run_cli returned {result:?} for a missing argument, expected an error"),
}
}

fix(rome_cli): print the correct subcommand in error messages (#3460)