From ac20ff6daa818b3dad99368dfbc123dcf81e6824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 17 Nov 2015 11:25:14 +0100 Subject: parse-options: deduplicate parse_options_usage() calls Avoid long lines and repeating parse_options_usage() calls with their duplicate parameters by providing labels with speaking names to jump to. Signed-off-by: Rene Scharfe Signed-off-by: Jeff King diff --git a/parse-options.c b/parse-options.c index 3eceba4..f0b6d9b 100644 --- a/parse-options.c +++ b/parse-options.c @@ -435,6 +435,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; @@ -454,10 +455,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx, if (arg[1] != '-') { ctx->opt = arg + 1; if (internal_help && *ctx->opt == 'h') - return parse_options_usage(ctx, usagestr, options, 0); + goto show_usage; 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); @@ -467,10 +468,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx, check_typos(arg + 1, options); while (ctx->opt) { if (internal_help && *ctx->opt == 'h') - return parse_options_usage(ctx, usagestr, options, 0); + goto show_usage; switch (parse_short_opt(ctx, options)) { case -1: - return parse_options_usage(ctx, usagestr, options, 1); + goto show_usage_error; case -2: /* fake a short option thing to hide the fact that we may have * started to parse aggregated stuff @@ -496,10 +497,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 +512,11 @@ unknown: ctx->opt = NULL; } return PARSE_OPT_DONE; + + show_usage_error: + err = 1; + show_usage: + return parse_options_usage(ctx, usagestr, options, err); } int parse_options_end(struct parse_opt_ctx_t *ctx) -- cgit v0.10.2-6-g49f6 From d3d1f8c46f9ff69de1b4d48d4e194b006ae17a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 17 Nov 2015 11:25:27 +0100 Subject: parse-options: inline parse_options_usage() at its only remaining caller Signed-off-by: Rene Scharfe Signed-off-by: Jeff King diff --git a/parse-options.c b/parse-options.c index f0b6d9b..4863966 100644 --- a/parse-options.c +++ b/parse-options.c @@ -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 @@ -516,7 +512,7 @@ unknown: show_usage_error: err = 1; show_usage: - return parse_options_usage(ctx, usagestr, options, err); + return usage_with_options_internal(ctx, usagestr, options, 0, err); } int parse_options_end(struct parse_opt_ctx_t *ctx) @@ -662,13 +658,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) { -- cgit v0.10.2-6-g49f6 From 5ad0d3d5266b83aa767a7efa09cb574d3c2968b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 17 Nov 2015 11:25:38 +0100 Subject: 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 Signed-off-by: Jeff King diff --git a/parse-options.c b/parse-options.c index 4863966..47a9192 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 * diff --git a/parse-options.h b/parse-options.h index e8b55ea..ea4af92 100644 --- a/parse-options.h +++ b/parse-options.h @@ -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; -- cgit v0.10.2-6-g49f6 From 4441549995d796084526a5ed6a6c9add7af6c665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 17 Nov 2015 11:25:53 +0100 Subject: grep: stop using PARSE_OPT_NO_INTERNAL_HELP The flag PARSE_OPT_NO_INTERNAL_HELP is set to allow overriding the option -h, except when it's the only one given. This is the default behavior now, so remove the flag and the hand-rolled --help-all handling. The internal --help-all handler now actually shows hidden options, i.e. --debug in this case. Signed-off-by: Rene Scharfe Signed-off-by: Jeff King diff --git a/builtin/grep.c b/builtin/grep.c index d04f440..749e346 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -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 ', 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) -- cgit v0.10.2-6-g49f6 From 42fdf86e5f6ecf675e973e3e4a08952c8b1fd4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 17 Nov 2015 11:26:05 +0100 Subject: show-ref: stop using PARSE_OPT_NO_INTERNAL_HELP The flag PARSE_OPT_NO_INTERNAL_HELP is set to allow overriding the option -h, except when it's the only one given. This is the default behavior now, so remove the flag and the hand-rolled --help-all handling. The internal --help-all handler now actually shows hidden options, i.e. -h in this case. Signed-off-by: Rene Scharfe Signed-off-by: Jeff King diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 264c392..6d4e669 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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); -- cgit v0.10.2-6-g49f6