From e2e6cd8fe6861d8e42a1b57d099c25265acf5c5f Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Wed, 18 Dec 2019 17:52:51 -0800 Subject: Fix parsing arguments that require a value at the end of command line We may be in state of waiting for the value for the parameter, and run out of the parameters. In this case we should try to parse the parameter as if it does not have a value and see if that succeeds. This makes sure that crosvm run ... --plugin-mount fails with error that --plugin-mount option needs a value instead of succeeding. BUG=None TEST=cargo test Change-Id: I9f3f1f3c7e6e2ca88efed1eeea5a52dd4aae70ef Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1975097 Reviewed-by: Dmitry Torokhov Tested-by: Dmitry Torokhov Tested-by: kokoro Commit-Queue: Dmitry Torokhov --- src/argument.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/argument.rs') diff --git a/src/argument.rs b/src/argument.rs index 2d32083..f59d503 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -313,7 +313,14 @@ where } } } - Ok(()) + + // If we ran out of arguments while parsing the last parameter, which may be either a + // value parameter or a flag, try to parse it as a flag. This will produce "missing value" + // error if the parameter is in fact a value parameter, which is the desired outcome. + match s { + State::Value { name } => f(&name, None), + _ => Ok(()), + } } /// Parses the given `args` against the list of know arguments `arg_list` and calls `f` with each @@ -473,6 +480,16 @@ mod tests { }, ); assert!(match_res.is_ok()); + let not_match_res = set_arguments( + ["-c", "5", "--cpus"].iter(), + &arguments[..], + |name, value| { + assert_eq!(name, "cpus"); + assert_eq!(value, Some("5")); + Ok(()) + }, + ); + assert!(not_match_res.is_err()); } #[test] -- cgit 1.4.1