Skip to content

xargs: do not let --eof/-e consume the next option - #993

Merged
sylvestre merged 4 commits into
uutils:mainfrom
emirmrkaljevic:xargs-eof-next-option
Nov 24, 2026
Merged

sylvestre merged 4 commits into
uutils:mainfrom
emirmrkaljevic:xargs-eof-next-option

Conversation

@emirmrkaljevic

Copy link
Copy Markdown
Contributor

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 -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.

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_option covers -e -l and --eof -l. It fails on main and passes with this change. cargo test --test test_xargs (38 passed), cargo fmt --check and cargo clippy --all-targets --all-features -- -D warnings are clean.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.59036% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.33%. Comparing base (83e46e7) to head (52eeb2c).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/xargs/mod.rs 97.59% 1 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 15 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 20 untouched benchmarks


Comparing emirmrkaljevic:xargs-eof-next-option (52eeb2c) with main (8d4d7b7)

Open in CodSpeed

Comment thread src/xargs/mod.rs
@@ -1158,7 +1158,6 @@ fn do_xargs(args: &[&str]) -> Result<CommandResult, XargsError> {
.short('e')
.long(options::EOF)
.num_args(0..=1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/test_xargs.rs
// 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"] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add a test that an attached hyphen value still works, -e-end and --eof=-end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added tests for -e-end and --eof=-end.

Comment thread src/xargs/mod.rs Outdated
out.push((*arg).to_owned());
continue;
}
if arg.starts_with("-e")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@emirmrkaljevic emirmrkaljevic Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/xargs/mod.rs Outdated
continue;
}
if arg.starts_with("-e")
&& !arg.starts_with("-e=")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/xargs/mod.rs Outdated
/// 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] = &[

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicates the option list from clap and will drift
could it be derived from the Command args instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@sylvestre
sylvestre force-pushed the xargs-eof-next-option branch from f2814cd to 60c1912 Compare November 16, 2026 01:27
Comment thread src/xargs/mod.rs Outdated
/// `-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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 lines of doc comment, could you please make it shorter? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shortened to two lines in 52eeb2c.

Comment thread src/xargs/mod.rs
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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sylvestre
sylvestre merged commit b4e34ec into uutils:main Nov 24, 2026
35 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xargs: --eof/-e consumes the next option as the EOF string

2 participants