Skip to content

wc: match GNU's error for a bad --total value - #14293

Closed
arbelonson-source wants to merge 1 commit into
uutils:mainfrom
arbelonson-source:fix/wc-total-invalid-argument-message
Closed

wc: match GNU's error for a bad --total value#14293
arbelonson-source wants to merge 1 commit into
uutils:mainfrom
arbelonson-source:fix/wc-total-invalid-argument-message

Conversation

@arbelonson-source

Copy link
Copy Markdown
Contributor

wc --total=bogus reports clap's wording rather than GNU's.

$ wc --total=bogus f            # ours, before
error: invalid value 'bogus' for '--total <WHEN>'
For more information, try '--help'.

$ wc --total=bogus f            # GNU 9.11
wc: invalid argument 'bogus' for '--total'
Valid arguments are:
  - 'auto'
  - 'always'
  - 'only'
  - 'never'
Try 'wc --help' for more information.

GNU also distinguishes a value that names nothing from one that names too much — --total=a abbreviates both auto and always, so it is ambiguous, not invalid, and an empty --total= abbreviates all four:

$ wc --total=a f
wc: ambiguous argument 'a' for '--total'
Valid arguments are:
...

And GNU checks every --total, not only the one that wins: wc --total=bogus --total=only f is an error even though the bad value is overridden.

What changed

  • The value is validated in Settings::new rather than by ShortcutValueParser, so the message is ours to write. Two new WcError variants carry it.
  • TotalWhen::parse keeps the abbreviation handling ShortcutValueParser already gave us: --total=o is only, --total=al is always, --total=a names two choices and so names neither. This also removes the unreachable!("Should have been caught by clap") arm that the old From<T> impl needed.
  • The option uses ArgAction::Append so an overridden value is still there to be checked; the last one still decides.
  • --help is byte-identical: the option already had hide_possible_values(true), so dropping the value parser changes nothing visible there.

Same shape as the numfmt --round fix in #14292.

Not covered

An invalid-UTF-8 value still gets clap's error: invalid UTF-8 was detected in one or more arguments where GNU says wc: invalid argument '\377' for '--total'. That is unchanged by this PR — main behaves identically — and fixing it means taking the option as an OsString, which felt like a separate change.

Testing

  • 75-case differential sweep against GNU 9.11 under LC_ALL=C — stdout, stderr and exit code all match: every valid value, the abbreviations (au, al, n, ne, nev, o, on, onl), near-misses (all, alw, onlyx), wrong case (ALWAYS, Only), values with stray whitespace, the empty value, all 25 --total=X --total=Y permutations, --tot=/--to= prefixes, and combinations with -l, -cmwL and two operands.
  • 4 new tests in test_wc.rs, each verified to fail without the change.
  • cargo test --features wc --test tests -- test_wc: 63 passed, 0 failed (59 pre-existing, 4 new)
  • cargo fmt --check and cargo clippy -p uu_wc --all-targets: clean

Disclosure

Prepared with AI assistance (Claude Opus 5, via Claude Code), per the AI policy in CONTRIBUTING.md. The GNU messages were established by running the installed GNU binary as a black box; I did not read GNU coreutils source. All testing was run locally.

`wc --total=bogus` reported clap's wording rather than GNU's:

    $ wc --total=bogus f
    error: invalid value 'bogus' for '--total <WHEN>'
    For more information, try '--help'.

GNU names the option, lists what it takes, and distinguishes a value
that names nothing from one that names too much:

    $ wc --total=bogus f
    wc: invalid argument 'bogus' for '--total'
    Valid arguments are:
      - 'auto'
      - 'always'
      - 'only'
      - 'never'
    Try 'wc --help' for more information.

    $ wc --total=a f
    wc: ambiguous argument 'a' for '--total'
    ...

Validate the value in `Settings::new` instead of in clap, so the message
is ours to write. `ShortcutValueParser` already accepted unambiguous
abbreviations, and `TotalWhen::parse` keeps that: `--total=o` is `only`,
`--total=a` names both `auto` and `always` and so names neither, and an
empty value abbreviates all four -- ambiguous rather than invalid, which
is how GNU reports it.

GNU also checks every `--total`, not just the one that wins, so
`wc --total=bogus --total=only f` is an error. `ArgAction::Set` keeps
only the last value, so the option now appends and every occurrence is
checked while the last still decides.

`--help` is unchanged: the option already hid its possible values.
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/tail-n0f is now passing!

@arbelonson-source

Copy link
Copy Markdown
Contributor Author

Closing proactively, same reason as #14292 — pure wording alignment over clap's own already-correct validation for --total, no functional change. See the discussion on #14308.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant