This commit adds function that extracts (both long and short) options and returns them as string. Right now, only ovs-appctl command provides an option to print this function output. And the output is to be used by bash command-line completion script for completion other options of the same command.
Signed-off-by: Alex Wang <al...@nicira.com> --- lib/command-line.c | 23 +++++++++++++++++++++++ lib/command-line.h | 1 + utilities/ovs-appctl.c | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/command-line.c b/lib/command-line.c index cb73a25..820f3ff 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,28 @@ long_options_to_short_options(const struct option options[]) return xstrdup(short_options); } +/* Given the GNU-style options in 'options', returns a string which prints all + * options. The caller is responsible for freeing the string. */ +char * +print_options(const struct option options[]) +{ + struct ds ds = DS_EMPTY_INITIALIZER; + char *ret; + + for (; options->name; options++) { + const struct option *o = options; + + ds_put_format(&ds, "--%s%s\n", o->name, 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" : ""); + } + } + ret = xstrdup(ds_cstr(&ds)); + ds_destroy(&ds); + + return ret; +} + /* 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..f7d5d23 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); +char *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/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index bb17ec2..9ae5201 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -81,6 +81,18 @@ main(int argc, char *argv[]) } static void +print_appctl_options(const struct option options[]) +{ + char *opts; + + opts = print_options(options); + printf("%s", opts); + free(opts); + + exit(EXIT_SUCCESS); +} + +static void usage(void) { printf("\ @@ -110,12 +122,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 +171,10 @@ parse_command_line(int argc, char *argv[]) usage(); break; + case 'o': + print_appctl_options(long_options); + break; + case 'T': time_alarm(atoi(optarg)); break; -- 1.7.9.5 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev