diff --git a/src/params.rs b/src/params.rs index 90856c3f..f84533bf 100644 --- a/src/params.rs +++ b/src/params.rs @@ -90,8 +90,12 @@ impl Default for Params { } } -fn extra_operand(param: &OsStr) -> String { - format!("extra operand '{}'", param.to_string_lossy()) +fn extra_operand(executable: &OsStr, param: &OsStr) -> String { + let exe = executable.to_string_lossy(); + format!( + "{exe}: extra operand '{}'\n{exe}: Try '{exe} --help' for more information.", + param.to_string_lossy() + ) } /// Parses the command line, and also returns its arguments other than the @@ -130,7 +134,7 @@ pub fn parse_params>( } else if to.is_none() { to = Some(param); } else { - return Err(extra_operand(¶m)); + return Err(extra_operand(¶ms.executable, ¶m)); } continue; } @@ -330,7 +334,7 @@ pub fn parse_params>( } else if to.is_none() { to = Some(param); } else { - return Err(extra_operand(¶m)); + return Err(extra_operand(¶ms.executable, ¶m)); } } params.from = if let Some(from) = from { @@ -357,7 +361,7 @@ pub fn parse_params>( }; // Operands after `--` are not checked by the option loop above. if let Some((_, param)) = opts.next() { - return Err(extra_operand(¶m)); + return Err(extra_operand(¶ms.executable, ¶m)); } // diff DIRECTORY FILE => diff DIRECTORY/FILE FILE @@ -1172,7 +1176,10 @@ mod tests { ] { assert_eq!( parse_params(args.iter().map(|x| os(x)).peekable()).err(), - Some("extra operand '".to_string() + args[args.len() - 1] + "'"), + Some(format!( + "diff: extra operand '{0}'\ndiff: Try 'diff --help' for more information.", + args[args.len() - 1] + )), "{args:?}" ); } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 4bf102e9..e79dac2c 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1104,3 +1104,18 @@ fn extra_operand_after_double_dash() -> Result<(), Box> { .stderr(predicate::str::contains("extra operand 'extra'")); Ok(()) } + +#[test] +fn extra_operand_without_double_dash() -> Result<(), Box> { + let mut old = NamedTempFile::new()?; + old.write_all(b"a\nb\n")?; + let mut new = NamedTempFile::new()?; + new.write_all(b"a\nc\n")?; + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("diff").arg(old.path()).arg(new.path()).arg("extra"); + cmd.assert() + .code(predicate::eq(2)) + .stdout(predicate::str::is_empty()) + .stderr("diff: extra operand 'extra'\ndiff: Try 'diff --help' for more information.\n"); + Ok(()) +}