From 4a0335937fb482e412952a5e05ef738eab9542dc Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 18 May 2010 12:46:56 -0700 Subject: [PATCH] ovs-vsctl: Add support for command options that accept arguments. --- utilities/ovs-vsctl.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c index acc7841cb..9b4019777 100644 --- a/utilities/ovs-vsctl.c +++ b/utilities/ovs-vsctl.c @@ -306,12 +306,27 @@ parse_command(int argc, char *argv[], struct vsctl_command *command) shash_init(&command->options); for (i = 0; i < argc; i++) { - if (argv[i][0] != '-') { + const char *option = argv[i]; + const char *equals; + char *key, *value; + + if (option[0] != '-') { break; } - if (!shash_add_once(&command->options, argv[i], NULL)) { + + equals = strchr(option, '='); + if (equals) { + key = xmemdup0(option, equals - option); + value = xstrdup(equals + 1); + } else { + key = xstrdup(option); + value = NULL; + } + + if (shash_find(&command->options, key)) { vsctl_fatal("'%s' option specified multiple times", argv[i]); } + shash_add_nocopy(&command->options, key, value); } if (i == argc) { vsctl_fatal("missing command name"); @@ -325,10 +340,20 @@ parse_command(int argc, char *argv[], struct vsctl_command *command) SHASH_FOR_EACH (node, &command->options) { const char *s = strstr(p->options, node->name); int end = s ? s[strlen(node->name)] : EOF; - if (end != ',' && end != ' ' && end != '\0') { + + if (end != '=' && end != ',' && end != ' ' && end != '\0') { vsctl_fatal("'%s' command has no '%s' option", argv[i], node->name); } + if ((end == '=') != (node->data != NULL)) { + if (end == '=') { + vsctl_fatal("missing argument to '%s' option on '%s' " + "command", node->name, argv[i]); + } else { + vsctl_fatal("'%s' option on '%s' does not accept an " + "argument", node->name, argv[i]); + } + } } n_arg = argc - i - 1; -- 2.43.0