getopt: let parse_arguments() take a slice of AsRef<str>
We don't need the content to be owned strings, and we don't need the list to be a Vec, since we only care about being able to iterate through and copy out portions of the strings we need, so take an &[T] where T: AsRef<str>. This avoids .iter().map(to_string).collect() before calling parse_arguments(). Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
2482c095b1
commit
4d0ea9978b
@ -52,8 +52,8 @@ fn parse_argument(arg: &str) -> RawArgument {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_arguments(
|
pub fn parse_arguments<T: AsRef<str>>(
|
||||||
args: &Vec<String>,
|
args: &[T],
|
||||||
arg_param: &Vec<&'static str>,
|
arg_param: &Vec<&'static str>,
|
||||||
schema: &ObjectSchema,
|
schema: &ObjectSchema,
|
||||||
) -> Result<(Value,Vec<String>), ParameterError> {
|
) -> Result<(Value,Vec<String>), ParameterError> {
|
||||||
@ -68,7 +68,7 @@ pub fn parse_arguments(
|
|||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
|
|
||||||
while pos < args.len() {
|
while pos < args.len() {
|
||||||
match parse_argument(&args[pos]) {
|
match parse_argument(args[pos].as_ref()) {
|
||||||
RawArgument::Separator => {
|
RawArgument::Separator => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -92,7 +92,7 @@ pub fn parse_arguments(
|
|||||||
let mut next_is_bool = false;
|
let mut next_is_bool = false;
|
||||||
|
|
||||||
if (pos + 1) < args.len() {
|
if (pos + 1) < args.len() {
|
||||||
let next = &args[pos+1];
|
let next = args[pos+1].as_ref();
|
||||||
if let RawArgument::Argument { value: _} = parse_argument(next) {
|
if let RawArgument::Argument { value: _} = parse_argument(next) {
|
||||||
next_is_argument = true;
|
next_is_argument = true;
|
||||||
if let Ok(_) = parse_boolean(next) { next_is_bool = true; }
|
if let Ok(_) = parse_boolean(next) { next_is_bool = true; }
|
||||||
@ -102,7 +102,7 @@ pub fn parse_arguments(
|
|||||||
if want_bool {
|
if want_bool {
|
||||||
if next_is_bool {
|
if next_is_bool {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
data.push((name, args[pos].clone()));
|
data.push((name, args[pos].as_ref().to_string()));
|
||||||
} else if can_default {
|
} else if can_default {
|
||||||
data.push((name, "true".to_string()));
|
data.push((name, "true".to_string()));
|
||||||
} else {
|
} else {
|
||||||
@ -114,7 +114,7 @@ pub fn parse_arguments(
|
|||||||
|
|
||||||
if next_is_argument {
|
if next_is_argument {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
data.push((name, args[pos].clone()));
|
data.push((name, args[pos].as_ref().to_string()));
|
||||||
} else {
|
} else {
|
||||||
errors.push(format_err!("parameter '{}': {}", name,
|
errors.push(format_err!("parameter '{}': {}", name,
|
||||||
"missing parameter value."));
|
"missing parameter value."));
|
||||||
@ -134,7 +134,10 @@ pub fn parse_arguments(
|
|||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rest.extend_from_slice(&args[pos..]);
|
rest.reserve(args.len() - pos);
|
||||||
|
for i in &args[pos..] {
|
||||||
|
rest.push(i.as_ref().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
for i in 0..arg_param.len() {
|
for i in 0..arg_param.len() {
|
||||||
if rest.len() > i {
|
if rest.len() > i {
|
||||||
@ -179,8 +182,7 @@ fn test_boolean_arg() {
|
|||||||
variants.push((vec!["--enable", "false"], false));
|
variants.push((vec!["--enable", "false"], false));
|
||||||
|
|
||||||
for (args, expect) in variants {
|
for (args, expect) in variants {
|
||||||
let string_args = args.iter().map(|s| s.to_string()).collect();
|
let res = parse_arguments(&args, &vec![], &schema);
|
||||||
let res = parse_arguments(&string_args, &vec![], &schema);
|
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
if let Ok((options, rest)) = res {
|
if let Ok((options, rest)) = res {
|
||||||
assert!(options["enable"] == expect);
|
assert!(options["enable"] == expect);
|
||||||
@ -197,8 +199,7 @@ fn test_argument_paramenter() {
|
|||||||
.required("storage", StringSchema::new("Storage."));
|
.required("storage", StringSchema::new("Storage."));
|
||||||
|
|
||||||
let args = vec!["-enable", "local"];
|
let args = vec!["-enable", "local"];
|
||||||
let string_args = args.iter().map(|s| s.to_string()).collect();
|
let res = parse_arguments(&args, &vec!["storage"], &schema);
|
||||||
let res = parse_arguments(&string_args, &vec!["storage"], &schema);
|
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
if let Ok((options, rest)) = res {
|
if let Ok((options, rest)) = res {
|
||||||
assert!(options["enable"] == true);
|
assert!(options["enable"] == true);
|
||||||
|
Loading…
Reference in New Issue
Block a user