Skip to content

Commit c34b7f6

Browse files
aw-server: Added option parsing and --testing option
1 parent cd00f31 commit c34b7f6

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aw-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fern = { version = "0.5", features = ["colored"] }
2929
toml = "0.5"
3030
gethostname = "0.2"
3131
uuid = { version = "0.8", features = ["serde", "v4"] }
32+
getopts = "0.2"
3233

3334
aw-datastore = { path = "../aw-datastore" }
3435
aw-models = { path = "../aw-models" }

aw-server/src/main.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
#[macro_use]
22
extern crate log;
3+
extern crate getopts;
34

5+
use getopts::Options;
46
use rocket::config::Environment;
7+
use std::env;
58

69
use aw_server::*;
710

11+
fn print_usage(program: &str, opts: Options) {
12+
let brief = format!("Usage: {} FILE [options]", program);
13+
print!("{}", opts.usage(&brief));
14+
}
15+
816
fn main() {
917
use std::sync::Mutex;
1018

19+
let args: Vec<String> = env::args().collect();
20+
let program = args[0].clone();
21+
22+
let mut opts = Options::new();
23+
opts.optflag("", "testing", "run in testing mode");
24+
opts.optflag("h", "help", "print this help menu");
25+
let matches = match opts.parse(&args[1..]) {
26+
Ok(m) => m,
27+
Err(f) => panic!(f.to_string()),
28+
};
29+
if matches.opt_present("h") {
30+
print_usage(&program, opts);
31+
return;
32+
}
33+
1134
let env = Environment::active().expect("Failed to get current environment");
12-
let testing = match env {
35+
let mut testing = match env {
1336
Environment::Production => false,
1437
Environment::Development => true,
1538
Environment::Staging => panic!("Staging environment not supported"),
1639
};
40+
// Always override environment if --testing is specified
41+
if matches.opt_present("testing") {
42+
testing = true;
43+
}
1744

1845
logging::setup_logger(testing).expect("Failed to setup logging");
1946

@@ -45,7 +72,6 @@ use std::path::PathBuf;
4572
// TODO: Should we talk to upstream about this? This changes the behavior quite a lot so maybe they
4673
// don't want this change?
4774
fn site_data_dir(app: Option<&str>, _: Option<&str>) -> Result<PathBuf, ()> {
48-
use std::env;
4975
// Iterate over all XDG_DATA_DIRS and return first match that exists
5076
match env::var_os("XDG_DATA_DIRS") {
5177
Some(joined) => {

0 commit comments

Comments
 (0)