diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 843b3040812..7cab3f81afe 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -8,6 +8,7 @@ use clap::{Arg, ArgAction, ArgMatches, Command}; use std::env; use std::ffi::{OsStr, OsString}; use std::io::{self, StdoutLock, Write}; +use std::os::unix::ffi::OsStrExt; use uucore::error::{UResult, USimpleError}; use uucore::format::{parse_escape_only, EscapedChar, FormatChar, OctalParsing}; use uucore::{format_usage, help_about, help_section, help_usage}; @@ -28,11 +29,16 @@ mod options { fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args { let mut result = Vec::new(); let mut is_first_double_hyphen = true; - - for arg in args { - if arg == "--" && is_first_double_hyphen { - result.push(OsString::from("--")); - is_first_double_hyphen = false; + let mut option_index = 0; + + for (i, arg) in args.enumerate() { + if is_first_double_hyphen { + if i < option_index + 2 && arg == "--" { + result.push(OsString::from("--")); + is_first_double_hyphen = false; + } else if arg.as_bytes()[0] == b'-' { + option_index = i + } } result.push(arg); } diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index d4430d05655..11676967493 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (words) araba merci +// spell-checker:ignore (words) araba merci hehehe use crate::common::util::TestScenario; @@ -243,7 +243,7 @@ fn test_hyphen_values_between() { } #[test] -fn test_double_hyphens() { +fn test_double_hyphens_at_start() { new_ucmd!().arg("--").succeeds().stdout_only("--\n"); new_ucmd!() .arg("--") @@ -252,6 +252,24 @@ fn test_double_hyphens() { .stdout_only("-- --\n"); } +#[test] +fn test_double_hyphens_between() { + new_ucmd!() + .arg("a") + .arg("--") + .arg("b") + .succeeds() + .stdout_only("a -- b\n"); + + new_ucmd!() + .arg("foo ") + .arg("-- --") + .arg("bar") + .arg("hehehe") + .succeeds() + .stdout_only("foo -- -- bar hehehe\n"); +} + #[test] fn wrapping_octal() { // Some odd behavior of GNU. Values of \0400 and greater do not fit in the