Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cut: fix argument parsing for the delimiter #3607

Merged
merged 1 commit into from
Jun 11, 2022
Merged
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
9 changes: 5 additions & 4 deletions src/uu/cut/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.collect_str(InvalidEncodingHandling::Ignore)
.accept_any();

let delimiter_is_equal = args.contains(&"-d=".to_string()); // special case
let matches = uu_app().get_matches_from(args);

let complement = matches.is_present(options::COMPLEMENT);
Expand Down Expand Up @@ -460,11 +461,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// GNU's `cut` supports `-d=` to set the delimiter to `=`.
// Clap parsing is limited in this situation, see:
// https://github.com/uutils/coreutils/issues/2424#issuecomment-863825242
// Since clap parsing handles `-d=` as delimiter explicitly set to "" and
// an empty delimiter is not accepted by GNU's `cut` (and makes no sense),
// we can use this as basis for a simple workaround:
if delim.is_empty() {
if delimiter_is_equal {
delim = "=";
} else if delim == "''" {
// treat `''` as empty delimiter
delim = "";
}
if delim.chars().count() > 1 {
Err("invalid input: The '--delimiter' ('-d') option expects empty or 1 character long, but was provided a value 2 characters or longer".into())
Expand Down
20 changes: 19 additions & 1 deletion tests/by-util/test_cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,28 @@ fn test_directory_and_no_such_file() {
}

#[test]
fn test_equal_as_delimiter() {
fn test_equal_as_delimiter1() {
new_ucmd!()
.args(&["-f", "2", "-d="])
.pipe_in("--dir=./out/lib")
.succeeds()
.stdout_only("./out/lib\n");
}

#[test]
fn test_equal_as_delimiter2() {
new_ucmd!()
.args(&["-f2", "--delimiter="])
.pipe_in("a=b\n")
.succeeds()
.stdout_only("a=b\n");
}

#[test]
fn test_equal_as_delimiter3() {
new_ucmd!()
.args(&["-f", "1,2", "-d", "''", "--output-delimiter=Z"])
.pipe_in("ab\0cd\n")
.succeeds()
.stdout_only_bytes("abZcd\n");
}