Skip to content

Commit a357dce

Browse files
committed
fixes issue where flags precedes "--" as arguments
1 parent b0df222 commit a357dce

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/uu/echo/src/echo.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,34 @@ mod options {
2323
pub const DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape";
2424
}
2525

26+
fn is_echo_flag(arg: &OsString) -> bool {
27+
matches!(arg.to_str(), Some("-e" | "-E" | "-n"))
28+
}
29+
2630
// A workaround because clap interprets the first '--' as a marker that a value
2731
// follows. In order to use '--' as a value, we have to inject an additional '--'
2832
fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
2933
let mut result = Vec::new();
30-
31-
for (i, arg) in args.enumerate() {
32-
// check for argument at index 1 which is the first argument to echo (0 being "echo")
33-
if i == 1 && arg == "--" {
34-
result.push(OsString::from("--"));
34+
let mut is_first_argument = true;
35+
let mut args_iter = args.into_iter();
36+
37+
if let Some(first_val) = args_iter.next() {
38+
// the first argument ('echo') gets pushed before we start with the checks for flags/'--'
39+
result.push(first_val);
40+
// We need to skip any possible Flag arguments until we find the first argument to echo that
41+
// is not a flag. If the first argument is double hyphen we inject an additional '--'
42+
// otherwhise we switch is_first_argument boolean to skip the checks for any further arguments
43+
for arg in args_iter {
44+
if is_first_argument {
45+
if !is_echo_flag(&arg) {
46+
is_first_argument = false;
47+
if arg == "--" {
48+
result.push(OsString::from("--"));
49+
}
50+
}
51+
}
52+
result.push(arg);
3553
}
36-
result.push(arg);
3754
}
3855

3956
result.into_iter()

tests/by-util/test_echo.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,53 @@ fn test_double_hyphens_at_start() {
273273
.stdout_only("-- a b --\n");
274274
}
275275

276+
#[test]
277+
fn test_double_hyphens_after_flags() {
278+
new_ucmd!()
279+
.arg("-e")
280+
.arg("--")
281+
.succeeds()
282+
.stdout_only("--\n");
283+
284+
new_ucmd!()
285+
.arg("-n")
286+
.arg("-e")
287+
.arg("--")
288+
.arg("foo\n")
289+
.succeeds()
290+
.stdout_only("-- foot \n");
291+
292+
new_ucmd!()
293+
.arg("-e")
294+
.arg("--")
295+
.arg("--")
296+
.succeeds()
297+
.stdout_only("-- --\n");
298+
299+
new_ucmd!()
300+
.arg("-e")
301+
.arg("--")
302+
.arg("a")
303+
.arg("--")
304+
.succeeds()
305+
.stdout_only("-- a --\n");
306+
307+
new_ucmd!()
308+
.arg("-n")
309+
.arg("--")
310+
.arg("a")
311+
.succeeds()
312+
.stdout_only("-- a");
313+
314+
new_ucmd!()
315+
.arg("-n")
316+
.arg("--")
317+
.arg("a")
318+
.arg("--")
319+
.succeeds()
320+
.stdout_only("-- a --");
321+
}
322+
276323
#[test]
277324
fn test_double_hyphens() {
278325
new_ucmd!().arg("--").succeeds().stdout_only("--\n");

0 commit comments

Comments
 (0)