Skip to content
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
15 changes: 8 additions & 7 deletions src/uu/pr/src/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@
fn recreate_arguments(args: &[String]) -> Vec<String> {
let num_regex = Regex::new(r"^[^-]\d*$").unwrap();
let n_regex = Regex::new(r"^-n\s*$").unwrap();
let e_regex = Regex::new(r"^-e").unwrap();
// `-e` ends a cluster of short flags that take no value of their own, as in `-tre`.
// Options that do take a value are excluded so that `-se` keeps meaning `-s e`.
let e_regex = Regex::new(r"^-[dtTrFfabmJ]*e$").unwrap();

Check warning on line 432 in src/uu/pr/src/pr.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'Ffabm' (file:'src/uu/pr/src/pr.rs', line:432)
let mut arguments = args.to_owned();
let num_option = args
.iter()
Expand All @@ -442,16 +444,15 @@
arguments.insert(pos + 2, could_be_file);
}

// To ensure not to accidentally delete the next argument after a short flag for -e we insert
// the default values for the -e flag is '-e' is present without direct arguments.
// `-e` takes an optional attached argument, which clap cannot express, so it is filled in
// here. Without it clap would report a missing value for `-tre`, or swallow the following
// argument, usually a file name, for a bare `-e`.
let expand_tabs_option = arguments
.iter()
.take_while(|arg| arg.as_str() != "--")
.find_position(|x| e_regex.is_match(x.trim()));
if let Some((pos, value)) = expand_tabs_option
&& value.trim().len() <= 2
{
arguments[pos] = "-e\t8".to_string();
if let Some((pos, value)) = expand_tabs_option {
arguments[pos] = format!("{}\t8", value.trim());
}

// Remove only whole-token legacy operands before clap parsing.
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,34 @@
.stdout_matches(&output_regex);
}

#[test]
fn test_expand_tab_at_end_of_short_flag_cluster() {
// `-e` closing a cluster of value-less short flags carries no attached argument, so it has
// to fall back to the defaults rather than report a missing value.
for (arg, expected) in [
("-tre", "oi\n"),
("-tre8", "oi\n"),
("-tfre", "oi\n\x0c"),

Check warning on line 955 in tests/by-util/test_pr.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'tfre' (file:'tests/by-util/test_pr.rs', line:955)
("-tfre8", "oi\n\x0c"),

Check warning on line 956 in tests/by-util/test_pr.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'tfre' (file:'tests/by-util/test_pr.rs', line:956)
] {
new_ucmd!()
.arg(arg)
.pipe_in("oi\n")
.succeeds()
.stdout_only(expected);
}
}

#[test]
fn test_expand_tab_does_not_consume_following_operand() {
// A bare `-e` must leave the file operand alone.
new_ucmd!()
.args(&["-t", "-e"])
.pipe_in("a\tb\n")
.succeeds()
.stdout_only("a b\n");
}

#[test]
fn test_simple_expand_tab_with_digit_argument() {
let whitespace = " ".repeat(50);
Expand Down
Loading