refactor: Prepare for deferring graph creation

This commit is contained in:
Ed Page 2022-02-14 12:21:02 -06:00
parent 54c2f0df05
commit b994789ee6
2 changed files with 19 additions and 12 deletions

View file

@ -23,6 +23,7 @@ use crate::error::Result as ClapResult;
use crate::mkeymap::MKeyMap;
use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage};
use crate::parse::{ArgMatcher, ArgMatches, Input, Parser};
use crate::util::ChildGraph;
use crate::util::{color::ColorChoice, Id, Key};
use crate::{Error, INTERNAL_ERROR_MSG};
@ -4444,6 +4445,23 @@ impl<'help> App<'help> {
})
}
pub(crate) fn required_graph(&self) -> ChildGraph<Id> {
let mut reqs = ChildGraph::with_capacity(5);
for a in self.args.args().filter(|a| a.is_required_set()) {
reqs.insert(a.id.clone());
}
for group in &self.groups {
if group.required {
let idx = reqs.insert(group.id.clone());
for a in &group.requires {
reqs.insert_child(idx, a.clone());
}
}
}
reqs
}
pub(crate) fn unroll_args_in_group(&self, group: &Id) -> Vec<Id> {
debug!("App::unroll_args_in_group: group={:?}", group);
let mut g_vec = vec![group];

View file

@ -37,18 +37,7 @@ pub(crate) struct Parser<'help, 'app> {
// Initializing Methods
impl<'help, 'app> Parser<'help, 'app> {
pub(crate) fn new(app: &'app mut App<'help>) -> Self {
let mut reqs = ChildGraph::with_capacity(5);
for a in app.args.args().filter(|a| a.is_required_set()) {
reqs.insert(a.id.clone());
}
for group in &app.groups {
if group.required {
let idx = reqs.insert(group.id.clone());
for a in &group.requires {
reqs.insert_child(idx, a.clone());
}
}
}
let reqs = app.required_graph();
Parser {
app,