Merge branch 'rs/parseopt-short-help'
Make "-h" command line option work more consistently in all commands. * rs/parseopt-short-help: show-ref: stop using PARSE_OPT_NO_INTERNAL_HELP grep: stop using PARSE_OPT_NO_INTERNAL_HELP parse-options: allow -h as a short option parse-options: inline parse_options_usage() at its only remaining caller parse-options: deduplicate parse_options_usage() calls
This commit is contained in:
commit
2d808073db
@ -612,11 +612,6 @@ static int pattern_callback(const struct option *opt, const char *arg,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int help_callback(const struct option *opt, const char *arg, int unset)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
int hit = 0;
|
||||
@ -738,18 +733,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
|
||||
OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored,
|
||||
N_("allow calling of grep(1) (ignored by this build)")),
|
||||
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"),
|
||||
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
/*
|
||||
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
|
||||
* to show usage information and exit.
|
||||
*/
|
||||
if (argc == 2 && !strcmp(argv[1], "-h"))
|
||||
usage_with_options(grep_usage, options);
|
||||
|
||||
init_grep_defaults();
|
||||
git_config(grep_cmd_config, NULL);
|
||||
grep_init(&opt, prefix);
|
||||
@ -766,8 +752,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
*/
|
||||
argc = parse_options(argc, argv, prefix, options, grep_usage,
|
||||
PARSE_OPT_KEEP_DASHDASH |
|
||||
PARSE_OPT_STOP_AT_NON_OPTION |
|
||||
PARSE_OPT_NO_INTERNAL_HELP);
|
||||
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||
grep_commit_pattern_type(pattern_type_arg, &opt);
|
||||
|
||||
if (use_index && !startup_info->have_repository)
|
||||
|
@ -161,11 +161,6 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int help_callback(const struct option *opt, const char *arg, int unset)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const struct option show_ref_options[] = {
|
||||
OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
|
||||
OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
|
||||
@ -186,18 +181,13 @@ static const struct option show_ref_options[] = {
|
||||
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
|
||||
N_("pattern"), N_("show refs from stdin that aren't in local repository"),
|
||||
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
|
||||
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"),
|
||||
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
if (argc == 2 && !strcmp(argv[1], "-h"))
|
||||
usage_with_options(show_ref_usage, show_ref_options);
|
||||
|
||||
argc = parse_options(argc, argv, prefix, show_ref_options,
|
||||
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
|
||||
show_ref_usage, 0);
|
||||
|
||||
if (exclude_arg)
|
||||
return exclude_existing(exclude_existing_arg);
|
||||
|
@ -5,10 +5,6 @@
|
||||
#include "color.h"
|
||||
#include "utf8.h"
|
||||
|
||||
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
|
||||
const char * const *usagestr,
|
||||
const struct option *opts, int err);
|
||||
|
||||
#define OPT_SHORT 1
|
||||
#define OPT_UNSET 2
|
||||
|
||||
@ -414,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;
|
||||
@ -435,6 +431,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
|
||||
const char * const usagestr[])
|
||||
{
|
||||
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
|
||||
int err = 0;
|
||||
|
||||
/* we must reset ->opt, unknown short option leave it dangling */
|
||||
ctx->opt = NULL;
|
||||
@ -451,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')
|
||||
return parse_options_usage(ctx, usagestr, options, 0);
|
||||
switch (parse_short_opt(ctx, options)) {
|
||||
case -1:
|
||||
return parse_options_usage(ctx, usagestr, options, 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')
|
||||
return parse_options_usage(ctx, usagestr, options, 0);
|
||||
switch (parse_short_opt(ctx, options)) {
|
||||
case -1:
|
||||
return parse_options_usage(ctx, usagestr, options, 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
|
||||
*
|
||||
@ -496,10 +498,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
|
||||
if (internal_help && !strcmp(arg + 2, "help-all"))
|
||||
return usage_with_options_internal(ctx, usagestr, options, 1, 0);
|
||||
if (internal_help && !strcmp(arg + 2, "help"))
|
||||
return parse_options_usage(ctx, usagestr, options, 0);
|
||||
goto show_usage;
|
||||
switch (parse_long_opt(ctx, arg + 2, options)) {
|
||||
case -1:
|
||||
return parse_options_usage(ctx, usagestr, options, 1);
|
||||
goto show_usage_error;
|
||||
case -2:
|
||||
goto unknown;
|
||||
}
|
||||
@ -511,6 +513,11 @@ unknown:
|
||||
ctx->opt = NULL;
|
||||
}
|
||||
return PARSE_OPT_DONE;
|
||||
|
||||
show_usage_error:
|
||||
err = 1;
|
||||
show_usage:
|
||||
return usage_with_options_internal(ctx, usagestr, options, 0, err);
|
||||
}
|
||||
|
||||
int parse_options_end(struct parse_opt_ctx_t *ctx)
|
||||
@ -656,13 +663,6 @@ void NORETURN usage_msg_opt(const char *msg,
|
||||
usage_with_options(usagestr, options);
|
||||
}
|
||||
|
||||
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
|
||||
const char * const *usagestr,
|
||||
const struct option *opts, int err)
|
||||
{
|
||||
return usage_with_options_internal(ctx, usagestr, opts, 0, err);
|
||||
}
|
||||
|
||||
#undef opterror
|
||||
int opterror(const struct option *opt, const char *reason, int flags)
|
||||
{
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user