diff --git a/src/bin/ufat/cli.rs b/src/bin/ufat/cli.rs index 8bc08aa..def25f8 100644 --- a/src/bin/ufat/cli.rs +++ b/src/bin/ufat/cli.rs @@ -127,7 +127,9 @@ impl Args { /// and -r for `--parents`, `--long` and `--recursive` where `flags` has /// them. Short options can be bundled, as in `-lh`. `--` ends the /// options, and `-` is a positional argument. `-h` or `--help` anywhere - /// prints the usage and exits, whatever else is on the command line. + /// among the options prints the usage and exits, whatever else is on + /// the command line. After `--`, or as the value of an option, `-h` is + /// an argument like any other. fn parse(args: &[OsString], with_value: &[&str], flags: &[&str]) -> Result { use lexopt::Arg::{Long, Short, Value}; @@ -1077,7 +1079,9 @@ fn run(argv: &[OsString]) -> Result { let command = command .to_str() .ok_or_else(|| err!(errno::EINVAL, "unknown command '{}'", command.to_string_lossy()))?; - if argv.iter().skip(1).any(|a| a == "-h" || a == "--help") || matches!(command, "help" | "--help" | "-h") { + // A -h or --help after the command is left to Args::parse(), which + // knows where the options end. + if matches!(command, "help" | "--help" | "-h") { print!("{}", USAGE); return Ok(ExitCode::SUCCESS); } diff --git a/tests/cli.rs b/tests/cli.rs index f8460e1..e676500 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -68,9 +68,10 @@ fn tree(root: &Path) { fs::write(root.join("loader/entries/x.conf"), b"title x\n").unwrap(); } -/// -h and --help print the usage and exit 0 wherever they are, alone or -/// in a bundle of short options, whatever else is on the command line. -/// Every other short option in a bundle is either applied or refused. +/// -h and --help print the usage and exit 0 wherever they are among the +/// options, alone or in a bundle of short options, whatever else is on the +/// command line. Every other short option in a bundle is either applied or +/// refused. #[test] fn help_and_short_options() { let _serial = common::serial(); @@ -99,6 +100,36 @@ fn help_and_short_options() { fails(&bin, &["list", "-l=1", "x.img"], "option -l takes no value"); } +/// After `--`, and as the value of an option, -h is an argument like any +/// other. +#[test] +fn help_after_the_options_is_an_argument() { + let _serial = common::serial(); + let Some(bin) = ufat() else { + return eprintln!("skipping: no ufat binary"); + }; + let dir = Temp::dir(); + let root = dir.path(); + fs::create_dir(root.join("-h")).unwrap(); + fs::write(root.join("-h/x"), b"x").unwrap(); + let source = root.join("-h"); + let source = source.to_str().unwrap(); + let expected = ok(&bin, &["manifest", source]); + let out = Command::new(&bin) + .args(["manifest", "--", "-h"]) + .current_dir(root) + .env("SOURCE_DATE_EPOCH", "1700000000") + .output() + .expect("ufat runs"); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + assert_eq!(String::from_utf8_lossy(&out.stdout), expected); + fails( + &bin, + &["assemble", "--plan", "--size", "-h", source], + "unknown size suffix in '-h'", + ); +} + #[test] fn assemble_and_populate_take_trees_and_manifests() { let _serial = common::serial();