shuf: report every malformed -i range the way GNU does - #14290
Open
arbelonson-source wants to merge 1 commit into
Open
shuf: report every malformed -i range the way GNU does#14290arbelonson-source wants to merge 1 commit into
arbelonson-source wants to merge 1 commit into
Conversation
The range was parsed by a clap `value_parser`, so each way of getting it
wrong surfaced clap's wording and its own reason:
$ shuf -i 5-1
error: invalid value '5-1' for '--input-range <LO-HI>': start exceeds end
GNU reports all of them identically, as `invalid input range: '5-1'`. A
range beginning with a hyphen never even reached the check, since clap
rejected `-1-5` as an unknown option first.
Take the range verbatim, allow hyphen-leading values, and check it in
`uumain` so the utility owns the message. Four tests asserted clap's
wording and now assert GNU's; each was checked against GNU 9.11 first.
|
GNU testsuite comparison: |
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.
shuf's-irange is parsed by a clapvalue_parser, so every way of getting it wrong surfaces clap's wrapper and its own internal reason, rather than the utility's message.shuf -i 5-1shuf: invalid input range: '5-1'error: invalid value '5-1' for '--input-range <LO-HI>': start exceeds endshuf -i abcshuf: invalid input range: 'abc'... : missing '-'shuf -i 1-abcshuf: invalid input range: '1-abc'... : invalid digit found in stringshuf -i 1-shuf: invalid input range: '1-'... : cannot parse integer from empty stringshuf -i -1-5shuf: invalid input range: '-1-5'error: unexpected argument '-1' foundGNU reports all of them the same way. The last row is the odd one out: a range beginning with a hyphen never reached the range check at all, because clap rejected it as an unknown option first.
Found by differential testing against GNU coreutils 9.11.
Approach
The range is now taken verbatim, with
allow_hyphen_valuesso a leading hyphen reaches us, and checked inuumainwhere the utility can emit its own message.parse_rangeno longer needs to explain why a range is bad, since GNU does not distinguish, so it returnsErr(())and the caller owns the wording.Two message keys that only existed to feed clap's wrapper (
shuf-error-start-exceeds-end,shuf-error-missing-dash) are replaced by the singleshuf-error-invalid-input-range.The existing tests
Four tests asserted clap's wording. I checked each input against GNU before changing them:
They now assert GNU's message. A fifth test covers the hyphen-leading range that previously could not reach the check.
Testing
cargo test --features shuf --no-default-features: 77 passed, 0 failed (76 pre-existing/updated, 1 new)shuf.rsand the.ftlalone fails 4 of themcargo fmt --checkandcargo clippy -p uu_shuf --all-targets: clean1-3,3-3, and the empty5-4, which succeeds silently in both)Disclosure
Prepared with AI assistance (Claude Opus 5, via Claude Code), per the AI policy in CONTRIBUTING.md. The GNU messages above were established by running the installed GNU binary as a black box; I did not read GNU coreutils source. All testing was run locally.