git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Garrit Franke <garrit@slashdev.space>
To: git@vger.kernel.org
Cc: "Garrit Franke" <garrit@slashdev.space>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: [PATCH v3] cli: add -v and -h shorthands
Date: Thu, 31 Mar 2022 23:27:09 +0200	[thread overview]
Message-ID: <20220331212709.36036-1-garrit@slashdev.space> (raw)
In-Reply-To: <20220330190956.21447-1-garrit@slashdev.space>

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 31.03.22 22:07, Junio C Hamano wrote:
>> Not that I'm a fan of this clutter, but it's a possible path to go
>> down if we actually needed a second shorthand using this letter.
> 
> Do you mean you want to use "-V" for version, instead of the "-v"
> used in the patch, so that "-v" can be left for "--verbose"?
> 
> I am not sure consistency with whom we are aiming for anymore with
> that mixed to the proposal X-<.

I think I put this in a wrong way. I don't advocate for curls UI and
don't think we should mimic it here. It's just a thing to consider if we
ever find ourselves in this situation. Using the lowercase "-v" for
"version" as proposed by this patch makes the most sense IMO. If we ever
need a shorthand for "verbose", "-V" could be the way to go.

>      (Side note) A tip for reviewers.  Be suspicious of any change
>      that adds new things _in front_ of existing sequence.  Question
>      if there is a good justification for it.  If there isn't, see if
>      it makes it better if you instead append the new stuff to
>      existing sequence.  If neither results in satisfying code,
>      perhaps it is good time to totally rewrite it to make both
>      existing and new stuff fit in the flow.

This is great advice, not just for reviewers. Thanks for taking the time
to write this.

> We may want to be a bit more explicit and readable, by spelling out
> the expectation, i.e.
> 
> 		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";
> 
> This makes it clear that these two pseudo-commands, spelled with a
> dash in front and stand for other commands, are the only thing we
> care about and what their accepted spelling are.

Nice and precise. I incorporated this into v3.

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

diff --git a/Documentation/git.txt b/Documentation/git.txt
index 13f83a2a3a..302607a496 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 a25940d72e..e2aeb57a35 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;
 
 		/*
@@ -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();
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
index 6c3e1f7159..6c33a43690 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


      parent reply	other threads:[~2022-03-31 21:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Garrit Franke [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331212709.36036-1-garrit@slashdev.space \
    --to=garrit@slashdev.space \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).