parse-options: sanity check PARSE_OPT_NOARG flag
Some option types cannot use an argument --- boolean options that would set a bit or flag or increment a counter, for example. If configured in the flag word to accept an argument anyway, the result is an argument that is advertised in "program -h" output only to be rejected by parse-options::get_value. Luckily all current users of these option types use PARSE_OPT_NOARG and do not use PARSE_OPT_OPTARG. Add a check to ensure that that remains true. The check is run once for each invocation of parse_option_start(). Improved-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Dec 2, 2010 at 00:08 UTC
5c400ed2e05070d79b6cd9438ff5607ec0a83589
1 file changed
+13
parse-options.c
+13
@@ -330,6 +330,19 @@ static void parse_options_check(const struct option *opts)
330
opts->long_name))
331
err |= optbug(opts, "uses feature "
332
"not supported for dashless options");
333
+ switch (opts->type) {
334
+ case OPTION_BOOLEAN:
335
+ case OPTION_BIT:
336
+ case OPTION_NEGBIT:
337
+ case OPTION_SET_INT:
338
+ case OPTION_SET_PTR:
339
+ case OPTION_NUMBER:
340
+ if ((opts->flags & PARSE_OPT_OPTARG) ||
341
+ !(opts->flags & PARSE_OPT_NOARG))
342
+ err |= optbug(opts, "should not accept an argument");
343
+ default:
344
+ ; /* ok. (usually accepts an argument) */
345
+ }
346
}
347
if (err)
348
exit(128);