Skip to content

Commit

Permalink
fix(RequiredArgs): required by default args should no longer be requi…
Browse files Browse the repository at this point in the history
…red when their exclusions are present
  • Loading branch information
kbknapp committed Apr 27, 2015
1 parent 09eb4d9 commit 4bb4c3c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,13 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self.validate_blacklist(&matches);

if !self.required.is_empty() {
println!("reqs: {:?}", self.required);
println!("bls: {:?}", self.blacklist);
println!("grps: {:?}", self.groups);
self.report_error("One or more required arguments were not supplied".to_owned(),
true, true);
// println!("reqs: {:?}", self.required);
// println!("bls: {:?}", self.blacklist);
// println!("grps: {:?}", self.groups);
if self.validate_required(&matches) {
self.report_error("One or more required arguments were not supplied".to_owned(),
true, true);
}
}

matches.usage = Some(self.create_usage());
Expand Down Expand Up @@ -1504,4 +1506,33 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
}

fn validate_required(&self, matches: &ArgMatches<'ar, 'ar>) -> bool{
for name in self.required.iter() {
validate_reqs!(self, flags, matches, name);

validate_reqs!(self, opts, matches, name);

// because positions use different keys, we dont use the macro
match self.positionals_idx.values().filter(|ref p| &p.name == name).next() {
Some(p) =>{
if let Some(ref bl) = p.blacklist {
for n in bl.iter() {
if matches.args.contains_key(n) {
return false
} else if self.groups.contains_key(n) {
let grp = self.groups.get(n).unwrap();
for an in grp.args.iter() {
if matches.args.contains_key(an) {
return false
}
}
}
}
}
},
None =>(),
}
}
true
}
}
22 changes: 22 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ macro_rules! parse_group_reqs {
};
}

// De-duplication macro used in src/app.rs
macro_rules! validate_reqs {
($me:ident, $t:ident, $m:ident, $n:ident) => {
if let Some(a) = $me.$t.get($n) {
if let Some(ref bl) = a.blacklist {
for n in bl.iter() {
if $m.args.contains_key(n) {
return false
} else if $me.groups.contains_key(n) {
let grp = $me.groups.get(n).unwrap();
for an in grp.args.iter() {
if $m.args.contains_key(an) {
return false
}
}
}
}
}
}
};
}

// Thanks to bluss and flan3002 in #rust IRC
//
// Helps with rightward drift when iterating over something and matching each item.
Expand Down

0 comments on commit 4bb4c3c

Please sign in to comment.