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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use getopts::Options;
use std::env;
use std::path::PathBuf;
use std::process;
#[derive(Debug, Default)]
pub struct Args {
pub config: Option<PathBuf>,
pub server: Option<String>,
pub auth: Option<String>,
pub url: Option<String>,
pub remote: Option<String>,
pub files: Vec<String>,
pub oneshot: bool,
pub expire: Option<String>,
pub prettify: bool,
pub print_server_version: bool,
}
impl Args {
pub fn parse() -> Self {
let mut opts = Options::new();
opts.optflag("h", "help", "prints help information");
opts.optflag("v", "version", "prints version information");
opts.optflag("V", "server-version", "retrieves the server version");
opts.optflag("o", "oneshot", "generates one shot links");
opts.optflag("p", "pretty", "prettifies the output");
opts.optopt("c", "config", "sets the configuration file", "CONFIG");
opts.optopt(
"s",
"server",
"sets the address of the rustypaste server",
"SERVER",
);
opts.optopt("a", "auth", "sets the authentication token", "TOKEN");
opts.optopt("u", "url", "sets the URL to shorten", "URL");
opts.optopt("r", "remote", "sets the remote URL for uploading", "URL");
opts.optopt(
"e",
"expire",
"sets the expiration time for the link",
"TIME",
);
let env_args: Vec<String> = env::args().collect();
let matches = match opts.parse(&env_args[1..]) {
Ok(m) => m,
Err(e) => {
eprintln!("Argument error: `{}`", e);
process::exit(1);
}
};
if matches.opt_present("h")
|| (matches.free.is_empty()
&& !matches.opt_present("u")
&& !matches.opt_present("r")
&& !matches.opt_present("V")
&& !matches.opt_present("v"))
{
let usage = format!(
"\n{} {} \u{2014} {}.\
\n\u{221F} written by {}\
\n\u{221F} licensed under MIT <{}>\
\n\nUsage:\n {} [options] <file(s)>",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_DESCRIPTION"),
env!("CARGO_PKG_AUTHORS"),
env!("CARGO_PKG_REPOSITORY"),
"rpaste",
);
println!("{}", opts.usage(&usage));
process::exit(0)
}
if matches.opt_present("v") {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
process::exit(0)
}
Args {
config: env::var("RPASTE_CONFIG")
.ok()
.or_else(|| matches.opt_str("c"))
.map(PathBuf::from),
server: matches.opt_str("s"),
auth: matches.opt_str("a"),
url: matches.opt_str("u"),
remote: matches.opt_str("r"),
oneshot: matches.opt_present("o"),
expire: matches.opt_str("e"),
prettify: matches.opt_present("p"),
print_server_version: matches.opt_present("V"),
files: matches.free,
}
}
}