Skip to content

Commit cb9d398

Browse files
MadCodergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a8531e commit cb9d398

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

parse-options.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,28 @@ static void check_typos(const char *arg, const struct option *options)
306306
}
307307
}
308308

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+
309331
void parse_options_start(struct parse_opt_ctx_t *ctx,
310332
int argc, const char **argv, const char *prefix,
311333
int flags)
@@ -331,6 +353,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
331353
{
332354
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
333355

356+
parse_options_check(options);
357+
334358
/* we must reset ->opt, unknown short option leave it dangling */
335359
ctx->opt = NULL;
336360

0 commit comments

Comments
 (0)