-
Notifications
You must be signed in to change notification settings - Fork 3
/
echo.rs
54 lines (45 loc) · 1.38 KB
/
echo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::ffi::OsString;
use uutils_args::{Arguments, Options};
#[derive(Arguments)]
#[arguments(parse_echo_style)]
enum Arg {
/// Do not output trailing newline
#[arg("-n")]
NoNewline,
/// Enable interpretation of backslash escapes
#[arg("-e")]
EnableEscape,
/// Disable interpretation of backslash escapes
#[arg("-E")]
DisableEscape,
}
#[derive(Default)]
struct Settings {
trailing_newline: bool,
escape: bool,
}
impl Options<Arg> for Settings {
fn apply(&mut self, arg: Arg) {
match arg {
Arg::NoNewline => self.trailing_newline = false,
Arg::EnableEscape => self.escape = true,
Arg::DisableEscape => self.escape = false,
}
}
}
// These next two tests exemplify echo style parsing. Which we have to
// support explicitly.
#[test]
#[ignore = "needs to be fixed after positional argument refactor"]
fn double_hyphen() {
let (_, operands) = Settings::default().parse(["echo", "--"]).unwrap();
assert_eq!(operands, vec![OsString::from("--")]);
let (_, operands) = Settings::default().parse(["echo", "--", "-n"]).unwrap();
assert_eq!(operands, vec![OsString::from("--"), OsString::from("-n")]);
}
#[test]
#[ignore]
fn nonexistent_options_are_values() {
let (_, operands) = Settings::default().parse(["echo", "-f"]).unwrap();
assert_eq!(operands, vec![OsString::from("-f")]);
}