help: error if [-a|-g|-c] and [-i|-m|-w] are combined

Add more sanity checking to "git help" usage by erroring out if these man viewer options are combined with incompatible command-modes that will never use these documentation viewers. This continues the work started in d35d03cf93e (help: simplify by moving to OPT_CMDMODE(), 2021-09-22) of adding more sanity checking to "git help". Doing this allows us to clarify the "SYNOPSIS" in the documentation, and the "git help -h" output. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 21, 2022 at 20:38 UTC 503cddacb63b9a37d063de5b11ac188de822e06f
2 files changed +45 -6
builtin/help.c
+35 -6
@@ -574,12 +574,40 @@ static const char *check_git_cmd(const char* cmd)
574 return cmd;
575 }
576
577 -static void opt_mode_usage(int argc, const char *opt_mode)
577 +static void no_help_format(const char *opt_mode, enum help_format fmt)
578 +{
579 + const char *opt_fmt;
580 +
581 + switch (fmt) {
582 + case HELP_FORMAT_NONE:
583 + return;
584 + case HELP_FORMAT_MAN:
585 + opt_fmt = "--man";
586 + break;
587 + case HELP_FORMAT_INFO:
588 + opt_fmt = "--info";
589 + break;
590 + case HELP_FORMAT_WEB:
591 + opt_fmt = "--web";
592 + break;
593 + default:
594 + BUG("unreachable");
595 + }
596 +
597 + usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
598 + builtin_help_usage, builtin_help_options, opt_mode,
599 + opt_fmt);
600 +}
601 +
602 +static void opt_mode_usage(int argc, const char *opt_mode,
603 + enum help_format fmt)
604 {
605 if (argc)
606 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
607 builtin_help_usage, builtin_help_options,
608 opt_mode);
609 +
610 + no_help_format(opt_mode, fmt);
611 }
612
613 int cmd_help(int argc, const char **argv, const char *prefix)
@@ -594,7 +622,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
622
623 switch (cmd_mode) {
624 case HELP_ACTION_ALL:
597 - opt_mode_usage(argc, "--all");
625 + opt_mode_usage(argc, "--all", help_format);
626 if (verbose) {
627 setup_pager();
628 list_all_cmds_help();
@@ -606,20 +634,21 @@ int cmd_help(int argc, const char **argv, const char *prefix)
634 printf("%s\n", _(git_more_info_string));
635 break;
636 case HELP_ACTION_GUIDES:
609 - opt_mode_usage(argc, "--guides");
637 + opt_mode_usage(argc, "--guides", help_format);
638 list_guides_help();
639 printf("%s\n", _(git_more_info_string));
640 return 0;
641 case HELP_ACTION_CONFIG_FOR_COMPLETION:
614 - opt_mode_usage(argc, "--config-for-completion");
642 + opt_mode_usage(argc, "--config-for-completion", help_format);
643 list_config_help(SHOW_CONFIG_VARS);
644 return 0;
645 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
618 - opt_mode_usage(argc, "--config-sections-for-completion");
646 + opt_mode_usage(argc, "--config-sections-for-completion",
647 + help_format);
648 list_config_help(SHOW_CONFIG_SECTIONS);
649 return 0;
650 case HELP_ACTION_CONFIG:
622 - opt_mode_usage(argc, "--config");
651 + opt_mode_usage(argc, "--config", help_format);
652 setup_pager();
653 list_config_help(SHOW_CONFIG_HUMAN);
654 printf("\n%s\n", _("'git help config' for more information"));
t/t0012-help.sh
+10
@@ -49,6 +49,16 @@ test_expect_success 'invalid usage' '
49 test_expect_code 129 git help --config-sections-for-completion add
50 '
51
52 +for opt in '-a' '-g' '-c' '--config-for-completion' '--config-sections-for-completion'
53 +do
54 + test_expect_success "invalid usage of '$opt' with [-i|-m|-w]" '
55 + git help $opt &&
56 + test_expect_code 129 git help $opt -i &&
57 + test_expect_code 129 git help $opt -m &&
58 + test_expect_code 129 git help $opt -w
59 + '
60 +done
61 +
62 test_expect_success "works for commands and guides by default" '
63 configure_help &&
64 git help status &&