From 4d95809e888fab622f3a27804718842c65bbd5ab Mon Sep 17 00:00:00 2001 From: Jake Wallin Date: Fri, 16 Oct 2026 19:00:57 +0000 Subject: [PATCH] coreutils: recognize abbreviated help options --- src/bin/coreutils.rs | 2 +- tests/test_util_name.rs | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 46418112286..46fcbc50ad8 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -123,7 +123,7 @@ fn main() { exit(uumain(vec![util_os].into_iter().chain(args))); } // GNU coreutils --help string shows help for coreutils - if util == "--help" || util == "-h" { + if util == "-h" || (util.starts_with("--h") && "--help".starts_with(util)) { usage(&utils, binary_as_util); exit(0); } else if util.starts_with('-') { diff --git a/tests/test_util_name.rs b/tests/test_util_name.rs index 6a0f50ebe57..a7c904d0537 100644 --- a/tests/test_util_name.rs +++ b/tests/test_util_name.rs @@ -73,16 +73,49 @@ fn binary_name_execfn_fallback() { assert!(res1 && res2); } -#[test] -fn test_coreutils_help_ignore_args() { +#[rstest::rstest] +fn test_coreutils_help_ignore_args(#[values("--h", "--he", "--hel", "--help", "-h")] flag: &str) { let scenario = TestScenario::new("help_ignoring_args"); let output = std::process::Command::new(&scenario.bin_path) - .arg("--help") + .arg(flag) .arg("---") .output() .unwrap(); assert_eq!(output.status.code(), Some(0)); + assert!(output.stderr.is_empty()); + assert!( + String::from_utf8(output.stdout) + .unwrap() + .contains("(multi-call binary)") + ); +} + +#[rstest::rstest] +fn test_coreutils_help_near_misses(#[values("--", "--hell", "--helps", "--helpless")] flag: &str) { + let scenario = TestScenario::new("invalid_help"); + let output = std::process::Command::new(&scenario.bin_path) + .arg(flag) + .output() + .unwrap(); + + assert_eq!(output.status.code(), Some(1)); + assert!(output.stdout.is_empty()); + assert!( + String::from_utf8(output.stderr) + .unwrap() + .contains(&format!("unrecognized option '{flag}'")) + ); +} + +#[test] +#[cfg(feature = "echo")] +fn test_coreutils_help_argument_is_forwarded() { + TestScenario::new("echo") + .ucmd() + .arg("--hel") + .succeeds() + .stdout_only("--hel\n"); } #[test]