Skip to content

Commit 8c8beb9

Browse files
authored
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
1 parent 1c75854 commit 8c8beb9

File tree

2 files changed

+122
-7
lines changed

2 files changed

+122
-7
lines changed

src/uu/echo/src/echo.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,32 @@ 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-
let mut is_first_double_hyphen = true;
31-
32-
for arg in args {
33-
if arg == "--" && is_first_double_hyphen {
34-
result.push(OsString::from("--"));
35-
is_first_double_hyphen = false;
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+
// otherwise 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 && !is_echo_flag(&arg) {
45+
is_first_argument = false;
46+
if arg == "--" {
47+
result.push(OsString::from("--"));
48+
}
49+
}
50+
result.push(arg);
3651
}
37-
result.push(arg);
3852
}
3953

4054
result.into_iter()

tests/by-util/test_echo.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,84 @@ fn test_hyphen_values_between() {
242242
.stdout_is("dumdum dum dum dum -e dum\n");
243243
}
244244

245+
#[test]
246+
fn test_double_hyphens_at_start() {
247+
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
248+
new_ucmd!()
249+
.arg("--")
250+
.arg("--")
251+
.succeeds()
252+
.stdout_only("-- --\n");
253+
254+
new_ucmd!()
255+
.arg("--")
256+
.arg("a")
257+
.succeeds()
258+
.stdout_only("-- a\n");
259+
260+
new_ucmd!()
261+
.arg("--")
262+
.arg("a")
263+
.arg("b")
264+
.succeeds()
265+
.stdout_only("-- a b\n");
266+
267+
new_ucmd!()
268+
.arg("--")
269+
.arg("a")
270+
.arg("b")
271+
.arg("--")
272+
.succeeds()
273+
.stdout_only("-- a b --\n");
274+
}
275+
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("-- foo\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+
245323
#[test]
246324
fn test_double_hyphens() {
247325
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
@@ -250,6 +328,29 @@ fn test_double_hyphens() {
250328
.arg("--")
251329
.succeeds()
252330
.stdout_only("-- --\n");
331+
332+
new_ucmd!()
333+
.arg("a")
334+
.arg("--")
335+
.arg("b")
336+
.succeeds()
337+
.stdout_only("a -- b\n");
338+
339+
new_ucmd!()
340+
.arg("a")
341+
.arg("--")
342+
.arg("b")
343+
.arg("--")
344+
.succeeds()
345+
.stdout_only("a -- b --\n");
346+
347+
new_ucmd!()
348+
.arg("a")
349+
.arg("b")
350+
.arg("--")
351+
.arg("--")
352+
.succeeds()
353+
.stdout_only("a b -- --\n");
253354
}
254355

255356
#[test]

0 commit comments

Comments
 (0)