Remove clap validator + add max-filesize integration tests

This commit is contained in:
Marc Tiehuis
2017-03-01 18:38:06 +13:00
committed by Andrew Gallant
parent 71585f6d47
commit adff43fbb4
5 changed files with 60 additions and 34 deletions

View File

@@ -1,7 +1,6 @@
use std::collections::HashMap;
use clap::{App, AppSettings, Arg, ArgSettings};
use regex::Regex;
const ABOUT: &'static str = "
ripgrep (rg) recursively searches your current directory for a regex pattern.
@@ -147,8 +146,7 @@ fn app<F>(next_line_help: bool, doc: F) -> App<'static, 'static>
.short("m").value_name("NUM").takes_value(true)
.validator(validate_number))
.arg(flag("max-filesize")
.value_name("NUM+SUFFIX?").takes_value(true)
.validator(validate_max_filesize))
.value_name("NUM+SUFFIX?").takes_value(true))
.arg(flag("maxdepth")
.value_name("NUM").takes_value(true)
.validator(validate_number))
@@ -502,24 +500,3 @@ lazy_static! {
fn validate_number(s: String) -> Result<(), String> {
s.parse::<usize>().map(|_|()).map_err(|err| err.to_string())
}
fn validate_max_filesize(s: String) -> Result<(), String> {
let re = Regex::new(r#"^(\d+)([KMG])?$"#).unwrap();
let caps = try!(re.captures(&s)
.ok_or("invalid format for max-filesize argument"));
let value = caps.get(1);
let suffix = caps.get(2).map(|x| x.as_str());
match value {
Some(value) => {
try!(value.as_str().parse::<u64>().map_err(|err| err.to_string()));
}
None => ()
}
match suffix {
None | Some("K") | Some("M") | Some("G") => Ok(()),
_ => Err(From::from("invalid suffix for max-filesize argument"))
}
}