From cff40b268f4bab3502c88a611a61225e80b9b29c Mon Sep 17 00:00:00 2001 From: timvisee Date: Thu, 11 Jan 2018 20:23:15 +0100 Subject: [PATCH] Optimize CLI argument handling with defaults --- src/arg_handler.rs | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/arg_handler.rs b/src/arg_handler.rs index 44a9407..5ad3780 100644 --- a/src/arg_handler.rs +++ b/src/arg_handler.rs @@ -94,9 +94,10 @@ impl<'a: 'b, 'b> ArgHandler<'a> { /// Get the thread count. pub fn count(&self) -> usize { self.matches.value_of("count") - .unwrap_or(&format!("{}", num_cpus::get())) - .parse::() - .expect("Invalid count specified") + .map(|count| count.parse::() + .expect("Invalid count specified") + ) + .unwrap_or(num_cpus::get()) } /// Get the image paths. @@ -111,17 +112,19 @@ impl<'a: 'b, 'b> ArgHandler<'a> { pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) { ( self.matches.value_of("width") - .unwrap_or( - &format!("{}", def.expect("No screen width set or known").0) + .map(|width| width.parse::() + .expect("Invalid image width") ) - .parse::() - .expect("Invalid image width"), + .unwrap_or( + def.expect("No screen width set or known").0 + ), self.matches.value_of("height") - .unwrap_or( - &format!("{}", def.expect("No screen height set or known").1) + .map(|height| height.parse::() + .expect("Invalid image height") ) - .parse::() - .expect("Invalid image height"), + .unwrap_or( + 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) { ( self.matches.value_of("x") - .unwrap_or("0") - .parse::() - .expect("Invalid X offset"), + .map(|x| x.parse::() + .expect("Invalid X offset") + ) + .unwrap_or(0), self.matches.value_of("y") - .unwrap_or("0") - .parse::() - .expect("Invalid Y offset"), + .map(|y| y.parse::() + .expect("Invalid Y offset") + ) + .unwrap_or(0), ) } /// Get the FPS. pub fn fps(&self) -> u32 { self.matches.value_of("fps") - .unwrap_or(&format!("{}", DEFAULT_IMAGE_FPS)) - .parse::() - .expect("Invalid frames per second rate") + .map(|fps| fps.parse::() + .expect("Invalid frames per second rate") + ) + .unwrap_or(DEFAULT_IMAGE_FPS) } }