Repository navigation
expand, unexpand: read a hyphen-leading tab list as a tab list - #14291
arbelonson-source wants to merge 1 commit into
Conversation
`-1` is the obsolete spelling of `--tabs=1`, and `expand_shortcuts`
rewrote it wherever it appeared, including where it was already the
value of `-t`:
$ expand -t -1 file
error: a value is required for '--tabs <N, LIST>' but none was supplied
GNU reads it as a (bad) tab list and reports it as one:
`tab size contains invalid character(s): '-1'`. The check that produces
that message already exists here -- `expand -t abc` and `expand -t 1,-2`
match GNU today -- the value just never reached it.
Rewrite the obsolete form only where an option is expected: not as the
value of -t/--tabs, and not past a `--` terminator, which GNU treats as
a file name. `allow_hyphen_values` lets clap pass the value through.
|
GNU testsuite comparison: |
|
|
||
| /// Preprocess command line arguments and expand shortcuts. For example, "-7" is expanded to | ||
| /// "--tabs=7" and "-1,3" to "--tabs=1 --tabs=3". | ||
| /// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as |
There was a problem hiding this comment.
this landed between expand_shortcuts and its doc comment, so rustdoc now attaches that doc to takes_tabs_value. could you please move it above?
| /// Preprocess command line arguments and expand shortcuts. For example, "-7" is expanded to | ||
| /// "--tabs=7 --first-only" and "-1,3" to "--tabs=1 --tabs=3 --first-only". However, if "-a" or | ||
| /// "--all" is provided, "--first-only" is omitted. | ||
| /// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as |
There was a problem hiding this comment.
same here, the doc comment of expand_shortcuts is now glued to takes_tabs_value
| /// "--all" is provided, "--first-only" is omitted. | ||
| /// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as | ||
| /// its value, so that argument is a tab list rather than an obsolete `-N`. | ||
| fn takes_tabs_value(arg: &str) -> bool { |
There was a problem hiding this comment.
this is the same function as in expand, could it be shared, maybe in uucore?
| // `-1` is the obsolete spelling of `--tabs=1`, but only where an | ||
| // option is expected. As the value of `-t` it is a (bad) tab list that | ||
| // has to reach the tab-list check, and past `--` it is a file name. | ||
| let is_operand = end_of_options || expecting_tabs; |
There was a problem hiding this comment.
if the -t value isn't valid UTF-8, expecting_tabs never gets reset and the next -4 isn't rewritten, no?
| expecting_tabs = takes_tabs_value(text); | ||
| } | ||
| } | ||
| if !is_operand |
There was a problem hiding this comment.
we already called to_str() just above, could the two blocks be merged?
| // A tab list may start with a hyphen. It is not valid, but the | ||
| // check below reports it far better than clap does, so it has to | ||
| // reach that check rather than be taken for an option. | ||
| .allow_hyphen_values(true) |
There was a problem hiding this comment.
please make this comment shorter :)
| } | ||
|
|
||
| // The obsolete spelling still works where an option is expected. | ||
| new_ucmd!().arg("-4").pipe_in("\tx\n").succeeds(); |
There was a problem hiding this comment.
please check stdout here, and add a test for -- -1 too
| .stderr_contains(format!("tab size contains invalid character(s): '{arg}'")); | ||
| } | ||
|
|
||
| new_ucmd!().arg("-4").pipe_in(" x\n").succeeds(); |
There was a problem hiding this comment.
please check stdout here too
did you run the upstream tests/expand and tests/unexpand? bash util/build-gnu.sh && bash util/run-gnu-test.sh tests/misc/expand.pl
|
it has been conflicting for more than 7 days, sorry! please rebase it |
-1is the obsolete spelling of--tabs=1, andexpand_shortcutsrewrote it wherever it appeared — including where it was already the value of-t.expand -t -1 ftab size contains invalid character(s): '-1'error: a value is required for '--tabs <N, LIST>' but none was suppliedexpand -t -0 ftab size contains invalid character(s): '-0'unexpand -t -1 ftab size contains invalid character(s): '-1'The check that produces GNU's message is already here and already correct —
expand -t abcandexpand -t 1,-2match GNU byte for byte today. The hyphen-leading value simply never reached it: clap took-1for an option, and when allowed through,expand_shortcutsrewrote it to--tabs=1so the message named the rewritten token rather than what was typed.Found by differential testing against GNU coreutils 9.11.
Approach
The obsolete
-Nform is now rewritten only where an option is expected — not as the value of-t/--tabs, and not after a--terminator, which GNU treats as a file name.allow_hyphen_valueslets clap pass the value through to the existing check.Same shape as the
--/option-value handling in #14248 and #14255.Testing
-4and-1,3, the attached-t8,--tabs=4, clusters (-it 4,-at 4), and-- -1-t -1and-t -0plus the obsolete spelling still workingcargo test --features "expand,unexpand" --no-default-features: 93 passed, 0 failed (91 pre-existing, 2 new)cargo fmt --checkandcargo clippy -p uu_expand -p uu_unexpand --all-targets: cleanDisclosure
Prepared with AI assistance (Claude Opus 5, via Claude Code), per the AI policy in CONTRIBUTING.md. The GNU behaviour above was established by running the installed GNU binaries as a black box; I did not read GNU coreutils source. All testing was run locally.