Merge pull request #771 from matthiasbeyer/clap-validators

Clap validators
This commit is contained in:
Matthias Beyer 2016-10-07 19:45:49 +02:00 committed by GitHub
commit 30875d153f
9 changed files with 61 additions and 0 deletions

View file

@ -2,6 +2,8 @@
The Tagging module.
A valid tag matches the regex `[a-zA-Z][0-9a-zA-Z]*`.
### Description
<!-- Description of the module -->

View file

@ -20,6 +20,7 @@
use clap::{Arg, App, SubCommand};
use libimagentrytag::ui::tag_add_arg;
use libimagutil::cli_validators::*;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
@ -41,6 +42,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(true)
.multiple(true)
.value_name("URL")
.validator(is_url)
.help("Add this URL, multiple possible"))
.arg(tag_add_arg())
)
@ -63,6 +65,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(true)
.multiple(true)
.value_name("URL")
.validator(is_url)
.help("Remove these urls, regex supported"))
)

View file

@ -27,3 +27,6 @@ path = "../libimaginteraction"
[dependencies.libimagentrylist]
path = "../libimagentrylist"
[dependencies.libimagutil]
path = "../libimagutil"

View file

@ -28,6 +28,7 @@ extern crate libimagref;
extern crate libimagerror;
extern crate libimagentrylist;
extern crate libimaginteraction;
extern crate libimagutil;
mod ui;
use ui::build_ui;

View file

@ -19,6 +19,8 @@
use clap::{Arg, App, SubCommand};
use libimagutil::cli_validators::is_existing_path;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
.subcommand(SubCommand::with_name("add")
@ -30,6 +32,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.takes_value(true)
.required(true)
.help("The path of the file")
.validator(is_existing_path)
.value_name("PATH"))
.arg(Arg::with_name("track-content")
.long("content-hash")

View file

@ -21,6 +21,8 @@ use clap::{Arg, ArgMatches, App, SubCommand};
use tag::Tag;
use libimagutil::cli_validators::is_tag;
/// Generates a `clap::SubCommand` to be integrated in the commandline-ui builder for building a
/// "tags --add foo --remove bar" subcommand to do tagging action.
pub fn tag_subcommand<'a, 'b>() -> App<'a, 'b> {
@ -39,6 +41,7 @@ pub fn tag_add_arg<'a, 'b>() -> Arg<'a, 'b> {
.takes_value(true)
.value_name("tags")
.multiple(true)
.validator(is_tag)
.help("Add tags, seperated by comma or by specifying multiple times")
}
@ -49,6 +52,7 @@ pub fn tag_remove_arg<'a, 'b>() -> Arg<'a, 'b> {
.takes_value(true)
.value_name("tags")
.multiple(true)
.validator(is_tag)
.help("Remove tags, seperated by comma or by specifying multiple times")
}
@ -76,6 +80,7 @@ pub fn tag_argument<'a, 'b>() -> Arg<'a, 'b> {
.long("tags")
.takes_value(true)
.multiple(true)
.validator(is_tag)
.help("Add or remove tags, prefixed by '+' (for adding) or '-' (for removing)")
}

View file

@ -4,6 +4,8 @@ version = "0.2.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
url = "1.1"
boolinator = "2.4.0"
lazy_static = "0.1.15"
log = "0.3"
regex = "0.1"

View file

@ -0,0 +1,39 @@
//! Functions to be used for clap::Arg::validator()
//! to validate arguments
use std::path::PathBuf;
use boolinator::Boolinator;
pub fn is_existing_path(s: String) -> Result<(), String> {
PathBuf::from(s.clone()).exists().as_result((), format!("Not a File or Directory: {}", s))
}
pub fn is_file(s: String) -> Result<(), String> {
PathBuf::from(s.clone()).is_file().as_result((), format!("Not a File: {}", s))
}
pub fn is_directory(s: String) -> Result<(), String> {
PathBuf::from(s.clone()).is_dir().as_result((), format!("Not a Directory: {}", s))
}
pub fn is_integer(s: String) -> Result<(), String> {
use std::str::FromStr;
let i : Result<i64, _> = FromStr::from_str(&s);
i.map(|_| ()).map_err(|_| format!("Not an integer: {}", s))
}
pub fn is_url(s: String) -> Result<(), String> {
use url::Url;
Url::parse(&s).map(|_| ()).map_err(|_| format!("Not a URL: {}", s))
}
pub fn is_tag(s: String) -> Result<(), String> {
use regex::Regex;
lazy_static! { static ref TAG_RE : Regex = Regex::new("[:alpha:][:word:]*").unwrap(); }
TAG_RE
.is_match(&s)
.as_result((), format!("Not a valid Tag: '{}' - Valid is [a-zA-Z][0-9a-zA-Z]*", s))
}

View file

@ -35,9 +35,12 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
extern crate regex;
extern crate url;
extern crate boolinator;
extern crate tempfile;
#[macro_use] mod log_result;
pub mod cli_validators;
pub mod debug_result;
pub mod edit;
pub mod info_result;