-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexclusives.rs
More file actions
34 lines (32 loc) · 1.1 KB
/
exclusives.rs
File metadata and controls
34 lines (32 loc) · 1.1 KB
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
use args::{
ArgsError,
Argument,
ArgumentParser,
ArgumentType,
};
fn main() {
// --json and --yaml are mutually exclusive; --out requires --format
let parser = ArgumentParser::new("exclusive")
.with_exclusive_group(&["--json", "--yaml"])
.with_requires("--out", "--format")
.with_argument(Argument::new("--json").with_type(ArgumentType::Boolean))
.with_argument(Argument::new("--yaml").with_type(ArgumentType::Boolean))
.with_argument(Argument::new("--format").with_type(ArgumentType::String))
.with_argument(Argument::new("--out").with_type(ArgumentType::String));
let args: Vec<String> = std::env::args().collect();
match parser.parse(&args) {
Ok(parsed) => {
let fmt = parsed
.get_string("--format")
.unwrap_or_else(|| "json".into());
let out = parsed
.get_string("--out")
.unwrap_or_else(|| "stdout".into());
println!("fmt={}, out={}", fmt, out);
}
Err(ArgsError::InvalidValue { name, expected, .. }) => {
eprintln!("Validation error on {}: {}", name, expected);
}
Err(e) => eprintln!("{}", e),
}
}