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
8 changes: 6 additions & 2 deletions src/bin/ufat/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Args> {
use lexopt::Arg::{Long, Short, Value};

Expand Down Expand Up @@ -1077,7 +1079,9 @@ fn run(argv: &[OsString]) -> Result<ExitCode> {
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);
}
Expand Down
37 changes: 34 additions & 3 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading