From a19f223deae815a1364e6b92fb92c1941615017c Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 18 Nov 2018 10:09:13 +0100 Subject: [PATCH] impl argument parameters --- src/getopts.rs | 35 +++++++++++++++++++++++++++++++++-- src/main.rs | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/getopts.rs b/src/getopts.rs index a77c1a49..074899ea 100644 --- a/src/getopts.rs +++ b/src/getopts.rs @@ -45,6 +45,7 @@ fn parse_argument(arg: &str) -> RawArgument { pub fn parse_arguments( args: &Vec, + arg_param: &Vec, schema: &ObjectSchema, ) -> Result<(Value,Vec), ParameterError> { @@ -129,8 +130,20 @@ pub fn parse_arguments( if pos >= args.len() { break; } } + for i in 0..arg_param.len() { + if rest.len() > i { + data.push((arg_param[i].clone(), rest[i].clone())); + } else { + errors.push(format_err!("missing argument '{}'", arg_param[i])); + } + } + if errors.len() > 0 { return Err(errors); } + if arg_param.len() > 0 { + rest = rest[arg_param.len()..].to_vec(); + } + let options = parse_parameter_strings(&data, schema, true)?; Ok((options,rest)) @@ -158,12 +171,30 @@ fn test_boolean_arg() { for (args, expect) in variants { let string_args = args.iter().map(|s| s.to_string()).collect(); - let res = parse_arguments(&string_args, &schema); + let res = parse_arguments(&string_args, &vec![], &schema); assert!(res.is_ok()); if let Ok((options, rest)) = res { assert!(options["enable"] == expect); assert!(rest.len() == 0); } } - +} + +#[test] +fn test_argument_paramenter() { + + let schema = parameter!{ + enable => Boolean!{ optional => false }, + storage => ApiString!{ optional => false } + }; + + let args = vec!["-enable", "local"]; + let string_args = args.iter().map(|s| s.to_string()).collect(); + let res = parse_arguments(&string_args, &vec!["storage".to_string()], &schema); + assert!(res.is_ok()); + if let Ok((options, rest)) = res { + assert!(options["enable"] == true); + assert!(options["storage"] == "local"); + assert!(rest.len() == 0); + } } diff --git a/src/main.rs b/src/main.rs index 3f7fbf5d..2c4fee2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { }; let args: Vec = std::env::args().skip(1).collect(); - match getopts::parse_arguments(&args, &schema) { + match getopts::parse_arguments(&args, &vec![], &schema) { Ok((options, rest)) => { println!("Got Options: {}", options); println!("Remaining Arguments: {:?}", rest);