diff --git a/fuzz/fuzz_targets/fuzz_cmp_args.rs b/fuzz/fuzz_targets/fuzz_cmp_args.rs index 667319f5..579cf34c 100644 --- a/fuzz/fuzz_targets/fuzz_cmp_args.rs +++ b/fuzz/fuzz_targets/fuzz_cmp_args.rs @@ -11,9 +11,6 @@ fn os(s: &str) -> OsString { } fuzz_target!(|x: Vec| -> 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. diff --git a/src/cmp.rs b/src/cmp.rs index 7dffa859..ed677e8d 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -36,6 +36,7 @@ pub struct Params { max_bytes: Option, verbose: bool, quiet: bool, + help: bool, } #[inline] @@ -204,8 +205,8 @@ pub fn parse_params>(mut opts: Peekable) -> 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())); @@ -463,6 +464,11 @@ pub fn main(opts: Peekable) -> ExitCode { } }; + if params.help { + println!("{}", usage_string(¶ms.executable.to_string_lossy())); + return ExitCode::SUCCESS; + } + if params.from == "-" && params.to == "-" || same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false) { @@ -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); diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index cdd5bc91..032f8043 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -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 \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.