From 16f80cbc41326fb407e735410cdd5d9396a72163 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Mon, 8 Feb 2021 13:40:10 -0800 Subject: [PATCH] Simplify repl line parsing regex --- src/bdk_cli.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/bdk_cli.rs b/src/bdk_cli.rs index b5bb2c2..6786da2 100644 --- a/src/bdk_cli.rs +++ b/src/bdk_cli.rs @@ -52,7 +52,7 @@ use regex::Regex; /// REPL mode #[derive(Debug, StructOpt, Clone, PartialEq)] -#[structopt(name = "", setting = AppSettings::NoBinaryName, +#[structopt(name = "", setting = AppSettings::NoBinaryName, version = option_env ! ("CARGO_PKG_VERSION").unwrap_or("unknown"), author = option_env ! ("CARGO_PKG_AUTHORS").unwrap_or(""))] pub enum ReplSubCommand { @@ -195,8 +195,7 @@ fn main() { // println!("No previous history."); // } - let split_regex = Regex::new(r#"[\w\-]+|"[\w\s]*""#).unwrap(); - let filter_regex = Regex::new(r#"[\w\s\-]+"#).unwrap(); + let split_regex = Regex::new(r#"([\w\-]+)|"([\w\s]*)""#).unwrap(); loop { let readline = rl.readline(">> "); @@ -206,14 +205,12 @@ fn main() { continue; } rl.add_history_entry(line.as_str()); - let split_line: Vec<&str> = - split_regex.find_iter(&line).map(|m| m.as_str()).collect(); - let filtered_line: Vec<&str> = split_line - .iter() - .flat_map(|s| filter_regex.find_iter(s).map(|m| m.as_str())) + let split_line: Vec<&str> = split_regex + .captures_iter(&line) + .map(|c| c.get(1).or_else(|| c.get(2)).unwrap().as_str()) .collect(); let repl_subcommand: Result = - ReplSubCommand::from_iter_safe(filtered_line); + ReplSubCommand::from_iter_safe(split_line); debug!("repl_subcommand = {:?}", repl_subcommand); if let Err(err) = repl_subcommand { @@ -270,21 +267,15 @@ mod test { #[test] fn test_regex() { - let split_regex = Regex::new(r#"[\w\-]+|"[\w\s]*""#).unwrap(); - //println!("split_regex = {:?}", &split_regex); - let filter_regex = Regex::new(r#"[\w\s\-]+"#).unwrap(); - //println!("filter_regex = {:?}", &filter_regex); + let split_regex = Regex::new(r#"([\w\-]+)|"([\w\s]*)""#).unwrap(); let line = r#"restore -m "word1 word2 word3" -p test"#; - let split_line: Vec<&str> = split_regex.find_iter(&line).map(|m| m.as_str()).collect(); - //println!("split_line({}) = {:?}", &line, &split_line); - let filtered_line: Vec<&str> = split_line - .iter() - .flat_map(|s| filter_regex.find_iter(s).map(|m| m.as_str())) + let split_line: Vec<&str> = split_regex + .captures_iter(&line) + .map(|c| c.get(1).or_else(|| c.get(2)).unwrap().as_str()) .collect(); - //println!("filtered_line({:?}) = {:?}", &split_line, &filtered_line); assert_eq!( vec!("restore", "-m", "word1 word2 word3", "-p", "test"), - filtered_line + split_line ); } }