summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorBryce Harrington <bryce@osg.samsung.com>2016-02-11 15:25:56 -0800
committerBryce Harrington <bryce@osg.samsung.com>2016-03-08 16:32:16 -0800
commit38298ec0b45661d2969fc75c6cb47e0863755792 (patch)
tree7fd8d8ccfa50f64a7c0b14a8c36f72fcdcd0cf0b /shared
parent66e1614122c22d455cc3a28db08b3afac2b7be29 (diff)
downloadweston-38298ec0b45661d2969fc75c6cb47e0863755792.tar.gz
option-parser: Handle short double-arg options
weston allows both short and long style options to take arguments. In the case of short options, allow an optional space between the option name and value. E.g., previously you could launch weston this way: weston -i2 -cmyconfig.ini now you can also launch it like this: weston -i 2 -c myconfig.ini Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Diffstat (limited to 'shared')
-rw-r--r--shared/option-parser.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/shared/option-parser.c b/shared/option-parser.c
index f1cc5294..d5fee8ec 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -98,14 +98,37 @@ short_option(const struct weston_option *options, int count, char *arg)
return 1;
}
- } else {
+ } else if (arg[2]) {
return handle_option(options + k, arg + 2);
+ } else {
+ return 0;
}
}
return 0;
}
+static int
+short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
+{
+ int k;
+
+ if (!arg[1])
+ return 0;
+
+ for (k = 0; k < count; k++) {
+ if (options[k].short_name != arg[1])
+ continue;
+
+ if (options[k].type == WESTON_OPTION_BOOLEAN)
+ continue;
+
+ return handle_option(options + k, param);
+ }
+
+ return 0;
+}
+
int
parse_options(const struct weston_option *options,
int count, int *argc, char *argv[])
@@ -115,10 +138,22 @@ parse_options(const struct weston_option *options,
for (i = 1, j = 1; i < *argc; i++) {
if (argv[i][0] == '-') {
if (argv[i][1] == '-') {
+ /* Long option, e.g. --foo or --foo=bar */
if (long_option(options, count, argv[i]))
continue;
- } else if (short_option(options, count, argv[i]))
- continue;
+
+ } else {
+ /* Short option, e.g -f or -f42 */
+ if (short_option(options, count, argv[i]))
+ continue;
+
+ /* ...also handle -f 42 */
+ if (i+1 < *argc &&
+ short_option_with_arg(options, count, argv[i], argv[i+1])) {
+ i++;
+ continue;
+ }
+ }
}
argv[j++] = argv[i];
}