Skip to content
Merged
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
19 changes: 18 additions & 1 deletion src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,26 @@ fn print_escaped(input: &[u8], output: &mut StdoutLock) -> io::Result<ControlFlo
Ok(ControlFlow::Continue(()))
}

// 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 '--'
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;
}
result.push(arg);
}

result.into_iter()
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args);
let matches = uu_app().get_matches_from(handle_double_hyphens(args));

// TODO
// "If the POSIXLY_CORRECT environment variable is set, then when echo’s first argument is not -n it outputs option-like arguments instead of treating them as options."
Expand Down
19 changes: 13 additions & 6 deletions tests/by-util/test_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ fn test_hyphen_values_at_start() {
.arg("-test")
.arg("araba")
.arg("-merci")
.run()
.success()
.succeeds()
.stdout_does_not_contain("-E")
.stdout_is("-test araba -merci\n");
}
Expand All @@ -231,20 +230,28 @@ fn test_hyphen_values_between() {
.arg("test")
.arg("-E")
.arg("araba")
.run()
.success()
.succeeds()
.stdout_is("test -E araba\n");

new_ucmd!()
.arg("dumdum ")
.arg("dum dum dum")
.arg("-e")
.arg("dum")
.run()
.success()
.succeeds()
.stdout_is("dumdum dum dum dum -e dum\n");
}

#[test]
fn test_double_hyphens() {
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
new_ucmd!()
.arg("--")
.arg("--")
.succeeds()
.stdout_only("-- --\n");
}

#[test]
fn wrapping_octal() {
// Some odd behavior of GNU. Values of \0400 and greater do not fit in the
Expand Down
Loading