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
55
56
57
58
59
use self::super::alias_to_fqdn;
use clap::{AppSettings, Arg};
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Options {
pub aliases: Vec<String>,
pub verbose: bool,
pub raw: bool,
pub currency_filter: Option<Vec<String>>,
}
impl Options {
pub fn parse() -> Options {
let matches = app_from_crate!("\n")
.setting(AppSettings::ColoredHelp)
.arg(Arg::from_usage("<OPEN_ALIAS>... 'Aliases to look up'").validator(Options::open_alias_validator).required(true))
.arg(Arg::from_usage("-v --verbose 'Print out more information'"))
.arg(Arg::from_usage("-r --raw 'Print just the record text'"))
.arg(Arg::from_usage("-c --currency=[CURRENCY]... 'Limit results to just CURRENCY'"))
.get_matches();
Options {
aliases: matches.values_of("OPEN_ALIAS").unwrap().map(String::from).collect(),
verbose: matches.is_present("verbose"),
raw: matches.is_present("raw"),
currency_filter: matches.values_of("currency").map(|cs| cs.map(String::from).collect()),
}
}
fn open_alias_validator(s: String) -> Result<(), String> {
alias_to_fqdn(&s).map(|_| ()).ok_or_else(|| format!("{} is not a valid OpenAlias address", s))
}
}