Skip to content

Commit

Permalink
tests(v2): fixing more tests on the new v2 base
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Jan 25, 2016
1 parent 94ed137 commit 1fbe26c
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 227 deletions.
5 changes: 4 additions & 1 deletion examples/01c_quick_example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate clap;

#[cfg(feature = "nightly")]
#[cfg(feature = "unstable")]
fn main() {
// This example shows how to create an application with several arguments using macro builder.
// It combines the simplicity of the from_usage methods and the performance of the Builder Pattern.
Expand Down Expand Up @@ -74,3 +74,6 @@ fn main() {

// more program logic goes here...
}

#[cfg(not(feature = "unstable"))]
fn main() {}
4 changes: 2 additions & 2 deletions examples/12_typed_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ fn main() {
//
// Using other methods such as unwrap_or_else(|e| println!("{}",e))
// are possible too.
let len = value_t!(matches.value_of("len"), u32).unwrap_or(10);
let len = value_t!(matches, "len", u32).unwrap_or(10);

println!("len ({}) + 2 = {}", len, len + 2);

// This code loops through all the values provided to "seq" and adds 2
// If seq fails to parse, the program exits, you don't have an option
for v in value_t_or_exit!(matches.values_of("seq"), u32) {
for v in values_t!(matches, "seq", u32).unwrap_or_else(|e| e.exit()) {
println!("Sequence part {} + 2: {}", v, v + 2);
}
}
2 changes: 1 addition & 1 deletion examples/13b_enum_values_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
.possible_values(&enum_vals))
.get_matches();

let t = value_t!(m.value_of("type"), Vals).or_else(|e| e.exit());
let t = value_t!(m, "type", Vals).unwrap_or_else(|e| e.exit());

// Now we can use our enum like normal.
match t {
Expand Down
6 changes: 4 additions & 2 deletions examples/18_builder_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate clap;
// Note, there isn't a need for "use clap::{ ... };" Because the clap_app! macro uses
// $crate:: internally

#[cfg(feature = "nightly")]
#[cfg(feature = "unstable")]
fn main() {

// Validation example testing that a file exists
Expand Down Expand Up @@ -80,6 +80,8 @@ fn main() {
}
}


// Continued program logic goes here...
}

#[cfg(not(feature = "unstable"))]
fn main() {}
34 changes: 27 additions & 7 deletions src/app/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,51 @@ macro_rules! arg_post_processing(
($me:ident, $arg:ident, $matcher:ident) => ({
use args::AnyArg;
// Handle POSIX overrides
debug!("Is '{}' in overrides...", $arg.to_string());
if $me.overrides.contains(&$arg.name()) {
if let Some(ref name) = $me.overriden_from(&*$arg.name(), $matcher) {
if let Some(ref name) = $me.overriden_from($arg.name(), $matcher) {
sdebugln!("Yes by {}", name);
$matcher.remove(name);
remove_overriden!($me, name);
}
}
} else { sdebugln!("No"); }

// Add overrides
debug!("Does '{}' have overrides...", $arg.to_string());
if let Some(or) = $arg.overrides() {
for pa in or {
sdebugln!("\tYes '{}'", pa);
$matcher.remove(&*pa);
remove_overriden!($me, pa);
$me.overrides.push(pa);
vec_remove!($me.required, pa);
}
}
} else { sdebugln!("No"); }

// Handle conflicts
debugln!("Does '{}' have conflicts...", $arg.to_string());
if let Some(bl) = $arg.blacklist() {
for name in bl {
sdebugln!("\tYes '{}'", name);
$me.blacklist.push(name);
vec_remove!($me.overrides, name);
vec_remove!($me.required, name);
}
}
} else { sdebugln!("No"); }

// Add all required args which aren't already found in matcher to the master
// list
debug!("Does '{}' have requirements...", $arg.to_string());
if let Some(reqs) = $arg.requires() {
for n in reqs {
if $matcher.contains(&*n) {
if $matcher.contains(&n) {
sdebugln!("\tYes '{}' but it's already met", n);
continue;
}
} else { sdebugln!("\tYes '{}'", n); }

$me.required.push(n);
}
}
} else { sdebugln!("No"); }

_handle_group_reqs!($me, $arg);
})
Expand Down Expand Up @@ -128,3 +139,12 @@ macro_rules! _handle_group_reqs{
}
})
}

macro_rules! validate_multiples {
($_self:ident, $a:ident, $m:ident) => {
if $m.contains(&$a.name) && !$a.settings.is_set(ArgSettings::Multiple) {
// Not the first time, and we don't allow multiples
return Err(Error::unexpected_multiple_usage($a, &*$_self.create_current_usage($m)))
}
};
}
4 changes: 2 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ impl<'a, 'b> App<'a, 'b> {
/// # use clap::{App, Arg};
/// App::new("myprog")
/// .args(
/// vec![Arg::from_usage("[debug] -d 'turns on debugging info"),
/// Arg::with_name("input").index(1).help("the input file to use")]
/// &[Arg::from_usage("[debug] -d 'turns on debugging info"),
/// Arg::with_name("input").index(1).help("the input file to use")]
/// )
/// # ;
/// ```
Expand Down
Loading

0 comments on commit 1fbe26c

Please sign in to comment.