@matheusbsbs / matheus-bagliano / commits / 1e5ce570ca
Fork 0

parse-options: clearer reporting of API misuse

The PARSE_OPT_LASTARG_DEFAULT flag is meant for options like --contains that (1) traditionally had a mandatory argument and (2) have some better behavior to use when appearing in the final position. It makes no sense to combine this with OPTARG, so ever since v1.6.4-rc0~71 (parse-options: add parse_options_check to validate option specs, 2009-07-09) this mistake is flagged with error: `--option` uses incompatible flags LASTARG_DEFAULT and OPTARG and an exit status representing an error in commandline usage. Unfortunately that which might confuse scripters calling such an erroneous program into thinking the _script_ contains an error. Clarify that it is an internal error by dying with a message beginning "error: BUG: ..." and status 128. While at it, clean up parse_options_check to prepare for more checks. Long term, it would be nicer to make such checks happen at compile time. 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:01 UTC 1e5ce570ca368b97c8e3b238bb0228c5ca41b494
1 file changed +11 -12
parse-options.c
+11 -12
@@ -11,6 +11,13 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx,
11 #define OPT_SHORT 1
12 #define OPT_UNSET 2
13
14 +static int optbug(const struct option *opt, const char *reason)
15 +{
16 + if (opt->long_name)
17 + return error("BUG: option '%s' %s", opt->long_name, reason);
18 + return error("BUG: switch '%c' %s", opt->short_name, reason);
19 +}
20 +
21 static int opterror(const struct option *opt, const char *reason, int flags)
22 {
23 if (flags & OPT_SHORT)
@@ -320,20 +327,12 @@ static void parse_options_check(const struct option *opts)
327
328 for (; opts->type != OPTION_END; opts++) {
329 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
323 - (opts->flags & PARSE_OPT_OPTARG)) {
324 - if (opts->long_name) {
325 - error("`--%s` uses incompatible flags "
326 - "LASTARG_DEFAULT and OPTARG", opts->long_name);
327 - } else {
328 - error("`-%c` uses incompatible flags "
329 - "LASTARG_DEFAULT and OPTARG", opts->short_name);
330 - }
331 - err |= 1;
332 - }
330 + (opts->flags & PARSE_OPT_OPTARG))
331 + err |= optbug(opts, "uses incompatible flags "
332 + "LASTARG_DEFAULT and OPTARG");
333 }
334 -
334 if (err)
336 - exit(129);
335 + exit(128);
336 }
337
338 void parse_options_start(struct parse_opt_ctx_t *ctx,