From 184a3d1fb59a51ff8080ed7bcff06c5aebe8507e Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 8 Nov 2021 09:29:11 +0100 Subject: [PATCH] option_parser: Extend option parsing for lists While parsing each parameter for getting the list of option/value, we check if the value is a list separated between brackets. If that's the case, we reconstruct the list so that it can be parsed afterwards, even though it uses commas for separating each value. Signed-off-by: Sebastien Boeuf --- option_parser/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/option_parser/src/lib.rs b/option_parser/src/lib.rs index e9b44461c..844cbdd82 100644 --- a/option_parser/src/lib.rs +++ b/option_parser/src/lib.rs @@ -49,7 +49,26 @@ impl OptionParser { return Ok(()); } - let options_list: Vec<&str> = input.trim().split(',').collect(); + let mut options_list: Vec = Vec::new(); + let mut merge_elements = false; + for element in input.trim().split(',') { + if merge_elements { + if let Some(last) = options_list.last_mut() { + *last = format!("{},{}", last, element); + } else { + return Err(OptionParserError::InvalidSyntax(input.to_owned())); + } + } else { + options_list.push(element.to_string()); + } + + if element.contains('[') { + merge_elements = true; + } + if element.contains(']') { + merge_elements = false; + } + } for option in options_list.iter() { let parts: Vec<&str> = option.split('=').collect();