Skip to content

Commit d35d03c

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a5940f commit d35d03c

File tree

2 files changed

+40
-47
lines changed

2 files changed

+40
-47
lines changed

builtin/help.c

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,36 @@ enum help_format {
3434
HELP_FORMAT_WEB
3535
};
3636

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;
3843

39-
static int show_all = 0;
40-
static int show_guides = 0;
41-
static int show_config;
44+
static const char *html_path;
4245
static int verbose = 1;
4346
static unsigned int colopts;
4447
static enum help_format help_format = HELP_FORMAT_NONE;
4548
static int exclude_guides;
4649
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),
4852
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),
5253
OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
5354
OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
5455
HELP_FORMAT_WEB),
5556
OPT_SET_INT('i', "info", &help_format, N_("show info page"),
5657
HELP_FORMAT_INFO),
5758
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+
5867
OPT_END(),
5968
};
6069

@@ -544,6 +553,13 @@ static const char *check_git_cmd(const char* cmd)
544553
return cmd;
545554
}
546555

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+
547563
int cmd_help(int argc, const char **argv, const char *prefix)
548564
{
549565
int nongit;
@@ -554,28 +570,8 @@ int cmd_help(int argc, const char **argv, const char *prefix)
554570
builtin_help_usage, 0);
555571
parsed_help_format = help_format;
556572

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:
579575
git_config(git_help_config, NULL);
580576
if (verbose) {
581577
setup_pager();
@@ -585,25 +581,20 @@ int cmd_help(int argc, const char **argv, const char *prefix)
585581
printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
586582
load_command_list("git-", &main_cmds, &other_cmds);
587583
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);
591588
list_guides_help();
592-
593-
if (show_all || show_guides) {
594589
printf("%s\n", _(git_more_info_string));
595590
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);
605596
setup_pager();
606-
list_config_help(for_human);
597+
list_config_help(1);
607598
printf("\n%s\n", _("'git help config' for more information"));
608599
return 0;
609600
}

parse-options.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ struct option {
169169
#define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
170170
#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
171171
(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+
174176
#define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
175177
#define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
176178
N_("n"), (h), PARSE_OPT_NONEG }

0 commit comments

Comments
 (0)