From: Peter Krempa Date: Thu, 12 Nov 2020 12:30:29 +0000 (+0100) Subject: tools: vshCmddefCheckInternals: Port mandatory options check from vshCmddefOptParse X-Git-Tag: v6.10.0-rc1~199 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fa7265e1274bf719d9979467529e8b3336a79e75;p=thirdparty%2Flibvirt.git tools: vshCmddefCheckInternals: Port mandatory options check from vshCmddefOptParse 'vshCmddefCheckInternals' is the go-to place for all checks related to the definition of parameters for commands, but the check that all mandatory parameters must be ordered before optional parameters was still only in vshCmddefOptParse. Adding a non-compliant option would not be caught by our test suite as 'virsh self-test' doesn't call vshCmddefOptParse. Re-implement the check in vshCmddefCheckInternals. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- diff --git a/tools/vsh.c b/tools/vsh.c index d1e795bbc1..28c7533a25 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -276,6 +276,7 @@ vshCmddefCheckInternals(vshControl *ctl, { size_t i; const char *help = NULL; + bool seenOptionalOption = false; /* in order to perform the validation resolve the alias first */ if (cmd->flags & VSH_CMD_FLAG_ALIAS) { @@ -311,6 +312,8 @@ vshCmddefCheckInternals(vshControl *ctl, opt->name, cmd->name); return -1; /* neither bool nor string options can be mandatory */ } + + seenOptionalOption = true; break; case VSH_OT_ALIAS: { @@ -360,9 +363,25 @@ vshCmddefCheckInternals(vshControl *ctl, opt->name, cmd->name); return -1; /* OT_DATA should always be required. */ } + + if (seenOptionalOption) { + vshError(ctl, _("parameter '%s' of command '%s' must be listed before optional parameters"), + opt->name, cmd->name); + return -1; /* mandatory options must be listed first */ + } break; case VSH_OT_INT: + if (opt->flags & VSH_OFLAG_REQ) { + if (seenOptionalOption) { + vshError(ctl, _("parameter '%s' of command '%s' must be listed before optional parameters"), + opt->name, cmd->name); + return -1; /* mandatory options must be listed first */ + } + } else { + seenOptionalOption = true; + } + break; } }