Skip to content

Commit

Permalink
parseopt: add PARSE_OPT_NO_INTERNAL_HELP
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Mar 8, 2009
1 parent b5ce3a5 commit b92891f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options,
const char * const usagestr[])
{
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);

/* we must reset ->opt, unknown short option leave it dangling */
ctx->opt = NULL;

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

if (arg[1] != '-') {
ctx->opt = arg + 1;
if (*ctx->opt == 'h')
if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
switch (parse_short_opt(ctx, options)) {
case -1:
Expand All @@ -279,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
if (ctx->opt)
check_typos(arg + 1, options);
while (ctx->opt) {
if (*ctx->opt == 'h')
if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
switch (parse_short_opt(ctx, options)) {
case -1:
Expand All @@ -306,9 +308,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
break;
}

if (!strcmp(arg + 2, "help-all"))
if (internal_help && !strcmp(arg + 2, "help-all"))
return usage_with_options_internal(usagestr, options, 1);
if (!strcmp(arg + 2, "help"))
if (internal_help && !strcmp(arg + 2, "help"))
return parse_options_usage(usagestr, options);
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
Expand Down
1 change: 1 addition & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum parse_opt_flags {
PARSE_OPT_STOP_AT_NON_OPTION = 2,
PARSE_OPT_KEEP_ARGV0 = 4,
PARSE_OPT_KEEP_UNKNOWN = 8,
PARSE_OPT_NO_INTERNAL_HELP = 16,
};

enum parse_opt_option_flags {
Expand Down

0 comments on commit b92891f

Please sign in to comment.