mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
echo: fixed double hyphen as argument (#7581)
* Fixes #7558 Added check to only insert addition double hyphen if at start of arguments to correctly prepend addition hyphens for clap as well as additional test case * additional comment * fixes issue where flags precedes "--" as arguments
This commit is contained in:
parent
1c75854d2f
commit
8c8beb96e4
2 changed files with 121 additions and 6 deletions
|
@ -23,18 +23,32 @@ mod options {
|
||||||
pub const DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape";
|
pub const DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_echo_flag(arg: &OsString) -> bool {
|
||||||
|
matches!(arg.to_str(), Some("-e" | "-E" | "-n"))
|
||||||
|
}
|
||||||
|
|
||||||
// A workaround because clap interprets the first '--' as a marker that a value
|
// A workaround because clap interprets the first '--' as a marker that a value
|
||||||
// follows. In order to use '--' as a value, we have to inject an additional '--'
|
// follows. In order to use '--' as a value, we have to inject an additional '--'
|
||||||
fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
|
fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
let mut is_first_double_hyphen = true;
|
let mut is_first_argument = true;
|
||||||
|
let mut args_iter = args.into_iter();
|
||||||
|
|
||||||
for arg in args {
|
if let Some(first_val) = args_iter.next() {
|
||||||
if arg == "--" && is_first_double_hyphen {
|
// the first argument ('echo') gets pushed before we start with the checks for flags/'--'
|
||||||
result.push(OsString::from("--"));
|
result.push(first_val);
|
||||||
is_first_double_hyphen = false;
|
// We need to skip any possible Flag arguments until we find the first argument to echo that
|
||||||
|
// is not a flag. If the first argument is double hyphen we inject an additional '--'
|
||||||
|
// otherwise we switch is_first_argument boolean to skip the checks for any further arguments
|
||||||
|
for arg in args_iter {
|
||||||
|
if is_first_argument && !is_echo_flag(&arg) {
|
||||||
|
is_first_argument = false;
|
||||||
|
if arg == "--" {
|
||||||
|
result.push(OsString::from("--"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push(arg);
|
||||||
}
|
}
|
||||||
result.push(arg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.into_iter()
|
result.into_iter()
|
||||||
|
|
|
@ -242,6 +242,84 @@ fn test_hyphen_values_between() {
|
||||||
.stdout_is("dumdum dum dum dum -e dum\n");
|
.stdout_is("dumdum dum dum dum -e dum\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_double_hyphens_at_start() {
|
||||||
|
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- --\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.arg("b")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a b\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.arg("b")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a b --\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_double_hyphens_after_flags() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-e")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("--\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-n")
|
||||||
|
.arg("-e")
|
||||||
|
.arg("--")
|
||||||
|
.arg("foo\n")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- foo\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-e")
|
||||||
|
.arg("--")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- --\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-e")
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a --\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-n")
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-n")
|
||||||
|
.arg("--")
|
||||||
|
.arg("a")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("-- a --");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_double_hyphens() {
|
fn test_double_hyphens() {
|
||||||
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
|
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
|
||||||
|
@ -250,6 +328,29 @@ fn test_double_hyphens() {
|
||||||
.arg("--")
|
.arg("--")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("-- --\n");
|
.stdout_only("-- --\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("a")
|
||||||
|
.arg("--")
|
||||||
|
.arg("b")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("a -- b\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("a")
|
||||||
|
.arg("--")
|
||||||
|
.arg("b")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("a -- b --\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("a")
|
||||||
|
.arg("b")
|
||||||
|
.arg("--")
|
||||||
|
.arg("--")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("a b -- --\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue