Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/xargs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,8 @@ fn value_required(arg: &Arg) -> Option<bool> {
(range.max_values() > 0).then(|| range.min_values() > 0)
}

/// Rewrite `-eVALUE` to `-e=VALUE` for clap's `require_equals`.
/// Stops at the command, like GNU getopt.
/// Rewrite `-eVALUE` to `-e=VALUE`, and `-lVALUE` to `-l=VALUE`, for clap's
/// `require_equals`. Stops at the command, like GNU getopt.
fn attach_eof_short_equals(command: &clap::Command, args: &[&str]) -> Vec<String> {
let mut out = Vec::with_capacity(args.len());
let mut rest = args.iter();
Expand Down Expand Up @@ -1230,7 +1230,9 @@ fn attach_eof_short_equals(command: &clap::Command, args: &[&str]) -> Vec<String
None => continue,
Some(true) => next_is_value = value.is_empty(),
Some(false) => {
if option.get_id() == options::EOF_E && !value.is_empty() {
let attached_only = option.get_id() == options::EOF_E
|| option.get_id() == options::MAX_LINES_L;
if attached_only && !value.is_empty() {
rewritten = Some((&cluster[..i], c, value));
}
}
Expand Down Expand Up @@ -1320,6 +1322,9 @@ fn xargs_command() -> clap::Command {
Arg::new(options::MAX_LINES_L)
.short('l')
.num_args(0..=1)
// Like GNU getopt, only an attached value (`-l2`) is the
// option's: in `xargs -l echo`, echo is the command.
.require_equals(true)
// Without a value the option still needs an index to be ordered
// against -L, -n and -I.
.default_missing_value("1")
Expand Down Expand Up @@ -1575,6 +1580,20 @@ mod tests {
assert_eq!(rewrite(&["xargs", "--", "-ecd"]), ["xargs", "--", "-ecd"]);
}

#[test]
fn attach_eof_short_equals_rewrites_attached_max_lines() {
assert_eq!(
rewrite(&["xargs", "-l2", "echo"]),
["xargs", "-l=2", "echo"]
);
assert_eq!(rewrite(&["xargs", "-tl2"]), ["xargs", "-t", "-l=2"]);
assert_eq!(rewrite(&["xargs", "-0l2"]), ["xargs", "-0", "-l=2"]);
assert_eq!(rewrite(&["xargs", "-l", "echo"]), ["xargs", "-l", "echo"]);
assert_eq!(rewrite(&["xargs", "-lt"]), ["xargs", "-l=t"]);
assert_eq!(rewrite(&["xargs", "-n", "-l2"]), ["xargs", "-n", "-l2"]);
assert_eq!(rewrite(&["xargs", "echo", "-l2"]), ["xargs", "echo", "-l2"]);
}

#[test]
fn attach_eof_short_equals_stops_at_the_command() {
assert_eq!(rewrite(&["xargs", "echo", "-en"]), ["xargs", "echo", "-en"]);
Expand Down
18 changes: 18 additions & 0 deletions tests/test_xargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,3 +1139,21 @@ fn xargs_replace_unterminated_quote() {
.stderr_contains("unmatched single quote")
.no_stdout();
}

#[test]
fn xargs_max_lines_l_takes_only_an_attached_value() {
// Like GNU xargs, `-l` only takes an attached value, so the next argument
// is the command, and it works inside a cluster of short options.
for (args, stdout) in [
(&["-l", "echo"][..], "a b\nc\nd\n"),
(&["-l2", "echo"][..], "a b c\nd\n"),
(&["-l", "-r", "echo"][..], "a b\nc\nd\n"),
(&["-rl2", "echo"][..], "a b c\nd\n"),
] {
ucmd()
.args(args)
.pipe_in("a b\nc\nd\n")
.succeeds()
.stdout_only(stdout);
}
}
Loading