Skip to content

Commit b4cdc36

Browse files
authored
Merge pull request #6997 from cakebaker/echo_handle_double_hyphen
echo: handle double hyphens
2 parents 958320e + 9046535 commit b4cdc36

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/uu/echo/src/echo.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,26 @@ fn print_escaped(input: &[u8], output: &mut StdoutLock) -> io::Result<ControlFlo
255255
Ok(ControlFlow::Continue(()))
256256
}
257257

258+
// A workaround because clap interprets the first '--' as a marker that a value
259+
// follows. In order to use '--' as a value, we have to inject an additional '--'
260+
fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
261+
let mut result = Vec::new();
262+
let mut is_first_double_hyphen = true;
263+
264+
for arg in args {
265+
if arg == "--" && is_first_double_hyphen {
266+
result.push(OsString::from("--"));
267+
is_first_double_hyphen = false;
268+
}
269+
result.push(arg);
270+
}
271+
272+
result.into_iter()
273+
}
274+
258275
#[uucore::main]
259276
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
260-
let matches = uu_app().get_matches_from(args);
277+
let matches = uu_app().get_matches_from(handle_double_hyphens(args));
261278

262279
// TODO
263280
// "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."

tests/by-util/test_echo.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ fn test_hyphen_values_at_start() {
219219
.arg("-test")
220220
.arg("araba")
221221
.arg("-merci")
222-
.run()
223-
.success()
222+
.succeeds()
224223
.stdout_does_not_contain("-E")
225224
.stdout_is("-test araba -merci\n");
226225
}
@@ -231,20 +230,28 @@ fn test_hyphen_values_between() {
231230
.arg("test")
232231
.arg("-E")
233232
.arg("araba")
234-
.run()
235-
.success()
233+
.succeeds()
236234
.stdout_is("test -E araba\n");
237235

238236
new_ucmd!()
239237
.arg("dumdum ")
240238
.arg("dum dum dum")
241239
.arg("-e")
242240
.arg("dum")
243-
.run()
244-
.success()
241+
.succeeds()
245242
.stdout_is("dumdum dum dum dum -e dum\n");
246243
}
247244

245+
#[test]
246+
fn test_double_hyphens() {
247+
new_ucmd!().arg("--").succeeds().stdout_only("--\n");
248+
new_ucmd!()
249+
.arg("--")
250+
.arg("--")
251+
.succeeds()
252+
.stdout_only("-- --\n");
253+
}
254+
248255
#[test]
249256
fn wrapping_octal() {
250257
// Some odd behavior of GNU. Values of \0400 and greater do not fit in the

0 commit comments

Comments
 (0)