help: simplify by moving to OPT_CMDMODE()

As preceding commits have incrementally established all of the --all, --guides, --config and hidden --config-for-completion options are mutually exclusive. So let's use OPT_CMDMODE() to parse the command-line instead, and take advantage of its conflicting options detection. This is the first command with a hidden CMDMODE, so let's introduce a OPT_CMDMODE_F() macro to go along with OPT_CMDMODE(). I think this makes the usage information that we emit slightly worse, e.g. before we'd emit: $ git help --all --config fatal: --config and --all cannot be combined usage: git help [-a|--all] [--[no-]verbose]] [[-i|--info] [-m|--man] [-w|--web]] [<command>] or: git help [-g|--guides] or: git help [-c|--config] [...] $ And now: $ git help --all --config error: option `config' is incompatible with --all $ But improving that is a general topic for parse-options.c improvement, i.e. we should probably emit the full usage in that case. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 22, 2021 at 00:40 UTC d35d03cf93ef0dba3e975c78fce73db91d52ba42
2 files changed +40 -47
builtin/help.c
+36 -45
@@ -34,27 +34,36 @@ enum help_format {
34 HELP_FORMAT_WEB
35 };
36
37 -static const char *html_path;
37 +static enum help_action {
38 + HELP_ACTION_ALL = 1,
39 + HELP_ACTION_GUIDES,
40 + HELP_ACTION_CONFIG,
41 + HELP_ACTION_CONFIG_FOR_COMPLETION,
42 +} cmd_mode;
43
39 -static int show_all = 0;
40 -static int show_guides = 0;
41 -static int show_config;
44 +static const char *html_path;
45 static int verbose = 1;
46 static unsigned int colopts;
47 static enum help_format help_format = HELP_FORMAT_NONE;
48 static int exclude_guides;
49 static struct option builtin_help_options[] = {
47 - OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
50 + OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
51 + HELP_ACTION_ALL),
52 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
49 - OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
50 - OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
51 - OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
53 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
54 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
55 HELP_FORMAT_WEB),
56 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
57 HELP_FORMAT_INFO),
58 OPT__VERBOSE(&verbose, N_("print command description")),
59 +
60 + OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"),
61 + HELP_ACTION_GUIDES),
62 + OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"),
63 + HELP_ACTION_CONFIG),
64 + OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "",
65 + HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN),
66 +
67 OPT_END(),
68 };
69
@@ -544,6 +553,13 @@ static const char *check_git_cmd(const char* cmd)
553 return cmd;
554 }
555
556 +static void no_extra_argc(int argc)
557 +{
558 + if (argc)
559 + usage_msg_opt(_("this option doesn't take any other arguments"),
560 + builtin_help_usage, builtin_help_options);
561 +}
562 +
563 int cmd_help(int argc, const char **argv, const char *prefix)
564 {
565 int nongit;
@@ -554,28 +570,8 @@ int cmd_help(int argc, const char **argv, const char *prefix)
570 builtin_help_usage, 0);
571 parsed_help_format = help_format;
572
557 - /* Incompatible options */
558 - if (show_all && show_config)
559 - usage_msg_opt(_("--config and --all cannot be combined"),
560 - builtin_help_usage, builtin_help_options);
561 -
562 - if (show_all && show_guides)
563 - usage_msg_opt(_("--config and --guides cannot be combined"),
564 - builtin_help_usage, builtin_help_options);
565 -
566 - if (show_config && show_guides)
567 - usage_msg_opt(_("--config and --guides cannot be combined"),
568 - builtin_help_usage, builtin_help_options);
569 -
570 - /* Options that take no further arguments */
571 - if (argc && show_config)
572 - usage_msg_opt(_("--config cannot be combined with command or guide names"),
573 - builtin_help_usage, builtin_help_options);
574 - if (argc && show_guides)
575 - usage_msg_opt(_("--guides cannot be combined with command or guide names"),
576 - builtin_help_usage, builtin_help_options);
577 -
578 - if (show_all) {
573 + switch (cmd_mode) {
574 + case HELP_ACTION_ALL:
575 git_config(git_help_config, NULL);
576 if (verbose) {
577 setup_pager();
@@ -585,25 +581,20 @@ int cmd_help(int argc, const char **argv, const char *prefix)
581 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
582 load_command_list("git-", &main_cmds, &other_cmds);
583 list_commands(colopts, &main_cmds, &other_cmds);
588 - }
589 -
590 - if (show_guides)
584 + printf("%s\n", _(git_more_info_string));
585 + break;
586 + case HELP_ACTION_GUIDES:
587 + no_extra_argc(argc);
588 list_guides_help();
592 -
593 - if (show_all || show_guides) {
589 printf("%s\n", _(git_more_info_string));
590 return 0;
596 - }
597 -
598 - if (show_config) {
599 - int for_human = show_config == 1;
600 -
601 - if (!for_human) {
602 - list_config_help(for_human);
603 - return 0;
604 - }
591 + case HELP_ACTION_CONFIG_FOR_COMPLETION:
592 + list_config_help(0);
593 + return 0;
594 + case HELP_ACTION_CONFIG:
595 + no_extra_argc(argc);
596 setup_pager();
606 - list_config_help(for_human);
597 + list_config_help(1);
598 printf("\n%s\n", _("'git help config' for more information"));
599 return 0;
600 }
parse-options.h
+4 -2
@@ -169,8 +169,10 @@ struct option {
169 #define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
170 #define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
171 (h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
172 -#define OPT_CMDMODE(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
173 - (h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
172 +#define OPT_CMDMODE_F(s, l, v, h, i, f) { OPTION_SET_INT, (s), (l), (v), NULL, \
173 + (h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), NULL, (i) }
174 +#define OPT_CMDMODE(s, l, v, h, i) OPT_CMDMODE_F(s, l, v, h, i, 0)
175 +
176 #define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
177 #define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
178 N_("n"), (h), PARSE_OPT_NONEG }