Skip to content

Commit b92891f

Browse files
René Scharfegitster
authored andcommitted
parseopt: add PARSE_OPT_NO_INTERNAL_HELP
Add a parseopt flag, PARSE_OPT_NO_INTERNAL_HELP, that turns off internal handling of -h, --help and --help-all. This allows the implementation of custom help option handlers or incremental parsers. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5ce3a5 commit b92891f

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

parse-options.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
253253
const struct option *options,
254254
const char * const usagestr[])
255255
{
256+
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
257+
256258
/* we must reset ->opt, unknown short option leave it dangling */
257259
ctx->opt = NULL;
258260

@@ -268,7 +270,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
268270

269271
if (arg[1] != '-') {
270272
ctx->opt = arg + 1;
271-
if (*ctx->opt == 'h')
273+
if (internal_help && *ctx->opt == 'h')
272274
return parse_options_usage(usagestr, options);
273275
switch (parse_short_opt(ctx, options)) {
274276
case -1:
@@ -279,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
279281
if (ctx->opt)
280282
check_typos(arg + 1, options);
281283
while (ctx->opt) {
282-
if (*ctx->opt == 'h')
284+
if (internal_help && *ctx->opt == 'h')
283285
return parse_options_usage(usagestr, options);
284286
switch (parse_short_opt(ctx, options)) {
285287
case -1:
@@ -306,9 +308,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
306308
break;
307309
}
308310

309-
if (!strcmp(arg + 2, "help-all"))
311+
if (internal_help && !strcmp(arg + 2, "help-all"))
310312
return usage_with_options_internal(usagestr, options, 1);
311-
if (!strcmp(arg + 2, "help"))
313+
if (internal_help && !strcmp(arg + 2, "help"))
312314
return parse_options_usage(usagestr, options);
313315
switch (parse_long_opt(ctx, arg + 2, options)) {
314316
case -1:

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum parse_opt_flags {
2222
PARSE_OPT_STOP_AT_NON_OPTION = 2,
2323
PARSE_OPT_KEEP_ARGV0 = 4,
2424
PARSE_OPT_KEEP_UNKNOWN = 8,
25+
PARSE_OPT_NO_INTERNAL_HELP = 16,
2526
};
2627

2728
enum parse_opt_option_flags {

0 commit comments

Comments
 (0)