[PATCH weston 1/6] shared/option-parser: Allow spaced options

Bill Spitzak spitzak at gmail.com
Tue Apr 23 08:05:36 PDT 2013


On 04/23/2013 05:54 AM, Quentin Glidic wrote:

> +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.

> -static void
> +static bool
>   handle_option(const struct weston_option *option, char *value)
>   {

This can be called with NULL from the code below (if the option is last 
on in argv with no argument after it).

> @@ -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]);

You need to check if argv[i][len + 2] == 0, if not then the option did 
not match.

> +				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]);

If this returns false then it should continue parsing the next letter as 
another single-letter option.

> +				else if (handle_option(&options[k], argv[i+1]))

I think you should also make '=' work (ie "-x=12").



More information about the wayland-devel mailing list