Repository navigation
pr: report invalid numeric options and page ranges as GNU does - #15176
Conversation
|
Is there an existing issue describing the problem being addressed? |
|
GNU testsuite comparison: |
| /// Like [`parse_number`], for an option that wants a number from 1 up: | ||
| /// zero and negative numbers are numbers out of range. | ||
| fn parse_positive(raw: &str, option_message: &str) -> Result<usize, PrError> { | ||
| let negative = raw |
There was a problem hiding this comment.
the same "is a negative number" check is in the -o parsing too, could be a small helper, no?
There was a problem hiding this comment.
Done @sylvestre , is_negative_number() for both.
|
@sylvestre the xstrtol failure came from util/build-gnu.sh rewriting xstrtol.pl to the old wording; those three seds are gone since pr prints what the test expects now. The Windows test no longer depends on the OS error text. |
efbfa5d to
32ba51e
Compare
The value of -l, -w, -W, -N and -o was dropped by the legacy operand scan when it looked like one: `pr -l -1 f` read `-1` as a column count and `f` as the page length, and `pr -l +5 f` took `+5` for a page range. A page range that was not one (`--pages=0`, `2:1`) was reported as a bad argument, a `+` operand that was not a range was taken for a file, and a zero or negative length, width or column count got a message of its own. Keep the value of an option out of the operand scan, and let clap take a value that starts with `-`. Report the ranges and numbers the way GNU 9.12 does: `invalid page range '0'`, `--pages argument '...' too large`, `invalid + argument 'x'`, `invalid suffix in + argument '1:3x'`, and for the numbers `'-l PAGE_LENGTH' invalid number of lines: '0': Numerical result out of range`. A `+` operand that GNU takes for a file name, digits followed by other characters or a range that is not one, stays a file name.
The list of options whose value is the next argument duplicated what clap knows and missed a cluster (`-tl -1`) and an abbreviation (`--len -1`). Ask the built Command instead: a long option, exact or the one option a prefix fits, or the last short option of a cluster, that takes at least one value and does not require `=`. The scan runs once, on the arguments after `-e` got its attached value, so clap and the operands see the same thing. `-h` and `-D` take a value that starts with `-`, as GNU does. One parse of `FIRST[:LAST]` serves both the `+` operand check and the messages. A number is the digits up to whatever follows them, so `--pages=1x` is an invalid page range and `+1x:3` a file name, and a number too large with characters after it is an invalid suffix, as GNU reports them. The numeric diagnostics go through translate!(). Drop the xstrtol.pl wording patch from build-gnu.sh, since pr now prints what the test expects, and make the file name test independent of the OS error text.
32ba51e to
16e7ca0
Compare
| if arg == "--" { | ||
| clap_args.push(arg); | ||
| break; | ||
| } |
There was a problem hiding this comment.
when arg is itself a value this still checks it as an option, so pr -h -l +2 f gives +2 to -l instead of the page operand
!is_value && takes_next_argument(...)? please add a test
doing it in a new PR is fine
| }) | ||
| matches | ||
| .get_one::<String>(opt) | ||
| .map(|raw| parse_number(raw, option_message)) |
There was a problem hiding this comment.
almost the same as parse_usize, do we need both wrappers?
doing it in a new PR is fine
| let option_message = translate!("pr-error-invalid-line-offset"); | ||
| match parse_number(raw, &option_message) { | ||
| // A negative margin is reported like one that does not fit; | ||
| // a negative zero is zero. |
There was a problem hiding this comment.
parsing as i64 only to detect -0 is a bit odd, checking that all the digits are 0 would be simpler, no?
The value of
-l,-w,-W,-Nand-owas dropped by the legacy operand scan when it looked like one:pr -l -1 fread-1as a column count andfas the page length, andpr -l +5 ftook+5for a page range. A page range that is not one (--pages=0,--pages=2:1) was reported as a bad argument, a+operand that is not a range was taken for a file, and a zero or negative length, width or column count had a wording of its own.The value of an option now stays out of the operand scan, and clap takes a value that starts with
-. The messages follow GNU 9.12:invalid page range '0',--pages argument '…' too large,invalid + argument 'x',invalid suffix in + argument '1:3x', and for the numbers'-l PAGE_LENGTH' invalid number of lines: '0': Numerical result out of range(the errno text, asValue too large for defined data typealready is). A+operand that GNU takes for a file name, digits followed by other characters or numbers that make no page range, stays a file name.Every case in the tests was run against GNU 9.12; the pr suite passes. This covers
pr/options.shfrom #14705.Closes #15179.