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 <dtor@chromium.org>
Tested-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dmitry Torokhov <dtor@chromium.org>
This commit is contained in:
Dmitry Torokhov 2019-12-18 17:52:51 -08:00 committed by Commit Bot
parent 9daf7907a3
commit e2e6cd8fe6

View file

@ -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]