@matheusbsbs / matheus-bagliano / commits / cb9d398c35
Fork 0

parse-options: add parse_options_check to validate option specs.

It only searches for now for the dreaded LASTARG_DEFAULT | OPTARG combination, but can be extended to check for any other forbidden combination. Options are checked each time we call parse_options_start. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Jun 9, 2009 at 10:23 UTC cb9d398c3506a6354a1c63d265a4228fcec28fda
1 file changed +24
parse-options.c
+24
@@ -306,6 +306,28 @@ static void check_typos(const char *arg, const struct option *options)
306 }
307 }
308
309 +static void parse_options_check(const struct option *opts)
310 +{
311 + int err = 0;
312 +
313 + for (; opts->type != OPTION_END; opts++) {
314 + if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
315 + (opts->flags & PARSE_OPT_OPTARG)) {
316 + if (opts->long_name) {
317 + error("`--%s` uses incompatible flags "
318 + "LASTARG_DEFAULT and OPTARG", opts->long_name);
319 + } else {
320 + error("`-%c` uses incompatible flags "
321 + "LASTARG_DEFAULT and OPTARG", opts->short_name);
322 + }
323 + err |= 1;
324 + }
325 + }
326 +
327 + if (err)
328 + exit(129);
329 +}
330 +
331 void parse_options_start(struct parse_opt_ctx_t *ctx,
332 int argc, const char **argv, const char *prefix,
333 int flags)
@@ -331,6 +353,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
353 {
354 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
355
356 + parse_options_check(options);
357 +
358 /* we must reset ->opt, unknown short option leave it dangling */
359 ctx->opt = NULL;
360