Skip to content

diff - -W N and --width N are rejected, only --width=N works #396

Description

@sylvestre

GNU diff accepts the side-by-side width as -W N, -WN, --width=N and --width N.
uutils only matches the single word --width=N, so -W 44 fails with
unrecognized option '-W' and --width 44 with unrecognized option '--width', both
exit 2. --tabsize N (separate argument) is rejected the same way. The GNU tests
side-by-side (-y -W 44) and side-by-side-seq (-yW 11, which also needs clustered
short options, #298) expose it.

Reproduction

printf 'x\n' > w1; printf 'y\n' > w2

Rust (incorrect)

$ ./target/release/diffutils diff -y -W 20 w1 w2
unrecognized option '-W'
# Exit code: 2
$ ./target/release/diffutils diff -y --width 20 w1 w2
unrecognized option '--width'
# Exit code: 2
$ ./target/release/diffutils diff --tabsize 4 w1 w2
unrecognized option '--tabsize'
# Exit code: 2

GNU (correct)

$ LC_ALL=C diff -y -W 20 w1 w2
x     |	y
# Exit code: 1
$ LC_ALL=C diff -y --width 20 w1 w2
x     |	y
# Exit code: 1
$ LC_ALL=C diff --tabsize 4 w1 w2
1c1
< x
---
> y
# Exit code: 1

Root cause

Both options are recognized only through whole-word regexes in src/params.rs:119-120:

let tabsize_re = Regex::new(r"^--tabsize=(?<num>\d+)$").unwrap();
let width_re = Regex::new(r"^--width=(?P<long>\d+)$").unwrap();

checked at src/params.rs:242 and src/params.rs:263. Anything else falls through to
unrecognized option at src/params.rs:324-326. option_argument/argument_value,
already used for -F and -S, handle all four spellings.

Test

Add to tests/by-util/test_diff.rs:

#[test]
fn width_option_forms() -> Result<(), Box<dyn std::error::Error>> {
    let mut a = NamedTempFile::new()?;
    a.write_all(b"x\n")?;
    let mut b = NamedTempFile::new()?;
    b.write_all(b"y\n")?;
    for args in [
        vec!["-W", "20"],
        vec!["-W20"],
        vec!["--width", "20"],
        vec!["--width=20"],
    ] {
        let mut cmd = cargo_bin_cmd!("diffutils");
        cmd.arg("diff").arg("-y").args(&args).arg(a.path()).arg(b.path());
        cmd.assert()
            .stderr(predicate::str::is_empty())
            .stdout(predicate::str::starts_with("x "));
    }
    let mut cmd = cargo_bin_cmd!("diffutils");
    cmd.arg("diff").args(["--tabsize", "4"]).arg(a.path()).arg(b.path());
    cmd.assert().code(predicate::eq(1));
    Ok(())
}

Exposed by the GNU diffutils test suite: side-by-side, side-by-side-seq.

Activity

  1. shadowmkj commented on Nov 15, 2026

    @shadowmkj
    Contributor

    Why are we not using clap for command line parsing?
    GNU diff also supports -W10 type of flags.

    I saw that uutils/coreutils is using clap with internal localization to parse the args seemlessly. Wouldn't it be better to implement a similar parser in this library?
    This would resolve a lot of the current open issues that target argument parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions