Skip to content

Commit

Permalink
shuf: treat -e as a flag, not as a multi-value arg
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake committed Feb 21, 2024
1 parent 4dba45d commit 69a48c8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/uu/shuf/src/shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rand::RngCore;
use std::fs::File;
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::{format_usage, help_about, help_usage};

mod rand_read_adapter;
Expand Down Expand Up @@ -42,15 +42,21 @@ mod options {
pub static RANDOM_SOURCE: &str = "random-source";
pub static REPEAT: &str = "repeat";
pub static ZERO_TERMINATED: &str = "zero-terminated";
pub static FILE: &str = "file";
pub static FILE_OR_ARGS: &str = "file-or-args";
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;

let mode = if let Some(args) = matches.get_many::<String>(options::ECHO) {
Mode::Echo(args.map(String::from).collect())
let mode = if matches.get_flag(options::ECHO) {
Mode::Echo(
matches
.get_many::<String>(options::FILE_OR_ARGS)
.unwrap_or_default()
.map(String::from)
.collect(),
)
} else if let Some(range) = matches.get_one::<String>(options::INPUT_RANGE) {
match parse_range(range) {
Ok(m) => Mode::InputRange(m),
Expand All @@ -59,13 +65,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
}
} else {
Mode::Default(
matches
.get_one::<String>(options::FILE)
.map(|s| s.as_str())
.unwrap_or("-")
.to_string(),
)
let mut operands = matches
.get_many::<String>(options::FILE_OR_ARGS)
.unwrap_or_default();
let file = operands.next().cloned().unwrap_or("-".into());
if let Some(second_file) = operands.next() {
return Err(UUsageError::new(

Check warning on line 73 in src/uu/shuf/src/shuf.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/shuf/src/shuf.rs#L73

Added line #L73 was not covered by tests
1,
format!("unexpected argument '{second_file}' found"),

Check warning on line 75 in src/uu/shuf/src/shuf.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/shuf/src/shuf.rs#L75

Added line #L75 was not covered by tests
));
};
Mode::Default(file)
};

let options = Options {
Expand Down Expand Up @@ -124,11 +134,9 @@ pub fn uu_app() -> Command {
Arg::new(options::ECHO)
.short('e')
.long(options::ECHO)
.value_name("ARG")
.help("treat each ARG as an input line")
.use_value_delimiter(false)
.num_args(0..)
.action(clap::ArgAction::Append)
.action(clap::ArgAction::SetTrue)
.overrides_with(options::ECHO)
.conflicts_with(options::INPUT_RANGE),
)
.arg(
Expand All @@ -137,7 +145,7 @@ pub fn uu_app() -> Command {
.long(options::INPUT_RANGE)
.value_name("LO-HI")
.help("treat each number LO through HI as an input line")
.conflicts_with(options::FILE),
.conflicts_with(options::FILE_OR_ARGS),
)
.arg(
Arg::new(options::HEAD_COUNT)
Expand Down Expand Up @@ -178,7 +186,11 @@ pub fn uu_app() -> Command {
.action(ArgAction::SetTrue)
.overrides_with(options::ZERO_TERMINATED),
)
.arg(Arg::new(options::FILE).value_hint(clap::ValueHint::FilePath))
.arg(
Arg::new(options::FILE_OR_ARGS)
.action(clap::ArgAction::Append)
.value_hint(clap::ValueHint::FilePath),
)
}

fn read_input_file(filename: &str) -> UResult<Vec<u8>> {
Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ fn test_echo_multi() {
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}

#[test]
fn test_echo_postfix() {
let result = new_ucmd!().arg("a").arg("b").arg("c").arg("-e").succeeds();
result.no_stderr();

let mut result_seq: Vec<String> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.into())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}

#[test]
fn test_echo_short_collapsed_zero() {
let result = new_ucmd!().arg("-ez").arg("a").arg("b").arg("c").succeeds();
result.no_stderr();

let mut result_seq: Vec<String> = result
.stdout_str()
.split('\0')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}

#[test]
fn test_head_count() {
let repeat_limit = 5;
Expand Down

0 comments on commit 69a48c8

Please sign in to comment.