diff options
author | Pierre Habouzit <madcoder@debian.org> | 2009-06-09 10:23:44 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-06-10 08:41:03 -0700 |
commit | cb9d398c3506a6354a1c63d265a4228fcec28fda (patch) | |
tree | 155555594e0ae8f770a0ecb966529cac5add097a | |
parent | 9a8531eeba5053281e14ef14e6beb019ce17f07e (diff) | |
download | git-cb9d398c3506a6354a1c63d265a4228fcec28fda.tar.gz |
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>
-rw-r--r-- | parse-options.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/parse-options.c b/parse-options.c index b85cab2466..79de18b37e 100644 --- a/parse-options.c +++ b/parse-options.c @@ -306,6 +306,28 @@ static void check_typos(const char *arg, const struct option *options) } } +static void parse_options_check(const struct option *opts) +{ + int err = 0; + + for (; opts->type != OPTION_END; opts++) { + if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && + (opts->flags & PARSE_OPT_OPTARG)) { + if (opts->long_name) { + error("`--%s` uses incompatible flags " + "LASTARG_DEFAULT and OPTARG", opts->long_name); + } else { + error("`-%c` uses incompatible flags " + "LASTARG_DEFAULT and OPTARG", opts->short_name); + } + err |= 1; + } + } + + if (err) + exit(129); +} + void parse_options_start(struct parse_opt_ctx_t *ctx, int argc, const char **argv, const char *prefix, int flags) @@ -331,6 +353,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx, { int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); + parse_options_check(options); + /* we must reset ->opt, unknown short option leave it dangling */ ctx->opt = NULL; |