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

Quentin Glidic sardemff7+wayland at sardemff7.net
Fri Apr 26 03:16:42 PDT 2013


On 23/04/2013 17:05, Bill Spitzak wrote:
> 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").

I think I will just change the whole code to get a more consistent 
behaviour for all the mentioned cases.
I should add proper error checking too, to fail on missing value.

-- 

Quentin “Sardem FF7” Glidic


More information about the wayland-devel mailing list