refactor(usage): Clarify required gathering

This commit is contained in:
Ed Page 2022-07-29 19:23:29 -05:00
parent edc7ac359f
commit d001952ac4

View file

@ -386,6 +386,7 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
.flat_map(|g| self.cmd.unroll_args_in_group(&g.id)) .flat_map(|g| self.cmd.unroll_args_in_group(&g.id))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut required_opts = IndexSet::new();
for a in unrolled_reqs for a in unrolled_reqs
.iter() .iter()
.chain(incls.iter()) .chain(incls.iter())
@ -402,9 +403,11 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
{ {
debug!("Usage::get_required_usage_from:iter:{:?}", a); debug!("Usage::get_required_usage_from:iter:{:?}", a);
let arg = self.cmd.find(a).expect(INTERNAL_ERROR_MSG).to_string(); let arg = self.cmd.find(a).expect(INTERNAL_ERROR_MSG).to_string();
ret_val.insert(arg); required_opts.insert(arg);
} }
let mut g_vec: Vec<String> = vec![]; ret_val.extend(required_opts);
let mut required_groups = IndexSet::new();
for g in unrolled_reqs for g in unrolled_reqs
.iter() .iter()
.filter(|n| self.cmd.get_groups().any(|g| g.id == **n)) .filter(|n| self.cmd.get_groups().any(|g| g.id == **n))
@ -422,13 +425,11 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
} }
let elem = self.cmd.format_group(g); let elem = self.cmd.format_group(g);
if !g_vec.contains(&elem) { required_groups.insert(elem);
g_vec.push(elem);
}
} }
ret_val.extend(g_vec); ret_val.extend(required_groups);
let mut pvec = unrolled_reqs let mut required_positionals = unrolled_reqs
.iter() .iter()
.chain(incls.iter()) .chain(incls.iter())
.filter(|a| self.cmd.get_positionals().any(|p| &&p.id == a)) .filter(|a| self.cmd.get_positionals().any(|p| &&p.id == a))
@ -440,13 +441,10 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
.filter(|pos| !args_in_groups.contains(&pos.id)) .filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos)) .map(|pos| (pos.index.unwrap(), pos))
.collect::<Vec<(usize, &Arg)>>(); .collect::<Vec<(usize, &Arg)>>();
pvec.sort_by_key(|(ind, _)| *ind); // sort by index required_positionals.sort_by_key(|(ind, _)| *ind); // sort by index
for (_, p) in required_positionals {
for (_, p) in pvec {
debug!("Usage::get_required_usage_from:push:{:?}", p.id); debug!("Usage::get_required_usage_from:push:{:?}", p.id);
if !args_in_groups.contains(&p.id) { ret_val.insert(p.to_string());
ret_val.insert(p.to_string());
}
} }
debug!("Usage::get_required_usage_from: ret_val={:?}", ret_val); debug!("Usage::get_required_usage_from: ret_val={:?}", ret_val);