mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
refactor(parser): Initialize MatchedArg from its source
This commit is contained in:
parent
e6b1468477
commit
8a58884459
3 changed files with 44 additions and 16 deletions
|
@ -130,23 +130,22 @@ impl ArgMatcher {
|
|||
pub(crate) fn start_occurrence_of_arg(&mut self, arg: &Arg) {
|
||||
let id = &arg.id;
|
||||
debug!("ArgMatcher::start_occurrence_of_arg: id={:?}", id);
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new());
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new_arg(arg));
|
||||
ma.set_source(ValueSource::CommandLine);
|
||||
ma.set_ignore_case(arg.is_ignore_case_set());
|
||||
ma.inc_occurrences();
|
||||
}
|
||||
|
||||
pub(crate) fn start_occurrence_of_group(&mut self, id: &Id) {
|
||||
debug!("ArgMatcher::start_occurrence_of_group: id={:?}", id);
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new());
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new_group());
|
||||
ma.set_source(ValueSource::CommandLine);
|
||||
ma.inc_occurrences();
|
||||
}
|
||||
|
||||
pub(crate) fn start_occurrence_of_external(&mut self) {
|
||||
pub(crate) fn start_occurrence_of_external(&mut self, cmd: &crate::Command) {
|
||||
let id = &Id::empty_hash();
|
||||
debug!("ArgMatcher::start_occurrence_of_external: id={:?}", id,);
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new());
|
||||
let ma = self.entry(id).or_insert(MatchedArg::new_external(cmd));
|
||||
ma.set_source(ValueSource::CommandLine);
|
||||
ma.inc_occurrences();
|
||||
}
|
||||
|
|
|
@ -22,14 +22,39 @@ pub(crate) struct MatchedArg {
|
|||
}
|
||||
|
||||
impl MatchedArg {
|
||||
pub(crate) fn new() -> Self {
|
||||
MatchedArg {
|
||||
pub(crate) fn new_arg(arg: &crate::Arg) -> Self {
|
||||
let ignore_case = arg.is_ignore_case_set();
|
||||
Self {
|
||||
occurs: 0,
|
||||
source: None,
|
||||
indices: Vec::new(),
|
||||
vals: Vec::new(),
|
||||
raw_vals: Vec::new(),
|
||||
ignore_case: false,
|
||||
ignore_case,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn new_group() -> Self {
|
||||
let ignore_case = false;
|
||||
Self {
|
||||
occurs: 0,
|
||||
source: None,
|
||||
indices: Vec::new(),
|
||||
vals: Vec::new(),
|
||||
raw_vals: Vec::new(),
|
||||
ignore_case,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn new_external(_cmd: &crate::Command) -> Self {
|
||||
let ignore_case = false;
|
||||
Self {
|
||||
occurs: 0,
|
||||
source: None,
|
||||
indices: Vec::new(),
|
||||
vals: Vec::new(),
|
||||
raw_vals: Vec::new(),
|
||||
ignore_case,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,15 +171,19 @@ impl MatchedArg {
|
|||
self.source = Some(source)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_ignore_case(&mut self, yes: bool) {
|
||||
self.ignore_case = yes;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MatchedArg {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
let ignore_case = false;
|
||||
Self {
|
||||
occurs: 0,
|
||||
source: None,
|
||||
indices: Vec::new(),
|
||||
vals: Vec::new(),
|
||||
raw_vals: Vec::new(),
|
||||
ignore_case,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,7 +221,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_grouped_vals_first() {
|
||||
let mut m = MatchedArg::new();
|
||||
let mut m = MatchedArg::new_group();
|
||||
m.new_val_group();
|
||||
m.new_val_group();
|
||||
m.append_val(AnyValue::new(String::from("bbb")), "bbb".into());
|
||||
|
@ -202,7 +231,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_grouped_vals_push_and_append() {
|
||||
let mut m = MatchedArg::new();
|
||||
let mut m = MatchedArg::new_group();
|
||||
m.push_val(AnyValue::new(String::from("aaa")), "aaa".into());
|
||||
m.new_val_group();
|
||||
m.append_val(AnyValue::new(String::from("bbb")), "bbb".into());
|
||||
|
|
|
@ -431,7 +431,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
|
|||
// Collect the external subcommand args
|
||||
let mut sc_m = ArgMatcher::new(self.cmd);
|
||||
if cfg!(feature = "unstable-v4") || !raw_args.is_end(&args_cursor) {
|
||||
sc_m.start_occurrence_of_external();
|
||||
sc_m.start_occurrence_of_external(self.cmd);
|
||||
}
|
||||
|
||||
for raw_val in raw_args.remaining(&mut args_cursor) {
|
||||
|
|
Loading…
Reference in a new issue