Skip to content

Commit

Permalink
fix(Usage Strings): now properly dedups args that are also in groups
Browse files Browse the repository at this point in the history
For example, if an arg is part of a required group, it will only appear
in the group usage string, and not in both the group as well as the arg
by itself.

Imagine a group containing two args, `arg1` and `--arg2`

OLD:
    `myprog <arg1> <arg1|--arg2>`

NEW:
    `myprog <arg1|--arg2>`

Closes #498
  • Loading branch information
kbknapp committed May 9, 2016
1 parent f574fb8 commit 3ca0947
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
20 changes: 9 additions & 11 deletions src/app/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,19 @@ macro_rules! _handle_group_reqs{
debugln!("macro=_handle_group_reqs!;");
for grp in $me.groups.values() {
let mut found = false;
for name in &grp.args {
if name == &$arg.name() {
vec_remove!($me.required, name);
if let Some(ref reqs) = grp.requires {
$me.required.extend(reqs);
}
if let Some(ref bl) = grp.conflicts {
$me.blacklist.extend(bl);
}
found = true; // What if arg is in more than one group with different reqs?
break;
if grp.args.contains(&$arg.name()) {
vec_remove!($me.required, &$arg.name());
if let Some(ref reqs) = grp.requires {
$me.required.extend(reqs);
}
if let Some(ref bl) = grp.conflicts {
$me.blacklist.extend(bl);
}
found = true; // What if arg is in more than one group with different reqs?
}
if found {
vec_remove_all!($me.required, &grp.args);
debugln!("Adding args from group to blacklist...{:?}", grp.args);
$me.blacklist.extend(&grp.args);
vec_remove!($me.blacklist, &$arg.name());
}
Expand Down
16 changes: 16 additions & 0 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,33 @@ impl<'a, 'b> Parser<'a, 'b>
c_pos.dedup();
c_flags.dedup();
c_opt.dedup();
grps.dedup();
let mut args_in_groups = vec![];
for g in grps.iter() {
for a in self.arg_names_in_group(g).into_iter() {
args_in_groups.push(a);
}
}

let mut pmap = BTreeMap::new();
for p in c_pos.into_iter() {
if matcher.is_some() && matcher.as_ref().unwrap().contains(p) {
continue;
}
if let Some(p) = self.positionals.values().filter(|x| &x.name == &p).next() {
if args_in_groups.contains(&p.name) {
continue;
}
pmap.insert(p.index, p.to_string());
}
}
debugln!("args_in_groups={:?}", args_in_groups);
for (_, s) in pmap {
if !args_in_groups.is_empty() {
if args_in_groups.contains(&&*s) {
continue;
}
}
ret_val.push_back(s);
}
macro_rules! write_arg {
Expand Down
33 changes: 10 additions & 23 deletions src/args/arg_builder/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,16 @@ impl<'n, 'e> PosBuilder<'n, 'e> {

impl<'n, 'e> Display for PosBuilder<'n, 'e> {
fn fmt(&self, f: &mut Formatter) -> Result {
// if self.settings.is_set(ArgSettings::Required) {
if let Some(ref names) = self.val_names {
try!(write!(f,
"{}",
names.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" ")));
} else {
try!(write!(f, "<{}>", self.name));
}
// } else {
// if let Some(ref names) = self.val_names {
// try!(write!(f,
// "{}",
// names.values()
// .map(|n| format!("[{}]", n))
// .collect::<Vec<_>>()
// .join(" ")));
// } else {
// try!(write!(f, "[{}]", self.name));
// }
// }
if let Some(ref names) = self.val_names {
try!(write!(f,
"{}",
names.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" ")));
} else {
try!(write!(f, "<{}>", self.name));
}
if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
try!(write!(f, "..."));
}
Expand Down

0 comments on commit 3ca0947

Please sign in to comment.