Repository navigation
xargs: do not let --eof/-e consume the next option - #993
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #993 +/- ##
==========================================
+ Coverage 93.12% 93.33% +0.21%
==========================================
Files 37 37
Lines 8175 8511 +336
Branches 406 433 +27
==========================================
+ Hits 7613 7944 +331
+ Misses 424 423 -1
- Partials 138 144 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performance1 warning
Comparing |
| @@ -1158,7 +1158,6 @@ fn do_xargs(args: &[&str]) -> Result<CommandResult, XargsError> { | |||
| .short('e') | |||
| .long(options::EOF) | |||
| .num_args(0..=1) | |||
There was a problem hiding this comment.
this only fixes values starting with -
xargs --eof cd still takes cd as the EOF string, while GNU runs cd as the command (optional argument)
and xargs_eof tests ["--eof", "cd"] the non-GNU way
should we use require_equals here instead? (fine as a follow-up)
There was a problem hiding this comment.
Thanks, you're right. xargs --eof cd now runs cd as the command. I used require_equals and rewrote -eVALUE to -e=VALUE so the attached short form (-ecd, -e-end) still works.
| // GNU: -e and --eof take an optional attached or --eof= value, and do not | ||
| // swallow a following option. `xargs --eof -l` still enables line mode. | ||
| // `-E` is different: it requires a value, so `-E -l` uses "-l" as the EOF string. | ||
| for flag in ["-e", "--eof"] { |
There was a problem hiding this comment.
please also add a test that an attached hyphen value still works, -e-end and --eof=-end
There was a problem hiding this comment.
Done, added tests for -e-end and --eof=-end.
| out.push((*arg).to_owned()); | ||
| continue; | ||
| } | ||
| if arg.starts_with("-e") |
There was a problem hiding this comment.
this also rewrites the command's own arguments
xargs echo -en now runs echo -e=n, xargs grep -efoo runs grep -e=foo
please stop at the first non-option word and add a test for it
There was a problem hiding this comment.
Right, that was wrong. In f2814cd the scan stops at -- or at the first word that is not an option, and copies the rest unchanged. So xargs echo -en and xargs grep -efoo keep their arguments. Added xargs_eof_rewrite_leaves_command_args_alone (xargs echo -ecd prints -ecd a b) plus unit cases for echo -en, -t grep -efoo and -n 1 grep -efoo.
| continue; | ||
| } | ||
| if arg.starts_with("-e") | ||
| && !arg.starts_with("-e=") |
There was a problem hiding this comment.
with GNU, -e=cd sets the EOF string to "=cd", here it becomes "cd"
why not always prepend -e=? same for -e= in xargs_empty_eof, which isn't an empty EOF string with GNU
There was a problem hiding this comment.
Agreed, it now always prepends -e=, so -e=cd sets the EOF string to =cd and -e= sets it to =, same as GNU. I dropped -e= from xargs_empty_eof since it is not an empty EOF string, and added xargs_eof_attached_value_keeps_equals for both cases.
| /// attached short form, while a separate word after `-e`/`--eof` stays the | ||
| /// command. Values of other options (for example `-E -end`) are left alone. | ||
| fn attach_eof_short_equals(args: &[&str]) -> Vec<String> { | ||
| const TAKES_SEPARATE_VALUE: &[&str] = &[ |
There was a problem hiding this comment.
this duplicates the option list from clap and will drift
could it be derived from the Command args instead?
There was a problem hiding this comment.
Yes, the list is gone. The clap command now lives in xargs_command(), gets built once, and the scan looks up each short or long option there and uses its num_args to tell flags, optional values and required values apart. -tecd is handled as a cluster too (-t -e=cd).
-e and --eof take an optional value only when it is attached (-eEND) or passed as --eof=END. A following option such as -l must still be parsed as an option, so `xargs --eof -l` enables line mode. clap was set to allow hyphen values on this optional argument, so `-l` was taken as the EOF string and line mode was ignored. -E is unchanged: it requires a value, so `-E -l` still uses "-l" as the EOF string.
GNU takes an optional attached or --eof= value only, so xargs --eof cd runs cd as the command. Use require_equals for that, rewrite -eVALUE to -e=VALUE so the attached short form still works, and test -e-end and --eof=-end.
Read which options take a value from the clap command instead of a separate list, and stop scanning at '--' or the first non-option word like GNU getopt, so the command's own -e... arguments are left alone. Always prepend -e=, so -e=cd sets the EOF string to '=cd' as in GNU.
f2814cd to
60c1912
Compare
| /// `-e`/`--eof` stays the command. Like GNU getopt for xargs, scanning stops | ||
| /// at `--` or at the first word that is not an option, so the command's own | ||
| /// arguments are left alone. Which options take a value is read from | ||
| /// `command`, which must be built. |
There was a problem hiding this comment.
6 lines of doc comment, could you please make it shorter? :)
| Some(true) => next_is_value = value.is_empty(), | ||
| Some(false) => { | ||
| if option.get_id() == options::EOF_E && !value.is_empty() { | ||
| rewritten = Some((&cluster[..i], c, value)); |
There was a problem hiding this comment.
-i also has require_equals, so -ifoo fails the same way
could this check option.is_require_equals_set() instead of the id? (fine as a follow-up)
There was a problem hiding this comment.
Agreed. Switching to is_require_equals_set() also changes -i={} to use ={} as the replace string, which is what GNU does (echo foo | xargs -i={} echo {} bar prints {} bar), but the current xargs_replace test expects the opposite. So I will do it in a follow-up PR together with that test.
|
Thanks for your PR |
Fixes #992
-e and --eof take an optional value only when it is attached (
-eEND) or passed as--eof=END. A following option such as-lmust still be parsed as an option, soxargs --eof -lenables line mode.clap was set to allow hyphen values on this optional argument, so
-lwas taken as the EOF string and line mode was ignored.-Eis unchanged: it requires a value, so-E -lstill uses "-l" as the EOF string.I found this with behave, by recording GNU findutils 4.10.0 and replaying the cases against current main.
Test (in
tests/test_xargs.rs):xargs_eof_does_not_consume_next_optioncovers-e -land--eof -l. It fails on main and passes with this change.cargo test --test test_xargs(38 passed),cargo fmt --checkandcargo clippy --all-targets --all-features -- -D warningsare clean.