diff options
author | Daniel <kingdread@gmx.de> | 2020-04-25 12:49:27 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-04-25 12:49:27 +0200 |
commit | 675d0aadf1495a7ac752789c125e124db5152b43 (patch) | |
tree | 76913372f4753863ea3e95945a7833ec7a5ff541 /src/main.rs | |
parent | fe88f503676091c53d31db99ca4af36fe08dcdc8 (diff) | |
download | raidgrep-675d0aadf1495a7ac752789c125e124db5152b43.tar.gz raidgrep-675d0aadf1495a7ac752789c125e124db5152b43.tar.bz2 raidgrep-675d0aadf1495a7ac752789c125e124db5152b43.zip |
better CLI args/parser integration
First, this commit adds a shortcut if only a single argument is given
that can be parsed as a regex. This is to retain the old behaviour of
"raidgrep NAME" just working, without needing to specify -player or
anything.
Secondly, this also re-wraps arguments with spaces (unless there's only
one argument in total). This means that the following should work:
raidgrep -- -player "Godric Gobbledygook"
instead of either
raidgrep -- '-player "Godric Gobbledygook"'
raidgrep -- -player '"Godric Gobbledygook"'
(notice the extra quotes).
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index c89614b..e18a109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,10 @@ use std::str::FromStr; use anyhow::{anyhow, Error, Result}; use chrono::{NaiveDateTime, Weekday}; use colored::Colorize; +use itertools::Itertools; use log::debug; use num_traits::cast::FromPrimitive; +use regex::Regex; use rustyline::Editor; use structopt::StructOpt; use walkdir::{DirEntry, WalkDir}; @@ -242,9 +244,7 @@ fn run() -> Result<()> { } if !opt.repl { - let expr_string = opt.expression.join(" "); - let filter = build_filter(&expr_string)?; - grep(&opt, &*filter)?; + single(&opt)?; } else { repl(&opt)?; } @@ -256,6 +256,41 @@ fn run() -> Result<()> { Ok(()) } +fn single(opt: &Opt) -> Result<()> { + // As a shortcut, we allow only the regular expression to be given, to retain the behaviour + // before the filter changes. + if opt.expression.len() == 1 { + let line = &opt.expression[0]; + let maybe_filter = build_filter(line); + if let Err(_) = maybe_filter { + let maybe_regex = Regex::new(line); + if let Ok(rgx) = maybe_regex { + let filter = filters::player::any( + filters::player::NameFilter::new(SearchField::Account, rgx.clone()) + | filters::player::NameFilter::new(SearchField::Character, rgx), + ); + return grep(opt, &*filter); + } + } + return grep(opt, &*maybe_filter?); + } + + let expr_string = opt + .expression + .iter() + .map(|part| { + if part.contains(' ') { + format!(r#""{}""#, part) + } else { + part.into() + } + }) + .join(" "); + let filter = build_filter(&expr_string)?; + grep(&opt, &*filter)?; + Ok(()) +} + fn repl(opt: &Opt) -> Result<()> { let mut rl = Editor::<()>::new(); loop { |