Browse Source
Moved argument parsing into module
Moved argument parsing into module
* Port was redefined as i16 as that is the valid range for ports * argument parsing is cleaned up and moved into args module * correctly prefer arguments over config if presentdevelop
Drew Short
6 years ago
3 changed files with 64 additions and 25 deletions
@ -0,0 +1,56 @@ |
|||||
|
use std::str::FromStr;
|
||||
|
|
||||
|
use clap::ArgMatches;
|
||||
|
|
||||
|
use config::model::Config;
|
||||
|
|
||||
|
fn get_config_for_string(arg_value: Option<&str>, config_value: Option<String>, default_value: &str) -> String {
|
||||
|
String::from(match arg_value {
|
||||
|
Some(v) => v,
|
||||
|
None => match c.server.host {
|
||||
|
Some(host) => host,
|
||||
|
None => default_host,
|
||||
|
}
|
||||
|
})
|
||||
|
}
|
||||
|
|
||||
|
fn get_config_for_number<T: FromStr>(arg_value: Option<&str>, config_value: Option<T>, default_value: T) -> T {
|
||||
|
match arg_value {
|
||||
|
Some(v) => v.parse::<T>().unwrap_or(default_value),
|
||||
|
None => match config_value {
|
||||
|
Some(v) => v,
|
||||
|
None => default_value
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
fn get_number<T: FromStr>(arg_value: Option<&str>, default_value: T) -> T {
|
||||
|
match arg_value {
|
||||
|
Some(value) => port.parse::<T>().unwrap_or(default_port),
|
||||
|
None => default_port
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub fn get_host(args: &ArgMatches, config: &Option<Config>, default_host: &str) -> String {
|
||||
|
let arg_value = args.value_of("host");
|
||||
|
match config {
|
||||
|
Some(c) => get_config_for_string(arg_value, c.server.host, default_host),
|
||||
|
None => String::from(arg_value.unwrap_or(default_host))
|
||||
|
}.clone()
|
||||
|
}
|
||||
|
|
||||
|
pub fn get_port(args: &ArgMatches, config: &Option<Config>, default_port: i16) -> i16 {
|
||||
|
let arg_value = args.value_of("port");
|
||||
|
match config {
|
||||
|
Some(c) => get_config_for_number(arg_value, c.server.port, default_port),
|
||||
|
None => get_number(arg_value, default_port)
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub fn get_workers(args: &ArgMatches, config: &Option<Config>, default_workers: usize) -> usize {
|
||||
|
let arg_value = args.value_of("workers");
|
||||
|
match config {
|
||||
|
Some(c) => get_config_for_number(arg_value, c.server.workers, default_workers),
|
||||
|
None => get_number(args_value, default_workers)
|
||||
|
}
|
||||
|
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue