Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
glowing-chemist committed Dec 27, 2016
1 parent a43f32c commit f49fb88
Show file tree
Hide file tree
Showing 46 changed files with 51 additions and 15,606 deletions.
1 change: 1 addition & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ impl<'n, 'e> AnyArg<'n, 'e> for App<'n, 'e> {
fn num_vals(&self) -> Option<u64> { None }
fn possible_vals(&self) -> Option<&[&'e str]> { None }
fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> { None }
fn validator_os(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> { None }
fn min_vals(&self) -> Option<u64> { None }
fn short(&self) -> Option<char> { None }
fn long(&self) -> Option<&'e str> { None }
Expand Down
5 changes: 5 additions & 0 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,11 @@ impl<'a, 'b> Parser<'a, 'b>
return Err(Error::value_validation(Some(arg), e, self.color()));
}
}
if let Some(vtor) = arg.validator_os() {
if let Err(e) = vtor(val.to_string_lossy().into_owned()) {
return Err(Error::value_validation(Some(arg), e, self.color()));
}
}
if matcher.needs_more_vals(arg) {
return Ok(Some(arg.name()));
}
Expand Down
1 change: 1 addition & 0 deletions src/args/any_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait AnyArg<'n, 'e>: std_fmt::Display {
fn num_vals(&self) -> Option<u64>;
fn possible_vals(&self) -> Option<&[&'e str]>;
fn validator(&self) -> Option<&Rc<Fn(String) -> Result<(), String>>>;
fn validator_os(&self) -> Option<&Rc<Fn(String) -> Result<(), String>>>;
fn short(&self) -> Option<char>;
fn long(&self) -> Option<&'e str>;
fn val_delim(&self) -> Option<char>;
Expand Down
34 changes: 34 additions & 0 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct Arg<'a, 'b>
#[doc(hidden)]
pub validator: Option<Rc<Fn(String) -> Result<(), String>>>,
#[doc(hidden)]
pub validator_os: Option<Rc<Fn(String) -> Result<(), String>>>,
#[doc(hidden)]
pub overrides: Option<Vec<&'a str>>,
#[doc(hidden)]
pub settings: ArgFlags,
Expand Down Expand Up @@ -97,6 +99,7 @@ impl<'a, 'b> Default for Arg<'a, 'b> {
max_vals: None,
min_vals: None,
validator: None,
validator_os: None,
overrides: None,
settings: ArgFlags::new(),
val_delim: None,
Expand Down Expand Up @@ -1908,6 +1911,35 @@ impl<'a, 'b> Arg<'a, 'b> {
self
}

///Works identically to Validator but is intended to be used with non UTF-8 formatted strings.
/// # Examples
/// ```rust
/// # use clap::{App, Arg};
///fn has_ampersand(v: String) -> Result<(), String> {
/// if v.contains("&") { return Ok(()); }
/// Err(String::from("The value did not contain the required & sigil"))
/// }
/// let res = App::new("validators")
/// .arg(Arg::with_name("file")
/// .index(1)
/// .validator(has_ampersand))
/// .get_matches_from_safe(vec![
/// "validators", "Fish & chips"
/// ]);
/// assert!(res.is_ok());
/// assert_eq!(res.unwrap().value_of("file"), Some("Fish & chips"));
/// ```
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
/// [`Err(String)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err
/// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
pub fn validator_os<F>(mut self, f: F) -> Self
where F: Fn(String) -> Result<(), String> + 'static
{
self.validator = Some(Rc::new(f));
self
}

/// Specifies the *maximum* number of values are for this argument. For example, if you had a
/// `-f <file>` argument where you wanted up to 3 'files' you would set `.max_values(3)`, and
/// this argument would be satisfied if the user provided, 1, 2, or 3 values.
Expand Down Expand Up @@ -2563,6 +2595,7 @@ impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> {
val_names: a.val_names.clone(),
groups: a.groups.clone(),
validator: a.validator.clone(),
validator_os: a.validator_os.clone(),
overrides: a.overrides.clone(),
settings: a.settings,
val_delim: a.val_delim,
Expand Down Expand Up @@ -2591,6 +2624,7 @@ impl<'a, 'b> Clone for Arg<'a, 'b> {
val_names: self.val_names.clone(),
groups: self.groups.clone(),
validator: self.validator.clone(),
validator_os: self.validator_os.clone(),
overrides: self.overrides.clone(),
settings: self.settings,
val_delim: self.val_delim,
Expand Down
1 change: 1 addition & 0 deletions src/args/arg_builder/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl<'n, 'e> AnyArg<'n, 'e> for FlagBuilder<'n, 'e> {
fn num_vals(&self) -> Option<u64> { None }
fn possible_vals(&self) -> Option<&[&'e str]> { None }
fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> { None }
fn validator_os(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> { None }
fn min_vals(&self) -> Option<u64> { None }
fn short(&self) -> Option<char> { self.s.short }
fn long(&self) -> Option<&'e str> { self.s.long }
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ impl<'n, 'e> AnyArg<'n, 'e> for OptBuilder<'n, 'e> {
fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
self.v.validator.as_ref()
}
fn validator_os(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
self.v.validator_os.as_ref()
}
fn min_vals(&self) -> Option<u64> { self.v.min_vals }
fn short(&self) -> Option<char> { self.s.short }
fn long(&self) -> Option<&'e str> { self.s.long }
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ impl<'n, 'e> AnyArg<'n, 'e> for PosBuilder<'n, 'e> {
fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
self.v.validator.as_ref()
}
fn validator_os(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
self.v.validator_os.as_ref()
}
fn min_vals(&self) -> Option<u64> { self.v.min_vals }
fn short(&self) -> Option<char> { None }
fn long(&self) -> Option<&'e str> { None }
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/valued.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Valued<'a, 'b>
pub max_vals: Option<u64>,
pub min_vals: Option<u64>,
pub validator: Option<Rc<Fn(String) -> Result<(), String>>>,
pub validator_os: Option<Rc<Fn(String) -> Result<(), String>>>,
pub val_delim: Option<char>,
pub default_val: Option<&'a str>,
}
Expand All @@ -28,6 +29,7 @@ impl<'n, 'e> Default for Valued<'n, 'e> {
max_vals: None,
val_names: None,
validator: None,
validator_os: None,
val_delim: Some(','),
default_val: None,
}
Expand All @@ -43,6 +45,7 @@ impl<'n, 'e, 'z> From<&'z Arg<'n, 'e>> for Valued<'n, 'e> {
max_vals: a.max_vals,
val_names: a.val_names.clone(),
validator: a.validator.clone(),
validator_os: a.validator_os.clone(),
val_delim: a.val_delim,
default_val: a.default_val,
};
Expand Down
Loading

0 comments on commit f49fb88

Please sign in to comment.