git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v2] cli: add -v and -h shorthands
@ 2022-03-30 19:09 Garrit Franke
  2022-03-30 21:53 ` Junio C Hamano
  2022-03-31 21:27 ` [PATCH v3] " Garrit Franke
  0 siblings, 2 replies; 11+ messages in thread
From: Garrit Franke @ 2022-03-30 19:09 UTC (permalink / raw)
  To: git; +Cc: Garrit Franke

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 <garrit@slashdev.space>
---

On Wed, Mar 30 2022, Ævar Arnfjörð Bjarmason wrote:

> On a second reading I see that there's an implied "change the behavior
> to ..." there.

Interesting, I haven't yet thought of it that way. I tried to be as
precise as possible this time. :)

> The translation files are updated by a process that the translation
> teams(s) use, see po/README.md. I think changing this from under them
> is probably more disruptive than not.

This doc slipped past me. Thanks!

> nit: minimize the diff here perhaps by keeping the existing line, and
> doing -h or -v on a new line? Your indentation here is also off.

Whoops, the indentation looked fine in my editor with a tab-width of
four. I'll make sure to review the patch more carefully next time!

> FWIW I have an existing branch (unsubmitted) at
> https://github.com/avar/git/tree/avar/parse-options-h-3 where I added
> some tests for "git cmd -h" behavior, which seems to pass with this
> change (not unexpected, as this is for the top-level command).

Do you think it makes sense to add tests for this behavior as well? I
originally refrained from adding them mainly due to lack of experience
in the project, but I'd be happy to add some if necessary.

Thanks
Garrit

 Documentation/git.txt |  4 +++-
 git.c                 | 12 +++++++++---
 t/t0012-help.sh       |  2 +-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/Documentation/git.txt b/Documentation/git.txt
index 13f83a2..302607a 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -9,7 +9,7 @@ git - the stupid content tracker
 SYNOPSIS
 --------
 [verse]
-'git' [--version] [--help] [-C <path>] [-c <name>=<value>]
+'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
     [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
     [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
     [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
@@ -39,6 +39,7 @@ or https://git-scm.com/docs.
 
 OPTIONS
 -------
+-v::
 --version::
 	Prints the Git suite version that the 'git' program came from.
 +
@@ -46,6 +47,7 @@ This option is internally converted to `git version ...` and accepts
 the same options as the linkgit:git-version[1] command. If `--help` is
 also given, it takes precedence over `--version`.
 
+-h::
 --help::
 	Prints the synopsis and a list of the most commonly used
 	commands. If the option `--all` or `-a` is given then all
diff --git a/git.c b/git.c
index a25940d..024d463 100644
--- a/git.c
+++ b/git.c
@@ -25,7 +25,7 @@ struct cmd_struct {
 };
 
 const char git_usage_string[] =
-	N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
+	N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
 	   "           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
 	   "           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
 	   "           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\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, "--version") ||
+		    !strcmp(cmd, "-h") || !strcmp(cmd, "-v"))
 			break;
 
 		/*
@@ -893,7 +894,12 @@ int cmd_main(int argc, const char **argv)
 	handle_options(&argv, &argc, NULL);
 	if (argc > 0) {
 		/* translate --help and --version into commands */
-		skip_prefix(argv[0], "--", &argv[0]);
+		if (!strcmp("-v", argv[0]))
+			argv[0] = "version";
+		else if (!strcmp("-h", argv[0]))
+			argv[0] = "help";
+		else
+			skip_prefix(argv[0], "--", &argv[0]);
 	} else {
 		/* The user didn't specify a command; give them help */
 		commit_pager_choice();
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
index 6c3e1f7..6c33a43 100755
--- a/t/t0012-help.sh
+++ b/t/t0012-help.sh
@@ -181,7 +181,7 @@ for cmd in git "git help"
 do
 	test_expect_success "'$cmd' section spacing" '
 		test_section_spacing_trailer git help <<-\EOF &&
-		usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
+		usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
 
 		These are common Git commands used in various situations:
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-04-04 21:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-30 19:09 [PATCH v2] cli: add -v and -h shorthands Garrit Franke
2022-03-30 21:53 ` Junio C Hamano
2022-03-30 22:50   ` Garrit Franke
2022-03-31  0:07     ` Ævar Arnfjörð Bjarmason
2022-03-31 13:08       ` Garrit Franke
2022-03-31 20:07         ` Junio C Hamano
2022-04-01  9:23           ` Ævar Arnfjörð Bjarmason
2022-04-01 16:02             ` Junio C Hamano
2022-04-04  7:18               ` Garrit Franke
2022-04-04 16:19                 ` Junio C Hamano
2022-03-31 21:27 ` [PATCH v3] " Garrit Franke

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).