wc: match GNU's error for a bad --total value - #14293
Closed
arbelonson-source wants to merge 1 commit into
Closed
Conversation
`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.
|
GNU testsuite comparison: |
This was referenced Aug 31, 2026
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wc --total=bogusreports clap's wording rather than GNU's.GNU also distinguishes a value that names nothing from one that names too much —
--total=aabbreviates bothautoandalways, so it is ambiguous, not invalid, and an empty--total=abbreviates all four:And GNU checks every
--total, not only the one that wins:wc --total=bogus --total=only fis an error even though the bad value is overridden.What changed
Settings::newrather than byShortcutValueParser, so the message is ours to write. Two newWcErrorvariants carry it.TotalWhen::parsekeeps the abbreviation handlingShortcutValueParseralready gave us:--total=oisonly,--total=alisalways,--total=anames two choices and so names neither. This also removes theunreachable!("Should have been caught by clap")arm that the oldFrom<T>impl needed.ArgAction::Appendso an overridden value is still there to be checked; the last one still decides.--helpis byte-identical: the option already hadhide_possible_values(true), so dropping the value parser changes nothing visible there.Same shape as the
numfmt --roundfix in #14292.Not covered
An invalid-UTF-8 value still gets clap's
error: invalid UTF-8 was detected in one or more argumentswhere GNU sayswc: invalid argument '\377' for '--total'. That is unchanged by this PR —mainbehaves identically — and fixing it means taking the option as anOsString, which felt like a separate change.Testing
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=Ypermutations,--tot=/--to=prefixes, and combinations with-l,-cmwLand two operands.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 --checkandcargo clippy -p uu_wc --all-targets: cleanDisclosure
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.