[PATCH weston 1/6] shared/option-parser: Allow spaced options
Quentin Glidic
sardemff7+wayland at sardemff7.net
Tue Apr 23 05:54:45 PDT 2013
From: Quentin Glidic <sardemff7+git at sardemff7.net>
Signed-off-by: Quentin Glidic <sardemff7+git at sardemff7.net>
---
man/weston.man | 3 +++
shared/option-parser.c | 25 ++++++++++++++++---------
2 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/man/weston.man b/man/weston.man
index 39d854b..a25e619 100644
--- a/man/weston.man
+++ b/man/weston.man
@@ -92,6 +92,9 @@ and
.\" ***************************************************************
.SH OPTIONS
.
+You can specify short options having an argument with a following space. Long
+options with argument can be specified either with or without an equal sign.
+.
.SS Weston core options:
.TP
\fB\-\^B\fR\fIbackend.so\fR, \fB\-\-backend\fR=\fIbackend.so\fR
diff --git a/shared/option-parser.c b/shared/option-parser.c
index a7e497f..023fe72 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -22,28 +22,29 @@
#include <stdlib.h>
#include <stdint.h>
+#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "config-parser.h"
-static void
+static bool
handle_option(const struct weston_option *option, char *value)
{
switch (option->type) {
case WESTON_OPTION_INTEGER:
* (int32_t *) option->data = strtol(value, NULL, 0);
- return;
+ return true;
case WESTON_OPTION_UNSIGNED_INTEGER:
* (uint32_t *) option->data = strtoul(value, NULL, 0);
- return;
+ return true;
case WESTON_OPTION_STRING:
* (char **) option->data = strdup(value);
- return;
+ return true;
case WESTON_OPTION_BOOLEAN:
* (int32_t *) option->data = 1;
- return;
+ return false;
default:
assert(0);
}
@@ -62,14 +63,20 @@ parse_options(const struct weston_option *options,
if (options[k].name &&
argv[i][0] == '-' &&
argv[i][1] == '-' &&
- strncmp(options[k].name, &argv[i][2], len) == 0 &&
- (argv[i][len + 2] == '=' || argv[i][len + 2] == '\0')) {
- handle_option(&options[k], &argv[i][len + 3]);
+ strncmp(options[k].name, &argv[i][2], len) == 0) {
+
+ if (argv[i][len + 2] == '=')
+ handle_option(&options[k], &argv[i][len + 3]);
+ else if (handle_option(&options[k], argv[i+1]))
+ ++i;
break;
} else if (options[k].short_name &&
argv[i][0] == '-' &&
options[k].short_name == argv[i][1]) {
- handle_option(&options[k], &argv[i][2]);
+ if (argv[i][2] != '\0')
+ handle_option(&options[k], &argv[i][2]);
+ else if (handle_option(&options[k], argv[i+1]))
+ ++i;
break;
}
}
--
1.8.2.1
More information about the wayland-devel
mailing list