From b5fa28251ce7f718d318dcb55f666001b64a2589 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 12 Mar 2019 14:38:47 +0100 Subject: [PATCH] src/cli/getopts.rs: factor out reusable code --- src/cli/getopts.rs | 82 +++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/src/cli/getopts.rs b/src/cli/getopts.rs index f8a31bbf..457aade8 100644 --- a/src/cli/getopts.rs +++ b/src/cli/getopts.rs @@ -52,47 +52,22 @@ fn parse_argument(arg: &str) -> RawArgument { }; } -/// Parses command line arguments using a `Schema` -/// -/// Returns parsed options as json object, together with the -/// list of additional command line arguments. -pub fn parse_arguments>( +/// parse as many arguments as possible into a Vec. This does not +/// verify the schema. +/// Returns parsed data and the rest as separate array +pub (crate) fn parse_argument_list>( args: &[T], - arg_param: &Vec<&'static str>, schema: &ObjectSchema, -) -> Result<(Value, Vec), ParameterError> { - let mut errors = ParameterError::new(); - - let properties = &schema.properties; - - // first check if all arg_param exists in schema - - let mut last_arg_param_is_optional = false; - let mut last_arg_param_is_array = false; - - for i in 0..arg_param.len() { - let name = arg_param[i]; - if let Some((optional, param_schema)) = properties.get::(&name) { - if i == arg_param.len() -1 { - last_arg_param_is_optional = *optional; - if let Schema::Array(_) = param_schema.as_ref() { - last_arg_param_is_array = true; - } - } else { - if *optional { - panic!("positional argument '{}' may not be optional", name); - } - } - } else { - panic!("no such property '{}' in schema", name); - } - } + errors: &mut ParameterError, +) -> (Vec<(String, String)>, Vec) { let mut data: Vec<(String, String)> = vec![]; let mut rest: Vec = vec![]; let mut pos = 0; + let properties = &schema.properties; + while pos < args.len() { match parse_argument(args[pos].as_ref()) { RawArgument::Separator => { @@ -167,6 +142,47 @@ pub fn parse_arguments>( rest.push(i.as_ref().to_string()); } + (data, rest) +} + +/// Parses command line arguments using a `Schema` +/// +/// Returns parsed options as json object, together with the +/// list of additional command line arguments. +pub fn parse_arguments>( + args: &[T], + arg_param: &Vec<&'static str>, + schema: &ObjectSchema, +) -> Result<(Value, Vec), ParameterError> { + let mut errors = ParameterError::new(); + + let properties = &schema.properties; + + // first check if all arg_param exists in schema + + let mut last_arg_param_is_optional = false; + let mut last_arg_param_is_array = false; + + for i in 0..arg_param.len() { + let name = arg_param[i]; + if let Some((optional, param_schema)) = properties.get::(&name) { + if i == arg_param.len() -1 { + last_arg_param_is_optional = *optional; + if let Schema::Array(_) = param_schema.as_ref() { + last_arg_param_is_array = true; + } + } else { + if *optional { + panic!("positional argument '{}' may not be optional", name); + } + } + } else { + panic!("no such property '{}' in schema", name); + } + } + + let (mut data, mut rest) = parse_argument_list(args, schema, &mut errors); + for i in 0..arg_param.len() { let name = arg_param[i];