parse-opt: do not print errors on unknown options, return -2 intead.
This way we can catch "unknown" options more easily. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Jun 23, 2008 at 22:46 UTC
07fe54db3cdf42500ac2e893b670fd74841afdc4
1 file changed
+30
-13
parse-options.c
+30
-13
@@ -94,14 +94,14 @@ static int get_value(struct parse_opt_ctx_t *p,
94
95
case OPTION_CALLBACK:
96
if (unset)
97
- return (*opt->callback)(opt, NULL, 1);
97
+ return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
98
if (opt->flags & PARSE_OPT_NOARG)
99
- return (*opt->callback)(opt, NULL, 0);
99
+ return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
100
if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
101
- return (*opt->callback)(opt, NULL, 0);
101
+ return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
102
if (!arg)
103
return opterror(opt, "requires a value", flags);
104
- return (*opt->callback)(opt, get_arg(p), 0);
104
+ return (*opt->callback)(opt, get_arg(p), 0) ? (-1) : 0;
105
106
case OPTION_INTEGER:
107
if (unset) {
@@ -132,7 +132,7 @@ static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *optio
132
return get_value(p, options, OPT_SHORT);
133
}
134
}
135
- return error("unknown switch `%c'", *p->opt);
135
+ return -2;
136
}
137
138
static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
@@ -217,7 +217,7 @@ is_abbreviated:
217
abbrev_option->long_name);
218
if (abbrev_option)
219
return get_value(p, abbrev_option, abbrev_flags);
220
- return error("unknown option `%s'", arg);
220
+ return -2;
221
}
222
223
void check_typos(const char *arg, const struct option *options)
@@ -271,15 +271,23 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
271
ctx->opt = arg + 1;
272
if (*ctx->opt == 'h')
273
return parse_options_usage(usagestr, options);
274
- if (parse_short_opt(ctx, options) < 0)
275
- usage_with_options(usagestr, options);
274
+ switch (parse_short_opt(ctx, options)) {
275
+ case -1:
276
+ return parse_options_usage(usagestr, options);
277
+ case -2:
278
+ return PARSE_OPT_UNKNOWN;
279
+ }
280
if (ctx->opt)
281
check_typos(arg + 1, options);
282
while (ctx->opt) {
283
if (*ctx->opt == 'h')
284
return parse_options_usage(usagestr, options);
281
- if (parse_short_opt(ctx, options) < 0)
282
- usage_with_options(usagestr, options);
285
+ switch (parse_short_opt(ctx, options)) {
286
+ case -1:
287
+ return parse_options_usage(usagestr, options);
288
+ case -2:
289
+ return PARSE_OPT_UNKNOWN;
290
+ }
291
}
292
continue;
293
}
@@ -296,8 +304,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
304
return usage_with_options_internal(usagestr, options, 1);
305
if (!strcmp(arg + 2, "help"))
306
return parse_options_usage(usagestr, options);
299
- if (parse_long_opt(ctx, arg + 2, options))
300
- usage_with_options(usagestr, options);
307
+ switch (parse_long_opt(ctx, arg + 2, options)) {
308
+ case -1:
309
+ return parse_options_usage(usagestr, options);
310
+ case -2:
311
+ return PARSE_OPT_UNKNOWN;
312
+ }
313
}
314
return PARSE_OPT_DONE;
315
}
@@ -321,7 +333,12 @@ int parse_options(int argc, const char **argv, const struct option *options,
333
case PARSE_OPT_DONE:
334
break;
335
default: /* PARSE_OPT_UNKNOWN */
324
- abort(); /* unreached yet */
336
+ if (ctx.argv[0][1] == '-') {
337
+ error("unknown option `%s'", ctx.argv[0] + 2);
338
+ } else {
339
+ error("unknown switch `%c'", *ctx.opt);
340
+ }
341
+ usage_with_options(usagestr, options);
342
}
343
344
return parse_options_end(&ctx);