Skip to content

Commit

Permalink
wip: implementing issue 170
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Aug 14, 2015
1 parent 23aca97 commit 88ae711
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ pub struct Arg<'n, 'l, 'h, 'g, 'p, 'r> {
#[doc(hidden)]
pub empty_vals: bool,
#[doc(hidden)]
pub global: bool
pub global: bool,
#[doc(hidden)]
pub validator: Option<Box<FnOnce(String) -> Result<(), String>>>
}

impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
Expand Down Expand Up @@ -131,7 +133,8 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
val_names: None,
group: None,
global: false,
empty_vals: true
empty_vals: true,
validator: None
}
}

Expand Down Expand Up @@ -289,7 +292,8 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
min_vals: None,
group: None,
global: false,
empty_vals: true
empty_vals: true,
validator: None,
}
}

Expand Down Expand Up @@ -663,6 +667,21 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
self
}

///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// # let matches = App::new("myprog")
/// # .arg(
/// # Arg::with_name("debug").index(1)
/// .number_of_values(3)
/// # ).get_matches();
pub fn validator<F>(mut self, f: F) -> Self where F: FnOnce(String) -> Result<(), String> + 'static {
self.validator = Some(Box::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
Expand Down Expand Up @@ -760,27 +779,29 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
}
}

impl<'n, 'l, 'h, 'g, 'p, 'r, 'z> From<&'z Arg<'n, 'l, 'h, 'g, 'p, 'r>> for Arg<'n, 'l, 'h, 'g, 'p, 'r> {
fn from(a: &'z Arg<'n, 'l, 'h, 'g, 'p, 'r>) -> Self {
Arg {
name: a.name,
short: a.short,
long: a.long,
help: a.help,
required: a.required,
takes_value: a.takes_value,
multiple: a.multiple,
index: a.index,
possible_vals: a.possible_vals.clone(),
blacklist: a.blacklist.clone(),
requires: a.requires.clone(),
num_vals: a.num_vals,
min_vals: a.min_vals,
max_vals: a.max_vals,
val_names: a.val_names.clone(),
group: a.group,
global: a.global,
empty_vals: a.empty_vals
}
}
}
// impl<'n, 'l, 'h, 'g, 'p, 'r, 'z> From<&'z Arg<'n, 'l, 'h, 'g, 'p, 'r>> for Arg<'n, 'l, 'h, 'g, 'p, 'r> {
// fn from(a: &'z Arg<'n, 'l, 'h, 'g, 'p, 'r>) -> Self {
// let f = mem::replace(dest, mut src)
// Arg {
// name: a.name,
// short: a.short,
// long: a.long,
// help: a.help,
// required: a.required,
// takes_value: a.takes_value,
// multiple: a.multiple,
// index: a.index,
// possible_vals: a.possible_vals.clone(),
// blacklist: a.blacklist.clone(),
// requires: a.requires.clone(),
// num_vals: a.num_vals,
// min_vals: a.min_vals,
// max_vals: a.max_vals,
// val_names: a.val_names.clone(),
// group: a.group,
// global: a.global,
// empty_vals: a.empty_vals,
// validator: a.validator
// }
// }
// }

0 comments on commit 88ae711

Please sign in to comment.