Skip to content

Commit 3ca0947

Browse files
committed
fix(Usage Strings): now properly dedups args that are also in groups
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
1 parent f574fb8 commit 3ca0947

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/app/macros.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,19 @@ macro_rules! _handle_group_reqs{
7777
debugln!("macro=_handle_group_reqs!;");
7878
for grp in $me.groups.values() {
7979
let mut found = false;
80-
for name in &grp.args {
81-
if name == &$arg.name() {
82-
vec_remove!($me.required, name);
83-
if let Some(ref reqs) = grp.requires {
84-
$me.required.extend(reqs);
85-
}
86-
if let Some(ref bl) = grp.conflicts {
87-
$me.blacklist.extend(bl);
88-
}
89-
found = true; // What if arg is in more than one group with different reqs?
90-
break;
80+
if grp.args.contains(&$arg.name()) {
81+
vec_remove!($me.required, &$arg.name());
82+
if let Some(ref reqs) = grp.requires {
83+
$me.required.extend(reqs);
9184
}
85+
if let Some(ref bl) = grp.conflicts {
86+
$me.blacklist.extend(bl);
87+
}
88+
found = true; // What if arg is in more than one group with different reqs?
9289
}
9390
if found {
9491
vec_remove_all!($me.required, &grp.args);
92+
debugln!("Adding args from group to blacklist...{:?}", grp.args);
9593
$me.blacklist.extend(&grp.args);
9694
vec_remove!($me.blacklist, &$arg.name());
9795
}

src/app/parser.rs

+16
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,33 @@ impl<'a, 'b> Parser<'a, 'b>
299299
c_pos.dedup();
300300
c_flags.dedup();
301301
c_opt.dedup();
302+
grps.dedup();
303+
let mut args_in_groups = vec![];
304+
for g in grps.iter() {
305+
for a in self.arg_names_in_group(g).into_iter() {
306+
args_in_groups.push(a);
307+
}
308+
}
302309

303310
let mut pmap = BTreeMap::new();
304311
for p in c_pos.into_iter() {
305312
if matcher.is_some() && matcher.as_ref().unwrap().contains(p) {
306313
continue;
307314
}
308315
if let Some(p) = self.positionals.values().filter(|x| &x.name == &p).next() {
316+
if args_in_groups.contains(&p.name) {
317+
continue;
318+
}
309319
pmap.insert(p.index, p.to_string());
310320
}
311321
}
322+
debugln!("args_in_groups={:?}", args_in_groups);
312323
for (_, s) in pmap {
324+
if !args_in_groups.is_empty() {
325+
if args_in_groups.contains(&&*s) {
326+
continue;
327+
}
328+
}
313329
ret_val.push_back(s);
314330
}
315331
macro_rules! write_arg {

src/args/arg_builder/positional.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,16 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
112112

113113
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
114114
fn fmt(&self, f: &mut Formatter) -> Result {
115-
// if self.settings.is_set(ArgSettings::Required) {
116-
if let Some(ref names) = self.val_names {
117-
try!(write!(f,
118-
"{}",
119-
names.values()
120-
.map(|n| format!("<{}>", n))
121-
.collect::<Vec<_>>()
122-
.join(" ")));
123-
} else {
124-
try!(write!(f, "<{}>", self.name));
125-
}
126-
// } else {
127-
// if let Some(ref names) = self.val_names {
128-
// try!(write!(f,
129-
// "{}",
130-
// names.values()
131-
// .map(|n| format!("[{}]", n))
132-
// .collect::<Vec<_>>()
133-
// .join(" ")));
134-
// } else {
135-
// try!(write!(f, "[{}]", self.name));
136-
// }
137-
// }
115+
if let Some(ref names) = self.val_names {
116+
try!(write!(f,
117+
"{}",
118+
names.values()
119+
.map(|n| format!("<{}>", n))
120+
.collect::<Vec<_>>()
121+
.join(" ")));
122+
} else {
123+
try!(write!(f, "<{}>", self.name));
124+
}
138125
if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
139126
try!(write!(f, "..."));
140127
}

0 commit comments

Comments
 (0)