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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Mar 8, 2009 at 19:15 UTC
b92891f9783ae197bb84b90d8404ad08c3875fa1
2 files changed
+7
-4
parse-options.c
+6
-4
@@ -253,6 +253,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
253
const struct option *options,
254
const char * const usagestr[])
255
{
256
+ int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
257
+
258
/* we must reset ->opt, unknown short option leave it dangling */
259
ctx->opt = NULL;
260
@@ -268,7 +270,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
270
271
if (arg[1] != '-') {
272
ctx->opt = arg + 1;
271
- if (*ctx->opt == 'h')
273
+ if (internal_help && *ctx->opt == 'h')
274
return parse_options_usage(usagestr, options);
275
switch (parse_short_opt(ctx, options)) {
276
case -1:
@@ -279,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
281
if (ctx->opt)
282
check_typos(arg + 1, options);
283
while (ctx->opt) {
282
- if (*ctx->opt == 'h')
284
+ if (internal_help && *ctx->opt == 'h')
285
return parse_options_usage(usagestr, options);
286
switch (parse_short_opt(ctx, options)) {
287
case -1:
@@ -306,9 +308,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
308
break;
309
}
310
309
- if (!strcmp(arg + 2, "help-all"))
311
+ if (internal_help && !strcmp(arg + 2, "help-all"))
312
return usage_with_options_internal(usagestr, options, 1);
311
- if (!strcmp(arg + 2, "help"))
313
+ if (internal_help && !strcmp(arg + 2, "help"))
314
return parse_options_usage(usagestr, options);
315
switch (parse_long_opt(ctx, arg + 2, options)) {
316
case -1:
parse-options.h
+1
@@ -22,6 +22,7 @@ enum parse_opt_flags {
22
PARSE_OPT_STOP_AT_NON_OPTION = 2,
23
PARSE_OPT_KEEP_ARGV0 = 4,
24
PARSE_OPT_KEEP_UNKNOWN = 8,
25
+ PARSE_OPT_NO_INTERNAL_HELP = 16,
26
};
27
28
enum parse_opt_option_flags {