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
32 changes: 22 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let context = matches.get_one::<usize>("context").copied();
let group_separator = matches
.get_one::<String>("group_separator")
.map_or("--", |s| s.as_str());
.map(|s| s.as_str());
let no_group_separator = matches.get_flag("no_group_separator");
// Color output requires an explicit --color option.
let color = matches
Expand Down Expand Up @@ -427,7 +427,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
_ => true,
}
};
let group_separator = (!no_group_separator).then_some(group_separator);
let group_separator = if no_group_separator {
None
} else {
Some(group_separator.unwrap_or("--"))
};
let use_color = match color {
ColorMode::Always => true,
ColorMode::Never => false,
Expand Down Expand Up @@ -741,28 +745,32 @@ pub fn uu_app() -> Command {
.long("binary-files")
.value_name("TYPE")
.help("assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'")
.value_parser(["binary", "text", "without-match"]),
.value_parser(["binary", "text", "without-match"])
.overrides_with_all(["text", "skip_binary"]),
)
.arg(
Arg::new("text")
.short('a')
.long("text")
.help("equivalent to --binary-files=text")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.overrides_with_all(["binary_files", "skip_binary"]),
)
.arg(
Arg::new("skip_binary")
.short('I')
.help("equivalent to --binary-files=without-match")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.overrides_with_all(["binary_files", "text"]),
)
.arg(
Arg::new("directories")
.short('d')
.long("directories")
.value_name("ACTION")
.help("how to handle directories; ACTION is 'read', 'recurse', or 'skip'")
.value_parser(["read", "skip", "recurse"]),
.value_parser(["read", "skip", "recurse"])
.overrides_with_all(["recursive", "dereference_recursive"]),
)
.arg(
Arg::new("devices")
Expand All @@ -777,14 +785,16 @@ pub fn uu_app() -> Command {
.short('r')
.long("recursive")
.help("like --directories=recurse")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.overrides_with_all(["directories", "dereference_recursive"]),
)
.arg(
Arg::new("dereference_recursive")
.short('R')
.long("dereference-recursive")
.help("likewise, but follow all symlinks")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.overrides_with_all(["directories", "recursive"]),
)
.arg(
Arg::new("include")
Expand Down Expand Up @@ -886,13 +896,15 @@ pub fn uu_app() -> Command {
.value_name("SEP")
.help("print SEP on line between matches with context")
.default_value("--")
.allow_hyphen_values(true),
.allow_hyphen_values(true)
.overrides_with("no_group_separator"),
)
.arg(
Arg::new("no_group_separator")
.long("no-group-separator")
.help("do not print separator for matches with context")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.overrides_with("group_separator"),
)
.arg(
Arg::new("color")
Expand Down
32 changes: 32 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2697,3 +2697,35 @@ fn diagnostics_follow_earlier_output() {
"a.txt:foo a\ngrep: bin.dat: binary file matches\ngrep: missing.txt: No such file or directory\nb.txt:foo b\n"
);
}

#[test]
fn same_setting_options_last_wins() {
let (scene, mut c) = ucmd();
scene.fixtures.write_bytes("bin", b"hit\0x\n");
c.args(&["-a", "-I", "hit", "bin"])
.fails_with_code(1)
.no_output();

let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-I", "--binary-files=text", "hit", "bin"])
.succeeds()
.stdout_only_bytes(b"hit\0x\n");

let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["--binary-files=without-match", "-a", "hit", "bin"])
.succeeds()
.stdout_only_bytes(b"hit\0x\n");

let (scene, mut c) = ucmd();
scene.fixtures.mkdir("tree");
scene.fixtures.write("tree/f", "foo\n");
c.args(&["-r", "-d", "skip", "foo", "tree"])
.fails_with_code(1)
.no_output();

let (_s, mut c) = ucmd();
c.args(&["-A0", "--no-group-separator", "--group-separator=::", "x"])
.pipe_in("x\ny\nx\n")
.succeeds()
.stdout_only("x\n::\nx\n");
}
Loading