Skip to content
Closed
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
16 changes: 11 additions & 5 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
}
Expand Down
22 changes: 20 additions & 2 deletions tests/by-util/test_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("--")
Expand All @@ -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
Expand Down
Loading