Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
parse-options: allow -h as a short option
Let callers provide their own handler for the short option -h even
without the flag PARSE_OPT_NO_INTERNAL_HELP, but call the internal
handler (showing usage information) if that is the only parameter.
Implement the first part by checking for -h only if parse_short_opt()
can't find it and returns -2.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
  • Loading branch information
René Scharfe authored and Jeff King committed Nov 20, 2015
1 parent d3d1f8c commit 5ad0d3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions parse-options.c
Expand Up @@ -410,7 +410,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
const struct option *options, int flags)
{
memset(ctx, 0, sizeof(*ctx));
ctx->argc = argc - 1;
ctx->argc = ctx->total = argc - 1;
ctx->argv = argv + 1;
ctx->out = argv;
ctx->prefix = prefix;
Expand Down Expand Up @@ -448,27 +448,32 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
continue;
}

/* lone -h asks for help */
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
goto show_usage;

if (arg[1] != '-') {
ctx->opt = arg + 1;
if (internal_help && *ctx->opt == 'h')
goto show_usage;
switch (parse_short_opt(ctx, options)) {
case -1:
goto show_usage_error;
case -2:
if (ctx->opt)
check_typos(arg + 1, options);
if (internal_help && *ctx->opt == 'h')
goto show_usage;
goto unknown;
}
if (ctx->opt)
check_typos(arg + 1, options);
while (ctx->opt) {
if (internal_help && *ctx->opt == 'h')
goto show_usage;
switch (parse_short_opt(ctx, options)) {
case -1:
goto show_usage_error;
case -2:
if (internal_help && *ctx->opt == 'h')
goto show_usage;

/* fake a short option thing to hide the fact that we may have
* started to parse aggregated stuff
*
Expand Down
2 changes: 1 addition & 1 deletion parse-options.h
Expand Up @@ -199,7 +199,7 @@ enum {
struct parse_opt_ctx_t {
const char **argv;
const char **out;
int argc, cpidx;
int argc, cpidx, total;
const char *opt;
int flags;
const char *prefix;
Expand Down

0 comments on commit 5ad0d3d

Please sign in to comment.