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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-11-12 13:30:29 +01:00
parent b070332261
commit fa7265e127

View File

@ -276,6 +276,7 @@ vshCmddefCheckInternals(vshControl *ctl,
{ {
size_t i; size_t i;
const char *help = NULL; const char *help = NULL;
bool seenOptionalOption = false;
/* in order to perform the validation resolve the alias first */ /* in order to perform the validation resolve the alias first */
if (cmd->flags & VSH_CMD_FLAG_ALIAS) { if (cmd->flags & VSH_CMD_FLAG_ALIAS) {
@ -311,6 +312,8 @@ vshCmddefCheckInternals(vshControl *ctl,
opt->name, cmd->name); opt->name, cmd->name);
return -1; /* neither bool nor string options can be mandatory */ return -1; /* neither bool nor string options can be mandatory */
} }
seenOptionalOption = true;
break; break;
case VSH_OT_ALIAS: { case VSH_OT_ALIAS: {
@ -360,9 +363,25 @@ vshCmddefCheckInternals(vshControl *ctl,
opt->name, cmd->name); opt->name, cmd->name);
return -1; /* OT_DATA should always be required. */ 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; break;
case VSH_OT_INT: 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; break;
} }
} }