From 9ea81eb34612a5fe4a92068279c2c9d2ff6c5caf Mon Sep 17 00:00:00 2001 From: Simon Egersand Date: Sun, 5 Aug 2018 19:52:46 +0200 Subject: [PATCH] Move short flag into OptionsCli struct I would like to keep all options from the CLI in the OptionsCli struct. That way we can just move around that struct and we don't have to worry about what specific value is sent to what function - we can just expect to receive this struct and look for the value we're interested in. All future flags and options should be added to this struct. --- src/bin/commentective.rs | 50 ++++++++++++++++++++-------------------- src/lib.rs | 4 +++- src/printer.rs | 17 +++++++------- tests/lib.rs | 11 +++++---- 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/bin/commentective.rs b/src/bin/commentective.rs index a1d8cf7..9805b62 100644 --- a/src/bin/commentective.rs +++ b/src/bin/commentective.rs @@ -14,31 +14,28 @@ use std::path::Path; use std::process; fn main() { + let arg_short = Arg::with_name("short").short("s").long("short").help( + "Formats output with \"file.ext:line\" without colors. Only outputs files with comments.", + ); + let arg_extension = Arg::with_name("extension") + .short("e") + .long("extension") + .help("Only analyze files with this extension") + .takes_value(true); + let arg_files = Arg::with_name("FILES") + .help("Files to analyze") + .required(true) + .multiple(true) + .validator_os(exists_on_filesystem) + .index(1); + let matches = App::new("commentective") .version("0.3.0") .author("Simon Egersand ") .about("CLI tool to find comments and commented out code") - .arg( - Arg::with_name("FILES") - .help("Files to analyze") - .required(true) - .multiple(true) - .validator_os(exists_on_filesystem) - .index(1), - ) - .arg( - Arg::with_name("extension") - .short("e") - .long("extension") - .help("Only analyze files with this extension") - .takes_value(true), - ) - .arg( - Arg::with_name("short") - .short("s") - .long("short") - .help("Formats output with \"file.ext:line\" without colors"), - ) + .arg(arg_files) + .arg(arg_extension) + .arg(arg_short) .get_matches(); let values: Values = matches.values_of("FILES").unwrap(); @@ -46,14 +43,17 @@ fn main() { let extension: Option = matches.value_of("extension").map(str); // Convert &str -> String - let mut printer = Printer { - writer: io::stdout(), + let opts_cli = OptionsCli { + extension, short: matches.is_present("short"), }; - let opts_cli = OptionsCli { extension }; + let mut printer = Printer { + writer: io::stdout(), + options: &opts_cli, + }; - let successful = commentective::run(paths, opts_cli) + let successful = commentective::run(paths, &opts_cli) .into_iter() .map(|result| printer.terminal(result)) .filter(|result| result.is_err()) diff --git a/src/lib.rs b/src/lib.rs index a3600f8..e11de62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,11 +28,13 @@ pub mod language; pub mod printer; pub mod utils; +#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct OptionsCli { pub extension: Option, + pub short: bool, } -pub fn run(paths: Vec<&Path>, opts: OptionsCli) -> Vec> { +pub fn run(paths: Vec<&Path>, opts: &OptionsCli) -> Vec> { paths .into_iter() .map(|path| resolve_type_and_run(path, &opts)) diff --git a/src/printer.rs b/src/printer.rs index 26f6e2a..1716dd9 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -7,6 +7,7 @@ use language::FindResult; use printer::Color::*; use std::io::Error; use std::io::Write; +use OptionsCli; enum Color { Green, @@ -15,12 +16,12 @@ enum Color { Yellow, } -pub struct Printer { +pub struct Printer<'a, W> { pub writer: W, - pub short: bool, + pub options: &'a OptionsCli, } -impl Printer +impl<'a, W> Printer<'a, W> where W: Write, { @@ -32,7 +33,7 @@ where } match result.lines.len() { 0 => { - if !self.short { + if !self.options.short { self.print_file_name(&result.file_name); self.print_colored(String::from("> No comments found"), White); } @@ -52,11 +53,11 @@ where } fn error(&mut self, err: &Error) { - if !self.short { + if !self.options.short { self.print_colored_line(Red); } let msg = format!("Error: {}", err.to_string()); - if !self.short { + if !self.options.short { self.print_colored(msg, Red); self.print_colored_line(Red); } @@ -64,7 +65,7 @@ where fn result(&mut self, result: FindResult) { let file_name = result.file_name; - if !self.short { + if !self.options.short { self.print_file_name(&file_name); } result @@ -82,7 +83,7 @@ where } fn print_comments(&mut self, line_number: u32, file_name: &String) { - if !self.short { + if !self.options.short { let msg = format!("L{}", line_number.to_string()); self.print_colored(msg, Green); } else { diff --git a/tests/lib.rs b/tests/lib.rs index e4f3f5a..551955e 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -426,8 +426,8 @@ mod flags { let path_python = Path::new("tests/resources/python/with-comments.py"); let paths = vec![path_javascript, path_python]; - let opts_no_ext = OptionsCli { extension: None }; - for (i, result) in commentective::run(paths.clone(), opts_no_ext) + let opts_no_ext = OptionsCli { extension: None, short: false }; + for (i, result) in commentective::run(paths.clone(), &opts_no_ext) .iter() .enumerate() { @@ -450,8 +450,9 @@ mod flags { let opts_with_ext = OptionsCli { extension: Some(str("js")), + short: false, }; - for (i, result) in commentective::run(paths, opts_with_ext).iter().enumerate() { + for (i, result) in commentective::run(paths, &opts_with_ext).iter().enumerate() { assert!(result.is_ok()); if i == 0 { @@ -489,14 +490,14 @@ mod utils { #[test] fn resolve_type_with_value() { let path = Path::new(EXISTING_FILE); - let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None }); + let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None, short: false }); assert!(result.is_ok()); } #[test] fn resolve_type_with_err() { let path = Path::new(UNSUPPORTED_FILE); - let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None }); + let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None, short: false }); assert!(result.is_err()); }