Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,7 +134,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
} else if to.is_none() {
to = Some(param);
} else {
return Err(extra_operand(&param));
return Err(extra_operand(&params.executable, &param));
}
continue;
}
Expand Down Expand Up @@ -330,7 +334,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
} else if to.is_none() {
to = Some(param);
} else {
return Err(extra_operand(&param));
return Err(extra_operand(&params.executable, &param));
}
}
params.from = if let Some(from) = from {
Expand All @@ -357,7 +361,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
};
// Operands after `--` are not checked by the option loop above.
if let Some((_, param)) = opts.next() {
return Err(extra_operand(&param));
return Err(extra_operand(&params.executable, &param));
}

// diff DIRECTORY FILE => diff DIRECTORY/FILE FILE
Expand Down Expand Up @@ -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:?}"
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,18 @@ fn extra_operand_after_double_dash() -> Result<(), Box<dyn std::error::Error>> {
.stderr(predicate::str::contains("extra operand 'extra'"));
Ok(())
}

#[test]
fn extra_operand_without_double_dash() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
Loading