Skip to content
Open
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
3 changes: 0 additions & 3 deletions fuzz/fuzz_targets/fuzz_cmp_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ fn os(s: &str) -> OsString {
}

fuzz_target!(|x: Vec<OsString>| -> Corpus {
if x.iter().any(|a| a == "--help") {
return Corpus::Reject;
}
if x.len() > 6 {
// Make sure we try to parse an option when we get longer args. x[0] will be
// the executable name.
Expand Down
24 changes: 22 additions & 2 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Params {
max_bytes: Option<BytesLimitU64>,
verbose: bool,
quiet: bool,
help: bool,
}

#[inline]
Expand Down Expand Up @@ -204,8 +205,8 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
continue;
}
if param == "--help" {
println!("{}", usage_string(&executable_str));
std::process::exit(0);
params.help = true;
return Ok(params);
}
if param_str.starts_with('-') {
return Err(format!("unrecognized option '{}'", param.to_string_lossy()));
Expand Down Expand Up @@ -463,6 +464,11 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
}
};

if params.help {
println!("{}", usage_string(&params.executable.to_string_lossy()));
return ExitCode::SUCCESS;
}

if params.from == "-" && params.to == "-"
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
{
Expand Down Expand Up @@ -722,6 +728,20 @@ mod tests {
OsString::from(s)
}

#[test]
fn help() {
for args in [
vec!["cmp", "--help"],
vec!["cmp", "foo", "--help"],
vec!["cmp", "--help", "--invalid"],
vec!["cmp", "-l", "-s", "--help"],
] {
let params = parse_params(args.into_iter().map(os).peekable()).unwrap();
assert!(params.help);
assert_eq!(params.executable, os("cmp"));
}
}

#[test]
fn count_newlines_across_chunks() {
assert_eq!(count_newlines(b""), 0);
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ use rstest::rstest;
use std::fs::OpenOptions;
use uutests::{at_and_ts, new_ucmd};

#[rstest]
#[case::alone(&["--help"])]
#[case::missing_file(&["foo", "--help"])]
#[case::stop_parsing(&["--help", "--invalid"])]
#[case::incompatible_options(&["-l", "-s", "--help"])]
fn cmp_help(#[case] args: &[&str]) {
new_ucmd!()
.args(args)
.succeeds()
.stdout_only("Usage: cmp <from> <to>\n");
}

// A file whose metadata length is 0 but which still yields bytes (/dev/zero)
// collapses the offset column to two characters, so the padding subtraction
// underflowed once the running offset reached three digits.
Expand Down
Loading