main: fix switch-or-option argh wrapper nits

Fix two small bugs in the code that handles options that are valid with
or without extra options (--battery, --video-encoder, --video-decoder,
--gpu, and --gpu-display):
- If the next arg was a short option (e.g. `-p`), we didn't consider it
  the start of a new flag, so it would have been used as the parameters
  for a switch-or-option arg (e.g. `--gpu -p something` would parse as
  `--gpu=-p`). Fix this by only requiring a single dash instead of two.
- The `crosvm run` command requires a positional argument at the end
  (kernel filename), and the check for the switch-or-option arg had an
  off-by-one that did not consider the positional argument. This broke
  the case where one of these switches occurred right before the kernel
  filename argument, e.g. `crosvm run [...] --gpu vm_kernel`. Fix this
  by comparing against len - 2 instead of len - 1.

BUG=b:237334804
TEST=crosvm run --battery -p init=/bin/bash -r vm_rootfs.img vm_kernel
TEST=crosvm run -p init=/bin/bash -r vm_roofs.img --battery vm_kernel

Change-Id: I2c44ca124ba35b980e655f4f0cf9a9366e6be33d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3749939
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2022-07-06 13:41:30 -07:00 committed by Chromeos LUCI
parent de7835d9b0
commit a44e8a03e3

View file

@ -447,7 +447,7 @@ fn crosvm_main() -> Result<CommandStatus> {
];
for arg in switch_or_option {
if let Some(i) = args.iter().position(|a| a == arg) {
if i == args.len() - 1 || args[i + 1].starts_with("--") {
if i >= args.len() - 2 || args[i + 1].starts_with("-") {
args.insert(i + 1, "".to_string());
}
}