Allow cli prefs to have numerical value

Fix for #14842.

Extract the code inside a function to unit-test it.
This commit is contained in:
charlesvdv 2017-01-05 16:27:40 +01:00
parent 4216c16879
commit 6a22174eb7
2 changed files with 40 additions and 9 deletions

View file

@ -889,14 +889,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
prefs::add_user_prefs();
for pref in opt_match.opt_strs("pref").iter() {
let split: Vec<&str> = pref.splitn(2, '=').collect();
let pref_name = split[0];
let value = split.get(1);
match value {
Some(&"false") => PREFS.set(pref_name, PrefValue::Boolean(false)),
Some(&"true") | None => PREFS.set(pref_name, PrefValue::Boolean(true)),
_ => PREFS.set(pref_name, PrefValue::String(value.unwrap().to_string()))
};
parse_pref_from_command_line(pref);
}
if let Some(layout_threads) = layout_threads {
@ -946,6 +939,20 @@ pub fn set_defaults(opts: Opts) {
}
}
pub fn parse_pref_from_command_line(pref: &str) {
let split: Vec<&str> = pref.splitn(2, '=').collect();
let pref_name = split[0];
let value = split.get(1);
match value {
Some(&"false") => PREFS.set(pref_name, PrefValue::Boolean(false)),
Some(&"true") | None => PREFS.set(pref_name, PrefValue::Boolean(true)),
Some(value) => match value.parse::<f64>() {
Ok(v) => PREFS.set(pref_name, PrefValue::Number(v)),
Err(_) => PREFS.set(pref_name, PrefValue::String(value.to_string()))
}
};
}
#[inline]
pub fn get() -> &'static Opts {
&OPTIONS