]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Break vshCmddefOptParse into helper functions
authorNishith Shah <nishithshah.2211@gmail.com>
Fri, 8 Jul 2016 12:56:02 +0000 (18:26 +0530)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 11 Jul 2016 06:48:05 +0000 (08:48 +0200)
Decompose vshCmddefOptParse into two helper functions, vshCmddefOptFill
and vshCmddefCheckInternals.

vshCmddefCheckInternals checks if the internal command definitions are
correct or not.

vshCmddefOptFill keeps track of the required options and mandatory
arguments through opts_required and opts_need_arg.

Signed-off-by: Nishith Shah <nishithshah.2211@gmail.com>
tools/vsh.c

index 2b7891918482babb6f289bcd754dc4f2ba61ac8c..dcf99f2366552a8924baf30b3f09ea2b920259f1 100644 (file)
@@ -318,16 +318,11 @@ vshCmddefGetInfo(const vshCmdDef * cmd, const char *name)
     return NULL;
 }
 
-/* Validate that the options associated with cmd can be parsed.  */
+/* Check if the internal command definitions are correct */
 static int
-vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
-                  uint64_t *opts_required)
+vshCmddefCheckInternals(const vshCmdDef *cmd)
 {
     size_t i;
-    bool optional = false;
-
-    *opts_need_arg = 0;
-    *opts_required = 0;
 
     if (!cmd->opts)
         return 0;
@@ -338,7 +333,6 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
         if (i > 63)
             return -1; /* too many options */
         if (opt->type == VSH_OT_BOOL) {
-            optional = true;
             if (opt->flags & VSH_OFLAG_REQ)
                 return -1; /* bool options can't be mandatory */
             continue;
@@ -368,6 +362,34 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
                 return -1; /* alias option must map to a later option name */
             continue;
         }
+        if (opt->type == VSH_OT_ARGV && cmd->opts[i + 1].name)
+            return -1; /* argv option must be listed last */
+    }
+    return 0;
+}
+
+/* Keeps track of options that are required or need and argument */
+static int
+vshCmddefOptFill(const vshCmdDef *cmd, uint64_t *opts_need_arg,
+                 uint64_t *opts_required)
+{
+    size_t i;
+    bool optional = false;
+
+    *opts_need_arg = 0;
+    *opts_required = 0;
+
+    if (!cmd->opts)
+        return 0;
+
+    for (i = 0; cmd->opts[i].name; i++) {
+        const vshCmdOptDef *opt = &cmd->opts[i];
+
+        if (opt->type == VSH_OT_BOOL) {
+            optional = true;
+            continue;
+        }
+
         if (opt->flags & VSH_OFLAG_REQ_OPT) {
             if (opt->flags & VSH_OFLAG_REQ)
                 *opts_required |= 1ULL << i;
@@ -376,6 +398,9 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
             continue;
         }
 
+        if (opt->type == VSH_OT_ALIAS)
+            continue; /* skip the alias option */
+
         *opts_need_arg |= 1ULL << i;
         if (opt->flags & VSH_OFLAG_REQ) {
             if (optional && opt->type != VSH_OT_ARGV)
@@ -384,13 +409,24 @@ vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
         } else {
             optional = true;
         }
-
-        if (opt->type == VSH_OT_ARGV && cmd->opts[i + 1].name)
-            return -1; /* argv option must be listed last */
     }
     return 0;
 }
 
+/* Validate that the options associated with cmd can be parsed.  */
+static int
+vshCmddefOptParse(const vshCmdDef *cmd, uint64_t *opts_need_arg,
+                  uint64_t *opts_required)
+{
+    if (vshCmddefCheckInternals(cmd) < 0)
+        return -1;
+
+    if (vshCmddefOptFill(cmd, opts_need_arg, opts_required) < 0)
+        return -1;
+
+    return 0;
+}
+
 static vshCmdOptDef helpopt = {
     .name = "help",
     .type = VSH_OT_BOOL,