This commit adds a function that prints (both long and short)
options of a ovs-* command.  To use this function, option
'--option' is added to ovs-appctl/dpctl/ofctl and ovsdb-tool
ommands.  A future patch will the option output to conduct
bash command-line completion.

Signed-off-by: Alex Wang <al...@nicira.com>

---
PATCH -> V2:
- distinguish optional and compulsory arguments.
- call print() in the print_option().
---
 lib/command-line.c     |   21 +++++++++++++++++++++
 lib/command-line.h     |    1 +
 ovsdb/ovsdb-tool.c     |    5 +++++
 utilities/ovs-appctl.c |    6 ++++++
 utilities/ovs-dpctl.c  |    6 +++++-
 utilities/ovs-ofctl.c  |    5 +++++
 6 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/lib/command-line.c b/lib/command-line.c
index cb73a25..1f26c5c 100644
--- a/lib/command-line.c
+++ b/lib/command-line.c
@@ -19,6 +19,7 @@
 #include <getopt.h>
 #include <limits.h>
 #include <stdlib.h>
+#include "dynamic-string.h"
 #include "ovs-thread.h"
 #include "util.h"
 #include "vlog.h"
@@ -51,6 +52,26 @@ long_options_to_short_options(const struct option options[])
     return xstrdup(short_options);
 }
 
+/* Given the GNU-style options in 'options', prints all options. */
+void
+print_options(const struct option options[])
+{
+    struct ds ds = DS_EMPTY_INITIALIZER;
+
+    for (; options->name; options++) {
+        const struct option *o = options;
+        const char *arg = o->has_arg == required_argument ? "ARG" : "[ARG]";
+
+        ds_put_format(&ds, "--%s%s%s\n", o->name, o->has_arg ? "=" : "",
+                      o->has_arg ? arg : "");
+        if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
+            ds_put_format(&ds, "-%c %s\n", o->val, o->has_arg ? arg : "");
+        }
+    }
+    printf("%s", ds.string);
+    ds_destroy(&ds);
+}
+
 /* Runs the command designated by argv[0] within the command table specified by
  * 'commands', which must be terminated by a command whose 'name' member is a
  * null pointer.
diff --git a/lib/command-line.h b/lib/command-line.h
index bb12f72..157cb58 100644
--- a/lib/command-line.h
+++ b/lib/command-line.h
@@ -31,6 +31,7 @@ struct command {
 };
 
 char *long_options_to_short_options(const struct option *options);
+void print_options(const struct option *options);
 void run_command(int argc, char *argv[], const struct command[]);
 
 void proctitle_init(int argc, char **argv);
diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c
index 350e72a..27bd1e6 100644
--- a/ovsdb/ovsdb-tool.c
+++ b/ovsdb/ovsdb-tool.c
@@ -69,6 +69,7 @@ parse_options(int argc, char *argv[])
         {"more", no_argument, NULL, 'm'},
         {"verbose", optional_argument, NULL, 'v'},
         {"help", no_argument, NULL, 'h'},
+        {"option", no_argument, NULL, 'o'},
         {"version", no_argument, NULL, 'V'},
         {NULL, 0, NULL, 0},
     };
@@ -90,6 +91,10 @@ parse_options(int argc, char *argv[])
         case 'h':
             usage();
 
+        case 'o':
+            print_options(long_options);
+            exit(EXIT_SUCCESS);
+
         case 'V':
             ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c
index bb17ec2..3d32cbd 100644
--- a/utilities/ovs-appctl.c
+++ b/utilities/ovs-appctl.c
@@ -110,12 +110,14 @@ static const char *
 parse_command_line(int argc, char *argv[])
 {
     enum {
+        OPT_START = UCHAR_MAX + 1,
         VLOG_OPTION_ENUMS
     };
     static const struct option long_options[] = {
         {"target", required_argument, NULL, 't'},
         {"execute", no_argument, NULL, 'e'},
         {"help", no_argument, NULL, 'h'},
+        {"option", no_argument, NULL, 'o'},
         {"version", no_argument, NULL, 'V'},
         {"timeout", required_argument, NULL, 'T'},
         VLOG_LONG_OPTIONS,
@@ -157,6 +159,10 @@ parse_command_line(int argc, char *argv[])
             usage();
             break;
 
+        case 'o':
+            print_options(long_options);
+            exit(EXIT_SUCCESS);
+
         case 'T':
             time_alarm(atoi(optarg));
             break;
diff --git a/utilities/ovs-dpctl.c b/utilities/ovs-dpctl.c
index 94a6b90..6d928af 100644
--- a/utilities/ovs-dpctl.c
+++ b/utilities/ovs-dpctl.c
@@ -85,6 +85,7 @@ parse_options(int argc, char *argv[])
         {"more", no_argument, NULL, 'm'},
         {"timeout", required_argument, NULL, 't'},
         {"help", no_argument, NULL, 'h'},
+        {"option", no_argument, NULL, 'o'},
         {"version", no_argument, NULL, 'V'},
         VLOG_LONG_OPTIONS,
         {NULL, 0, NULL, 0},
@@ -130,6 +131,10 @@ parse_options(int argc, char *argv[])
         case 'h':
             usage(NULL);
 
+        case 'o':
+            print_options(long_options);
+            exit(EXIT_SUCCESS);
+
         case 'V':
             ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
@@ -184,4 +189,3 @@ usage(void *userdata OVS_UNUSED)
            "  -V, --version               display version information\n");
     exit(EXIT_SUCCESS);
 }
-
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index ae8d59d..77f5a29 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -171,6 +171,7 @@ parse_options(int argc, char *argv[])
         {"rsort", optional_argument, NULL, OPT_RSORT},
         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
         {"help", no_argument, NULL, 'h'},
+        {"option", no_argument, NULL, 'o'},
         DAEMON_LONG_OPTIONS,
         OFP_VERSION_LONG_OPTIONS,
         VLOG_LONG_OPTIONS,
@@ -240,6 +241,10 @@ parse_options(int argc, char *argv[])
         case 'h':
             usage();
 
+        case 'o':
+            print_options(long_options);
+            exit(EXIT_SUCCESS);
+
         case OPT_STRICT:
             strict = true;
             break;
-- 
1.7.9.5

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to