router: cli: move freestanding new parse loop into its method

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2024-07-04 09:54:45 +02:00
parent 95c0614ccd
commit 9288c00372

View File

@ -319,18 +319,6 @@ impl<'t, 'o> ParseOptions<'t, 'o> {
A: IntoIterator<Item = AI>,
AI: AsRef<str>,
{
parse_parameters(args, self)
}
}
pub(crate) fn parse_parameters<A, AI>(
args: A,
parse_opts: ParseOptions<'_, '_>,
) -> Result<Vec<AI>, ParameterError>
where
A: IntoIterator<Item = AI>,
AI: AsRef<str>,
{
let mut errors = ParameterError::new();
let mut positional = Vec::new();
@ -339,7 +327,7 @@ where
let arg = orig_arg.as_ref();
if arg == "--" {
if parse_opts.retain_separator {
if self.retain_separator {
positional.push(orig_arg);
}
break;
@ -349,7 +337,7 @@ where
Some(opt) => opt,
None => {
positional.push(orig_arg);
if parse_opts.stop_at_positional {
if self.stop_at_positional {
break;
}
continue;
@ -358,32 +346,29 @@ where
if let Some(eq) = option.find('=') {
let (option, argument) = (&option[..eq], &option[(eq + 1)..]);
if parse_opts.deny_unknown && !parse_opts.option_schemas.contains_key(option) {
if parse_opts.stop_at_unknown {
if self.deny_unknown && !self.option_schemas.contains_key(option) {
if self.stop_at_unknown {
positional.push(orig_arg);
break;
}
errors.push(option.to_string(), format_err!("unknown option {option:?}"));
}
parse_opts
.target
.push((option.to_string(), argument.to_string()));
self.target.push((option.to_string(), argument.to_string()));
continue;
}
if parse_opts.deny_unknown && !parse_opts.option_schemas.contains_key(option) {
if parse_opts.stop_at_unknown {
if self.deny_unknown && !self.option_schemas.contains_key(option) {
if self.stop_at_unknown {
positional.push(orig_arg);
break;
}
errors.push(option.to_string(), format_err!("unknown option {option:?}"));
}
match parse_opts.option_schemas.get(option) {
match self.option_schemas.get(option) {
Some(Schema::Boolean(schema)) => {
if let Some(value) = args.next_if(|v| parse_boolean(v.as_ref()).is_ok()) {
parse_opts
.target
self.target
.push((option.to_string(), value.as_ref().to_string()));
} else {
// next parameter is not a boolean value
@ -391,9 +376,7 @@ where
// default-true booleans cannot be passed without values:
errors.push(option.to_string(), format_err!("missing boolean value."));
}
parse_opts
.target
.push((option.to_string(), "true".to_string()))
self.target.push((option.to_string(), "true".to_string()))
}
}
_ => {
@ -401,13 +384,12 @@ where
let next = match args.next() {
Some(next) => next.as_ref().to_string(),
None => {
errors.push(option.to_string(), format_err!("missing parameter value."));
errors
.push(option.to_string(), format_err!("missing parameter value."));
break;
}
};
parse_opts
.target
.push((option.to_string(), next.to_string()));
self.target.push((option.to_string(), next.to_string()));
continue;
}
}
@ -419,4 +401,5 @@ where
positional.extend(args);
Ok(positional)
}
}