Optimize CLI argument handling with defaults

This commit is contained in:
timvisee
2018-01-11 20:23:15 +01:00
parent 7a3ae058e8
commit cff40b268f

View File

@@ -94,9 +94,10 @@ impl<'a: 'b, 'b> ArgHandler<'a> {
/// Get the thread count. /// Get the thread count.
pub fn count(&self) -> usize { pub fn count(&self) -> usize {
self.matches.value_of("count") self.matches.value_of("count")
.unwrap_or(&format!("{}", num_cpus::get())) .map(|count| count.parse::<usize>()
.parse::<usize>() .expect("Invalid count specified")
.expect("Invalid count specified") )
.unwrap_or(num_cpus::get())
} }
/// Get the image paths. /// Get the image paths.
@@ -111,17 +112,19 @@ impl<'a: 'b, 'b> ArgHandler<'a> {
pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) { pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) {
( (
self.matches.value_of("width") self.matches.value_of("width")
.unwrap_or( .map(|width| width.parse::<u32>()
&format!("{}", def.expect("No screen width set or known").0) .expect("Invalid image width")
) )
.parse::<u32>() .unwrap_or(
.expect("Invalid image width"), def.expect("No screen width set or known").0
),
self.matches.value_of("height") self.matches.value_of("height")
.unwrap_or( .map(|height| height.parse::<u32>()
&format!("{}", def.expect("No screen height set or known").1) .expect("Invalid image height")
) )
.parse::<u32>() .unwrap_or(
.expect("Invalid image height"), def.expect("No screen height set or known").1
),
) )
} }
@@ -129,21 +132,24 @@ impl<'a: 'b, 'b> ArgHandler<'a> {
pub fn offset(&self) -> (u32, u32) { pub fn offset(&self) -> (u32, u32) {
( (
self.matches.value_of("x") self.matches.value_of("x")
.unwrap_or("0") .map(|x| x.parse::<u32>()
.parse::<u32>() .expect("Invalid X offset")
.expect("Invalid X offset"), )
.unwrap_or(0),
self.matches.value_of("y") self.matches.value_of("y")
.unwrap_or("0") .map(|y| y.parse::<u32>()
.parse::<u32>() .expect("Invalid Y offset")
.expect("Invalid Y offset"), )
.unwrap_or(0),
) )
} }
/// Get the FPS. /// Get the FPS.
pub fn fps(&self) -> u32 { pub fn fps(&self) -> u32 {
self.matches.value_of("fps") self.matches.value_of("fps")
.unwrap_or(&format!("{}", DEFAULT_IMAGE_FPS)) .map(|fps| fps.parse::<u32>()
.parse::<u32>() .expect("Invalid frames per second rate")
.expect("Invalid frames per second rate") )
.unwrap_or(DEFAULT_IMAGE_FPS)
} }
} }