Skip to content

Commit 2d80807

Browse files
committed
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
2 parents c69d08d + 42fdf86 commit 2d80807

File tree

4 files changed

+23
-48
lines changed

4 files changed

+23
-48
lines changed

builtin/grep.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,6 @@ static int pattern_callback(const struct option *opt, const char *arg,
612612
return 0;
613613
}
614614

615-
static int help_callback(const struct option *opt, const char *arg, int unset)
616-
{
617-
return -1;
618-
}
619-
620615
int cmd_grep(int argc, const char **argv, const char *prefix)
621616
{
622617
int hit = 0;
@@ -738,18 +733,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
738733
PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
739734
OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored,
740735
N_("allow calling of grep(1) (ignored by this build)")),
741-
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"),
742-
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
743736
OPT_END()
744737
};
745738

746-
/*
747-
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
748-
* to show usage information and exit.
749-
*/
750-
if (argc == 2 && !strcmp(argv[1], "-h"))
751-
usage_with_options(grep_usage, options);
752-
753739
init_grep_defaults();
754740
git_config(grep_cmd_config, NULL);
755741
grep_init(&opt, prefix);
@@ -766,8 +752,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
766752
*/
767753
argc = parse_options(argc, argv, prefix, options, grep_usage,
768754
PARSE_OPT_KEEP_DASHDASH |
769-
PARSE_OPT_STOP_AT_NON_OPTION |
770-
PARSE_OPT_NO_INTERNAL_HELP);
755+
PARSE_OPT_STOP_AT_NON_OPTION);
771756
grep_commit_pattern_type(pattern_type_arg, &opt);
772757

773758
if (use_index && !startup_info->have_repository)

builtin/show-ref.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
161161
return 0;
162162
}
163163

164-
static int help_callback(const struct option *opt, const char *arg, int unset)
165-
{
166-
return -1;
167-
}
168-
169164
static const struct option show_ref_options[] = {
170165
OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
171166
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[] = {
186181
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
187182
N_("pattern"), N_("show refs from stdin that aren't in local repository"),
188183
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
189-
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"),
190-
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
191184
OPT_END()
192185
};
193186

194187
int cmd_show_ref(int argc, const char **argv, const char *prefix)
195188
{
196-
if (argc == 2 && !strcmp(argv[1], "-h"))
197-
usage_with_options(show_ref_usage, show_ref_options);
198-
199189
argc = parse_options(argc, argv, prefix, show_ref_options,
200-
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
190+
show_ref_usage, 0);
201191

202192
if (exclude_arg)
203193
return exclude_existing(exclude_existing_arg);

parse-options.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include "color.h"
66
#include "utf8.h"
77

8-
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9-
const char * const *usagestr,
10-
const struct option *opts, int err);
11-
128
#define OPT_SHORT 1
139
#define OPT_UNSET 2
1410

@@ -414,7 +410,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
414410
const struct option *options, int flags)
415411
{
416412
memset(ctx, 0, sizeof(*ctx));
417-
ctx->argc = argc - 1;
413+
ctx->argc = ctx->total = argc - 1;
418414
ctx->argv = argv + 1;
419415
ctx->out = argv;
420416
ctx->prefix = prefix;
@@ -435,6 +431,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
435431
const char * const usagestr[])
436432
{
437433
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
434+
int err = 0;
438435

439436
/* we must reset ->opt, unknown short option leave it dangling */
440437
ctx->opt = NULL;
@@ -451,27 +448,32 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
451448
continue;
452449
}
453450

451+
/* lone -h asks for help */
452+
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
453+
goto show_usage;
454+
454455
if (arg[1] != '-') {
455456
ctx->opt = arg + 1;
456-
if (internal_help && *ctx->opt == 'h')
457-
return parse_options_usage(ctx, usagestr, options, 0);
458457
switch (parse_short_opt(ctx, options)) {
459458
case -1:
460-
return parse_options_usage(ctx, usagestr, options, 1);
459+
goto show_usage_error;
461460
case -2:
462461
if (ctx->opt)
463462
check_typos(arg + 1, options);
463+
if (internal_help && *ctx->opt == 'h')
464+
goto show_usage;
464465
goto unknown;
465466
}
466467
if (ctx->opt)
467468
check_typos(arg + 1, options);
468469
while (ctx->opt) {
469-
if (internal_help && *ctx->opt == 'h')
470-
return parse_options_usage(ctx, usagestr, options, 0);
471470
switch (parse_short_opt(ctx, options)) {
472471
case -1:
473-
return parse_options_usage(ctx, usagestr, options, 1);
472+
goto show_usage_error;
474473
case -2:
474+
if (internal_help && *ctx->opt == 'h')
475+
goto show_usage;
476+
475477
/* fake a short option thing to hide the fact that we may have
476478
* started to parse aggregated stuff
477479
*
@@ -496,10 +498,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
496498
if (internal_help && !strcmp(arg + 2, "help-all"))
497499
return usage_with_options_internal(ctx, usagestr, options, 1, 0);
498500
if (internal_help && !strcmp(arg + 2, "help"))
499-
return parse_options_usage(ctx, usagestr, options, 0);
501+
goto show_usage;
500502
switch (parse_long_opt(ctx, arg + 2, options)) {
501503
case -1:
502-
return parse_options_usage(ctx, usagestr, options, 1);
504+
goto show_usage_error;
503505
case -2:
504506
goto unknown;
505507
}
@@ -511,6 +513,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
511513
ctx->opt = NULL;
512514
}
513515
return PARSE_OPT_DONE;
516+
517+
show_usage_error:
518+
err = 1;
519+
show_usage:
520+
return usage_with_options_internal(ctx, usagestr, options, 0, err);
514521
}
515522

516523
int parse_options_end(struct parse_opt_ctx_t *ctx)
@@ -656,13 +663,6 @@ void NORETURN usage_msg_opt(const char *msg,
656663
usage_with_options(usagestr, options);
657664
}
658665

659-
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
660-
const char * const *usagestr,
661-
const struct option *opts, int err)
662-
{
663-
return usage_with_options_internal(ctx, usagestr, opts, 0, err);
664-
}
665-
666666
#undef opterror
667667
int opterror(const struct option *opt, const char *reason, int flags)
668668
{

parse-options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ enum {
199199
struct parse_opt_ctx_t {
200200
const char **argv;
201201
const char **out;
202-
int argc, cpidx;
202+
int argc, cpidx, total;
203203
const char *opt;
204204
int flags;
205205
const char *prefix;

0 commit comments

Comments
 (0)