Skip to content

Commit

Permalink
Merge pull request #3607 from jhscheer/cut_fix_delimiter
Browse files Browse the repository at this point in the history
cut: fix argument parsing for the delimiter
  • Loading branch information
sylvestre authored Jun 11, 2022
2 parents f4dd9d6 + fa64407 commit 3461b6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
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");
}

0 comments on commit 3461b6c

Please sign in to comment.