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;
|
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 cmd_grep(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
int hit = 0;
|
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 },
|
PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
|
||||||
OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored,
|
OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored,
|
||||||
N_("allow calling of grep(1) (ignored by this build)")),
|
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()
|
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();
|
init_grep_defaults();
|
||||||
git_config(grep_cmd_config, NULL);
|
git_config(grep_cmd_config, NULL);
|
||||||
grep_init(&opt, prefix);
|
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,
|
argc = parse_options(argc, argv, prefix, options, grep_usage,
|
||||||
PARSE_OPT_KEEP_DASHDASH |
|
PARSE_OPT_KEEP_DASHDASH |
|
||||||
PARSE_OPT_STOP_AT_NON_OPTION |
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||||
PARSE_OPT_NO_INTERNAL_HELP);
|
|
||||||
grep_commit_pattern_type(pattern_type_arg, &opt);
|
grep_commit_pattern_type(pattern_type_arg, &opt);
|
||||||
|
|
||||||
if (use_index && !startup_info->have_repository)
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int help_callback(const struct option *opt, const char *arg, int unset)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct option show_ref_options[] = {
|
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, "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)")),
|
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,
|
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
|
||||||
N_("pattern"), N_("show refs from stdin that aren't in local repository"),
|
N_("pattern"), N_("show refs from stdin that aren't in local repository"),
|
||||||
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
|
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()
|
OPT_END()
|
||||||
};
|
};
|
||||||
|
|
||||||
int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
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,
|
argc = parse_options(argc, argv, prefix, show_ref_options,
|
||||||
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
|
show_ref_usage, 0);
|
||||||
|
|
||||||
if (exclude_arg)
|
if (exclude_arg)
|
||||||
return exclude_existing(exclude_existing_arg);
|
return exclude_existing(exclude_existing_arg);
|
||||||
|
@ -5,10 +5,6 @@
|
|||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "utf8.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_SHORT 1
|
||||||
#define OPT_UNSET 2
|
#define OPT_UNSET 2
|
||||||
|
|
||||||
@ -414,7 +410,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
|
|||||||
const struct option *options, int flags)
|
const struct option *options, int flags)
|
||||||
{
|
{
|
||||||
memset(ctx, 0, sizeof(*ctx));
|
memset(ctx, 0, sizeof(*ctx));
|
||||||
ctx->argc = argc - 1;
|
ctx->argc = ctx->total = argc - 1;
|
||||||
ctx->argv = argv + 1;
|
ctx->argv = argv + 1;
|
||||||
ctx->out = argv;
|
ctx->out = argv;
|
||||||
ctx->prefix = prefix;
|
ctx->prefix = prefix;
|
||||||
@ -435,6 +431,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
|
|||||||
const char * const usagestr[])
|
const char * const usagestr[])
|
||||||
{
|
{
|
||||||
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
|
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
|
||||||
|
int err = 0;
|
||||||
|
|
||||||
/* we must reset ->opt, unknown short option leave it dangling */
|
/* we must reset ->opt, unknown short option leave it dangling */
|
||||||
ctx->opt = NULL;
|
ctx->opt = NULL;
|
||||||
@ -451,27 +448,32 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* lone -h asks for help */
|
||||||
|
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
|
||||||
|
goto show_usage;
|
||||||
|
|
||||||
if (arg[1] != '-') {
|
if (arg[1] != '-') {
|
||||||
ctx->opt = 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)) {
|
switch (parse_short_opt(ctx, options)) {
|
||||||
case -1:
|
case -1:
|
||||||
return parse_options_usage(ctx, usagestr, options, 1);
|
goto show_usage_error;
|
||||||
case -2:
|
case -2:
|
||||||
if (ctx->opt)
|
if (ctx->opt)
|
||||||
check_typos(arg + 1, options);
|
check_typos(arg + 1, options);
|
||||||
|
if (internal_help && *ctx->opt == 'h')
|
||||||
|
goto show_usage;
|
||||||
goto unknown;
|
goto unknown;
|
||||||
}
|
}
|
||||||
if (ctx->opt)
|
if (ctx->opt)
|
||||||
check_typos(arg + 1, options);
|
check_typos(arg + 1, options);
|
||||||
while (ctx->opt) {
|
while (ctx->opt) {
|
||||||
if (internal_help && *ctx->opt == 'h')
|
|
||||||
return parse_options_usage(ctx, usagestr, options, 0);
|
|
||||||
switch (parse_short_opt(ctx, options)) {
|
switch (parse_short_opt(ctx, options)) {
|
||||||
case -1:
|
case -1:
|
||||||
return parse_options_usage(ctx, usagestr, options, 1);
|
goto show_usage_error;
|
||||||
case -2:
|
case -2:
|
||||||
|
if (internal_help && *ctx->opt == 'h')
|
||||||
|
goto show_usage;
|
||||||
|
|
||||||
/* fake a short option thing to hide the fact that we may have
|
/* fake a short option thing to hide the fact that we may have
|
||||||
* started to parse aggregated stuff
|
* 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"))
|
if (internal_help && !strcmp(arg + 2, "help-all"))
|
||||||
return usage_with_options_internal(ctx, usagestr, options, 1, 0);
|
return usage_with_options_internal(ctx, usagestr, options, 1, 0);
|
||||||
if (internal_help && !strcmp(arg + 2, "help"))
|
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)) {
|
switch (parse_long_opt(ctx, arg + 2, options)) {
|
||||||
case -1:
|
case -1:
|
||||||
return parse_options_usage(ctx, usagestr, options, 1);
|
goto show_usage_error;
|
||||||
case -2:
|
case -2:
|
||||||
goto unknown;
|
goto unknown;
|
||||||
}
|
}
|
||||||
@ -511,6 +513,11 @@ unknown:
|
|||||||
ctx->opt = NULL;
|
ctx->opt = NULL;
|
||||||
}
|
}
|
||||||
return PARSE_OPT_DONE;
|
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)
|
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);
|
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
|
#undef opterror
|
||||||
int opterror(const struct option *opt, const char *reason, int flags)
|
int opterror(const struct option *opt, const char *reason, int flags)
|
||||||
{
|
{
|
||||||
|
@ -199,7 +199,7 @@ enum {
|
|||||||
struct parse_opt_ctx_t {
|
struct parse_opt_ctx_t {
|
||||||
const char **argv;
|
const char **argv;
|
||||||
const char **out;
|
const char **out;
|
||||||
int argc, cpidx;
|
int argc, cpidx, total;
|
||||||
const char *opt;
|
const char *opt;
|
||||||
int flags;
|
int flags;
|
||||||
const char *prefix;
|
const char *prefix;
|
||||||
|
Loading…
Reference in New Issue
Block a user