numfmt: report bad --round and --invalid values the way --from does - #14292
Closed
arbelonson-source wants to merge 2 commits into
Closed
numfmt: report bad --round and --invalid values the way --from does#14292arbelonson-source wants to merge 2 commits into
arbelonson-source wants to merge 2 commits into
Conversation
Both were restricted by a clap `value_parser`, so a bad value surfaced
clap's wording while `--from` and `--to`, checked in code, already used
GNU's:
$ numfmt --round=bogus 1
error: invalid value 'bogus' for '--round <METHOD>' # clap
$ numfmt --from=bogus 1
numfmt: invalid argument 'bogus' for '--from' # GNU's wording
Check them in code as well, reusing `OptionValueError` and the existing
`numfmt-error-invalid-unit-argument` message.
`ShortcutValueParser` was also accepting unambiguous abbreviations, and
GNU accepts them too (`--round=f` is from-zero), so `resolve_choice`
keeps that. An empty value matches every choice at once, which GNU
reports as `ambiguous argument` rather than `invalid argument`; that
message is added.
|
GNU testsuite comparison: |
`.args(&[&format!(...), "1"])` mixes a `&String` and a `&str` in one array literal; `args<S>` needs a single `S` for both, so this never compiled -- CI catches it, `cargo test -p uu_numfmt` alone does not, since the standalone crate's test binary isn't the one that builds this file. Two `.arg()` calls sidestep the type unification instead of fighting it.
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.
--roundand--invalidwere restricted by a clapvalue_parser, so a bad value surfaced clap's wording — while--fromand--to, which are checked in code, already used GNU's:numfmt --round=bogus 1numfmt: invalid argument 'bogus' for '--round'error: invalid value 'bogus' for '--round <METHOD>'numfmt --invalid=bogus 1numfmt: invalid argument 'bogus' for '--invalid'error: invalid value 'bogus' for '--invalid <INVALID>'numfmt --round= 1numfmt: ambiguous argument '' for '--round'error: invalid value '' for '--round <METHOD>'Found by differential testing against GNU coreutils 9.11.
Approach
Both are now checked in code, reusing the existing
OptionValueErrorandnumfmt-error-invalid-unit-argumentmessage thatparse_unitalready uses, so all four options report consistently.Two details the
value_parserwas quietly handling:ShortcutValueParseraccepted any unambiguous prefix, and so does GNU —--round=fisfrom-zero,--invalid=igisignore.resolve_choicepreserves that. The existingtest_roundcoversfrom-zerand would have caught its loss.ambiguous argument ''rather thaninvalid argument ''. A message is added for it.Scope, stated honestly
GNU also prints a second line after this one,
Valid arguments are:followed by the list. uutils' existing--from/--tomessage does not, so this brings--roundand--invalidin line with numfmt's own convention and matches GNU's first line — it is not full GNU parity for any of the four. Thehelpline each error carries (--round takes up, down, from-zero, towards-zero or nearest) serves a similar purpose in uutils' style.The same clap-vs-GNU wording gap exists in
wc --total=bogus; left out to keep this to one utility.Testing
test_invalid_round_and_invalid_arguments, verified to fail without the changecargo test --features numfmt --no-default-features: 159 passed, 0 failed (158 pre-existing, 1 new)cargo fmt --checkandcargo clippy -p uu_numfmt --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.