cmd: Add accessor functions for options
authorSimon Wilkinson <sxw@your-file-system.com>
Tue, 19 Apr 2011 10:41:54 +0000 (11:41 +0100)
committerDerrick Brashear <shadow@dementia.org>
Wed, 27 Apr 2011 19:06:26 +0000 (12:06 -0700)
Add a load of accessor functions to help with pulling values out
from the the cmd_syndesc structure. The idea here is to make it
simpler to manipulate command line values, as well as starting to
hide the structure of the cmd_syndesc structure from callers, with
a view to eventually making it private to the cmd library.

Change-Id: I38757d776364ceacd43e98fda16e995da35da65c
Reviewed-on: http://gerrit.openafs.org/4543
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: BuildBot <buildbot@rampaginggeek.com>

src/cmd/cmd.c
src/cmd/cmd.p.h
src/cmd/cmd_errors.et
tests/cmd/command-t.c

index c47c63b..edd4ec0 100644 (file)
@@ -1127,3 +1127,64 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
        }
     }
 }
+
+int
+cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL ||
+       syn->parms[pos].items->data == NULL)
+       return CMD_MISSING;
+    *value = strtol(syn->parms[pos].items->data, NULL, 10);
+
+    return 0;
+}
+
+int
+cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
+       return CMD_MISSING;
+
+    if (*value)
+       free(*value);
+    *value = strdup(syn->parms[pos].items->data);
+
+    return 0;
+}
+
+int
+cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL)
+       return CMD_MISSING;
+
+    *value = syn->parms[pos].items;
+    return 0;
+}
+
+int
+cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL)
+       return CMD_MISSING;
+
+    *value = 1;
+    return 0;
+}
+
+int
+cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
+{
+    if (pos > syn->nParms || syn->parms[pos].items == NULL)
+       return 0;
+
+    return 1;
+}
index dfe48a1..139d7bc 100644 (file)
@@ -86,5 +86,10 @@ extern void PrintFlagHelp(struct cmd_syndesc *as);
 
 extern int cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax);
 extern void cmd_FreeOptions(struct cmd_syndesc **ts);
+extern int cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value);
+extern int cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value);
+extern int cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **);
+extern int cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value);
+extern int cmd_OptionPresent(struct cmd_syndesc *syn, int pos);
 
 #endif /* __CMD_INCL__ */
index 1f6e6b1..fd8453b 100644 (file)
@@ -18,4 +18,5 @@ error_table CMD
   ec CMD_AMBIG, "<unused>"
   ec CMD_TOOFEW, "Insufficient required parameters provided"
   ec CMD_TOOBIG, "Token too large"
+  ec CMD_MISSING, "Option not specified on command line"
 end
index d3098a7..c599f49 100644 (file)
@@ -56,8 +56,10 @@ main(int argc, char **argv)
     struct cmd_syndesc *retopts;
     int code;
     int tc;
+    int retval;
+    char *retstring;
 
-    plan(51);
+    plan(58);
 
     initialize_CMD_error_table();
 
@@ -190,6 +192,29 @@ main(int argc, char **argv)
     cmd_FreeOptions(&retopts);
     cmd_FreeArgv(tv);
 
+    /* Check Accessors */
+    code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100);
+    is_int(0, code, "cmd_ParseLine succeeds");
+    code = cmd_Parse(tc, tv, &retopts);
+
+    code = cmd_OptionAsInt(retopts, 0, &retval);
+    is_int(0, code, "cmd_OptionsAsInt succeeds");
+    is_int(1, retval, " ... and returns correct value");
+
+    code = cmd_OptionAsString(retopts, 1, &retstring);
+    is_int(0, code, "cmd_OptionsAsString succeeds");
+    is_string("second", retstring, " ... and returns correct value");
+    free(retstring);
+    retstring = NULL;
+
+    code = cmd_OptionAsFlag(retopts, 2, &retval);
+    is_int(0, code, "cmd_OptionsAsFlag succeeds");
+    ok(retval, " ... and flag is correct");
+
+    cmd_FreeOptions(&retopts);
+    cmd_FreeArgv(tv);
+
+
     return 0;
 }