From 6b52f48b8f1cda8d428d3e4fe685bd28713f293f Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Thu, 31 Mar 2022 23:27:09 +0200 Subject: cli: add -v and -h shorthands Change the behavior of "git -v" to be synonymous with "--version" / "version", and "git -h" to be synonymous with "--help", but not "help". These shorthands both display the "unknown option" message. Following this change, "-v" displays the version, and "-h" displays the help text of the "git" command. It should be noted that the "-v" shorthand could be misinterpreted by the user to mean "verbose" instead of "version", since some sub-commands make use of it in this context. The top-level "git" command does not have a "verbose" flag, so it's safe to introduce this shorthand unambiguously. Signed-off-by: Garrit Franke Signed-off-by: Junio C Hamano --- git.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'git.c') diff --git a/git.c b/git.c index a25940d72e..8e1a90490e 100644 --- a/git.c +++ b/git.c @@ -25,7 +25,7 @@ struct cmd_struct { }; const char git_usage_string[] = - N_("git [--version] [--help] [-C ] [-c =]\n" + N_("git [-v | --version] [-h | --help] [-C ] [-c =]\n" " [--exec-path[=]] [--html-path] [--man-path] [--info-path]\n" " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n" " [--git-dir=] [--work-tree=] [--namespace=]\n" @@ -146,7 +146,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) * commands can be written with "--" prepended * to make them look like flags. */ - if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) + if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") || + !strcmp(cmd, "--version") || !strcmp(cmd, "-v")) break; /* @@ -892,8 +893,10 @@ int cmd_main(int argc, const char **argv) argc--; handle_options(&argv, &argc, NULL); if (argc > 0) { - /* translate --help and --version into commands */ - skip_prefix(argv[0], "--", &argv[0]); + if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0])) + argv[0] = "version"; + else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0])) + argv[0] = "help"; } else { /* The user didn't specify a command; give them help */ commit_pager_choice(); -- cgit v1.2.3