]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tools: vshCmddefCheckInternals: Port mandatory options check from vshCmddefOptParse
authorPeter Krempa <pkrempa@redhat.com>
Thu, 12 Nov 2020 12:30:29 +0000 (13:30 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 13 Nov 2020 10:12:29 +0000 (11:12 +0100)
'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>
tools/vsh.c

index d1e795bbc159689b988b1dac03de8601e934f7dd..28c7533a25ef9a4aefe2c6662022d07a2e842cd7 100644 (file)
@@ -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;
         }
     }