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
use clap::{AppSettings, Arg};
use reqwest::Url;
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Options {
pub remote_dir: Url,
}
impl Options {
pub fn parse() -> Options {
let matches = app_from_crate!("\n")
.setting(AppSettings::ColoredHelp)
.arg(Arg::from_usage("<URL> 'Remote directory to browse'").validator(Options::url_validator))
.get_matches();
let u = matches.value_of("URL").unwrap();
Options {
remote_dir: Url::parse(u)
.or_else(|_| Url::parse(&format!("http://{}", u)))
.unwrap(),
}
}
fn url_validator(s: String) -> Result<(), String> {
Url::parse(&s)
.or_else(|_| Url::parse(&format!("http://{}", s)))
.map(|_| ())
.map_err(|e| e.to_string())
}
}