Skip to content

Commit 67cd89b

Browse files
authored
Merge pull request #601 from brian-pane/xargs
xargs: add support for -l option
2 parents 84a0473 + dfc460a commit 67cd89b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/xargs/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod options {
2727
pub const MAX_ARGS: &str = "max-args";
2828
pub const MAX_CHARS: &str = "max-chars";
2929
pub const MAX_LINES: &str = "max-lines";
30+
pub const MAX_LINES_L: &str = "max-lines-l";
3031
pub const MAX_PROCS: &str = "max-procs";
3132
pub const NO_RUN_IF_EMPTY: &str = "no-run-if-empty";
3233
pub const NULL: &str = "null";
@@ -927,6 +928,17 @@ fn do_xargs(args: &[&str]) -> Result<CommandResult, XargsError> {
927928
)
928929
.value_parser(validate_positive_usize),
929930
)
931+
.arg(
932+
Arg::new(options::MAX_LINES_L)
933+
.short('l')
934+
.num_args(0..=1)
935+
.help(
936+
"Equivalent to -L, but with a default value of 1 if max-lines \
937+
is unspecified",
938+
)
939+
.value_name("max-lines")
940+
.value_parser(validate_positive_usize),
941+
)
930942
.arg(
931943
Arg::new(options::MAX_PROCS)
932944
.short('P')
@@ -1032,7 +1044,15 @@ fn do_xargs(args: &[&str]) -> Result<CommandResult, XargsError> {
10321044
exit_if_pass_char_limit: matches.get_flag(options::EXIT),
10331045
max_args: matches.get_one::<usize>(options::MAX_ARGS).copied(),
10341046
max_chars: matches.get_one::<usize>(options::MAX_CHARS).copied(),
1035-
max_lines: matches.get_one::<usize>(options::MAX_LINES).copied(),
1047+
max_lines: [options::MAX_LINES, options::MAX_LINES_L]
1048+
.iter()
1049+
.find_map(|&option| {
1050+
matches.contains_id(option).then(|| {
1051+
matches
1052+
.get_one::<usize>(option)
1053+
.map_or_else(|| 1, std::borrow::ToOwned::to_owned)
1054+
})
1055+
}),
10361056
no_run_if_empty: matches.get_flag(options::NO_RUN_IF_EMPTY),
10371057
null: matches.get_flag(options::NULL),
10381058
replace: [options::REPLACE_I, options::REPLACE]

tests/xargs_tests.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn xargs_max_args() {
106106

107107
#[test]
108108
fn xargs_max_lines() {
109-
for arg in ["-L2", "--max-lines=2"] {
109+
for arg in ["-L2", "--max-lines=2", "-l2"] {
110110
cargo_bin_cmd!("xargs")
111111
.arg(arg)
112112
.write_stdin("ab cd\nef\ngh i\n\njkl\n")
@@ -115,6 +115,13 @@ fn xargs_max_lines() {
115115
.stderr(predicate::str::is_empty())
116116
.stdout(predicate::str::diff("ab cd ef\ngh i jkl\n"));
117117
}
118+
cargo_bin_cmd!("xargs")
119+
.arg("-l")
120+
.write_stdin("ab cd\nef\ngh i\n\njkl\n")
121+
.assert()
122+
.success()
123+
.stderr(predicate::str::is_empty())
124+
.stdout(predicate::str::diff("ab cd\nef\ngh i\njkl\n"));
118125
}
119126

120127
#[test]

0 commit comments

Comments
 (0)