Files
Terts Diepraam 08c0383cb9 move Arg from associated type to type parameter
This allows a struct to implement Options for multiple types. See the tail test for rationale
2023-02-15 14:53:29 +01:00

59 lines
1.4 KiB
Rust

use std::ffi::OsString;
use uutils_args::{Arguments, Initial, Options};
#[derive(Arguments)]
#[arguments(parse_echo_style)]
enum Arg {
/// Do not output trailing newline
#[option("-n")]
NoNewline,
/// Enable interpretation of backslash escapes
#[option("-e")]
EnableEscape,
/// Disable interpretation of backslash escapes
#[option("-E")]
DisableEscape,
#[positional(last)]
String(Vec<OsString>),
}
#[derive(Initial)]
struct Settings {
trailing_newline: bool,
escape: bool,
strings: Vec<OsString>,
}
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,
Arg::String(s) => self.strings = s,
}
}
}
// These next two tests exemplify echo style parsing. Which we have to
// support explicitly.
#[test]
fn double_hyphen() {
let s = Settings::parse(["echo", "--"]);
assert_eq!(s.strings, vec![OsString::from("--")]);
let s = Settings::parse(["echo", "--", "-n"]);
assert_eq!(s.strings, vec![OsString::from("--"), OsString::from("-n")]);
}
#[test]
#[ignore]
fn nonexistent_options_are_values() {
let s = Settings::parse(["echo", "-f"]);
assert_eq!(s.strings, vec![OsString::from("-f")]);
}