summaryrefslogtreecommitdiff
path: root/parse-options.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2015-11-17 11:25:38 +0100
committerJeff King <peff@peff.net>2015-11-20 08:02:07 -0500
commit5ad0d3d5266b83aa767a7efa09cb574d3c2968b3 (patch)
tree005a6e86ca2eec0b0f3d158ef9884a512730fdb8 /parse-options.c
parentd3d1f8c46f9ff69de1b4d48d4e194b006ae17a90 (diff)
downloadgit-5ad0d3d5266b83aa767a7efa09cb574d3c2968b3.tar.gz
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>
Diffstat (limited to 'parse-options.c')
-rw-r--r--parse-options.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/parse-options.c b/parse-options.c
index 4863966fa5..47a9192060 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -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;
@@ -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
*