git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>
Subject: [PATCH v2 17/20] builtin/remote.c: let parse-options parse subcommands
Date: Fri, 19 Aug 2022 18:04:08 +0200	[thread overview]
Message-ID: <20220819160411.1791200-18-szeder.dev@gmail.com> (raw)
In-Reply-To: <20220819160411.1791200-1-szeder.dev@gmail.com>

'git remote' parses its subcommands with a long list of if-else if
statements.  parse-options has just learned to parse subcommands, so
let's use that facility instead, with the benefits of shorter code,
handling unknown subcommands, and listing subcommands for Bash
completion.  Make sure that the default operation mode doesn't accept
any arguments; and while at it remove the capitalization of the error
message and adjust the test checking it accordingly.

Note that 'git remote' has both 'remove' and 'rm' subcommands, and the
former is preferred [1], so hide the latter for completion.

Note also that the functions implementing each subcommand only accept
the 'argc' and '**argv' parameters, so add a (unused) '*prefix'
parameter to make them match the type expected by parse-options, and
thus avoid casting a bunch of function pointers.

[1] e17dba8fe1 (remote: prefer subcommand name 'remove' to 'rm',
    2012-09-06)

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 builtin/remote.c  | 70 +++++++++++++++++++++--------------------------
 t/t5505-remote.sh |  2 +-
 2 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index d9b8746cb3..4a6d47c03a 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -150,7 +150,7 @@ static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
 	return 0;
 }
 
-static int add(int argc, const char **argv)
+static int add(int argc, const char **argv, const char *prefix)
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
@@ -680,7 +680,7 @@ static void handle_push_default(const char* old_name, const char* new_name)
 }
 
 
-static int mv(int argc, const char **argv)
+static int mv(int argc, const char **argv, const char *prefix)
 {
 	int show_progress = isatty(2);
 	struct option options[] = {
@@ -844,7 +844,7 @@ static int mv(int argc, const char **argv)
 	return 0;
 }
 
-static int rm(int argc, const char **argv)
+static int rm(int argc, const char **argv, const char *prefix)
 {
 	struct option options[] = {
 		OPT_END()
@@ -1255,7 +1255,7 @@ static int show_all(void)
 	return result;
 }
 
-static int show(int argc, const char **argv)
+static int show(int argc, const char **argv, const char *prefix)
 {
 	int no_query = 0, result = 0, query_flag = 0;
 	struct option options[] = {
@@ -1358,7 +1358,7 @@ static int show(int argc, const char **argv)
 	return result;
 }
 
-static int set_head(int argc, const char **argv)
+static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
 	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
@@ -1463,7 +1463,7 @@ static int prune_remote(const char *remote, int dry_run)
 	return result;
 }
 
-static int prune(int argc, const char **argv)
+static int prune(int argc, const char **argv, const char *prefix)
 {
 	int dry_run = 0, result = 0;
 	struct option options[] = {
@@ -1492,7 +1492,7 @@ static int get_remote_default(const char *key, const char *value, void *priv)
 	return 0;
 }
 
-static int update(int argc, const char **argv)
+static int update(int argc, const char **argv, const char *prefix)
 {
 	int i, prune = -1;
 	struct option options[] = {
@@ -1575,7 +1575,7 @@ static int set_remote_branches(const char *remotename, const char **branches,
 	return 0;
 }
 
-static int set_branches(int argc, const char **argv)
+static int set_branches(int argc, const char **argv, const char *prefix)
 {
 	int add_mode = 0;
 	struct option options[] = {
@@ -1594,7 +1594,7 @@ static int set_branches(int argc, const char **argv)
 	return set_remote_branches(argv[0], argv + 1, add_mode);
 }
 
-static int get_url(int argc, const char **argv)
+static int get_url(int argc, const char **argv, const char *prefix)
 {
 	int i, push_mode = 0, all_mode = 0;
 	const char *remotename = NULL;
@@ -1647,7 +1647,7 @@ static int get_url(int argc, const char **argv)
 	return 0;
 }
 
-static int set_url(int argc, const char **argv)
+static int set_url(int argc, const char **argv, const char *prefix)
 {
 	int i, push_mode = 0, add_mode = 0, delete_mode = 0;
 	int matches = 0, negative_matches = 0;
@@ -1739,41 +1739,33 @@ static int set_url(int argc, const char **argv)
 
 int cmd_remote(int argc, const char **argv, const char *prefix)
 {
+	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
 		OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
+		OPT_SUBCOMMAND("add", &fn, add),
+		OPT_SUBCOMMAND("rename", &fn, mv),
+		OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
+		OPT_SUBCOMMAND("remove", &fn, rm),
+		OPT_SUBCOMMAND("set-head", &fn, set_head),
+		OPT_SUBCOMMAND("set-branches", &fn, set_branches),
+		OPT_SUBCOMMAND("get-url", &fn, get_url),
+		OPT_SUBCOMMAND("set-url", &fn, set_url),
+		OPT_SUBCOMMAND("show", &fn, show),
+		OPT_SUBCOMMAND("prune", &fn, prune),
+		OPT_SUBCOMMAND("update", &fn, update),
 		OPT_END()
 	};
-	int result;
 
 	argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
-		PARSE_OPT_STOP_AT_NON_OPTION);
+			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
 
-	if (argc < 1)
-		result = show_all();
-	else if (!strcmp(argv[0], "add"))
-		result = add(argc, argv);
-	else if (!strcmp(argv[0], "rename"))
-		result = mv(argc, argv);
-	else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
-		result = rm(argc, argv);
-	else if (!strcmp(argv[0], "set-head"))
-		result = set_head(argc, argv);
-	else if (!strcmp(argv[0], "set-branches"))
-		result = set_branches(argc, argv);
-	else if (!strcmp(argv[0], "get-url"))
-		result = get_url(argc, argv);
-	else if (!strcmp(argv[0], "set-url"))
-		result = set_url(argc, argv);
-	else if (!strcmp(argv[0], "show"))
-		result = show(argc, argv);
-	else if (!strcmp(argv[0], "prune"))
-		result = prune(argc, argv);
-	else if (!strcmp(argv[0], "update"))
-		result = update(argc, argv);
-	else {
-		error(_("Unknown subcommand: %s"), argv[0]);
-		usage_with_options(builtin_remote_usage, options);
+	if (fn) {
+		return !!fn(argc, argv, prefix);
+	} else {
+		if (argc) {
+			error(_("unknown subcommand: %s"), argv[0]);
+			usage_with_options(builtin_remote_usage, options);
+		}
+		return !!show_all();
 	}
-
-	return result ? 1 : 0;
 }
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index a549a21ef6..9006196ac6 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -258,7 +258,7 @@ test_expect_success 'without subcommand accepts -v' '
 
 test_expect_success 'without subcommand does not take arguments' '
 	test_expect_code 129 git -C test remote origin 2>err &&
-	grep "^error: Unknown subcommand:" err
+	grep "^error: unknown subcommand:" err
 '
 
 cat >test/expect <<EOF
-- 
2.37.2.817.g36f84ce71d


  parent reply	other threads:[~2022-08-19 16:32 UTC|newest]

Thread overview: 97+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-25 12:38 [PATCH 00/20] parse-options: handle subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 01/20] git.c: update NO_PARSEOPT markings SZEDER Gábor
2022-07-25 14:31   ` Ævar Arnfjörð Bjarmason
2022-08-02 17:37     ` SZEDER Gábor
2022-08-02 21:00       ` Junio C Hamano
2022-08-03 13:11         ` Ævar Arnfjörð Bjarmason
2022-08-03 21:34         ` SZEDER Gábor
2022-08-04  7:47           ` Ævar Arnfjörð Bjarmason
2022-08-11 21:35           ` Junio C Hamano
2022-08-12 15:28             ` SZEDER Gábor
2022-08-12 16:46               ` Junio C Hamano
2022-07-26 19:55   ` SZEDER Gábor
2022-07-25 12:38 ` [PATCH 02/20] t3301-notes.sh: check that default operation mode doesn't take arguments SZEDER Gábor
2022-07-25 12:38 ` [PATCH 03/20] t5505-remote.sh: check the behavior without a subcommand SZEDER Gábor
2022-07-25 14:37   ` Ævar Arnfjörð Bjarmason
2022-07-25 12:38 ` [PATCH 04/20] t0040-parse-options: test parse_options() with various 'parse_opt_flags' SZEDER Gábor
2022-07-25 14:38   ` Ævar Arnfjörð Bjarmason
2022-08-12 15:04     ` SZEDER Gábor
2022-07-25 12:38 ` [PATCH 05/20] api-parse-options.txt: fix description of OPT_CMDMODE SZEDER Gábor
2022-07-25 12:38 ` [PATCH 06/20] parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options SZEDER Gábor
2022-07-25 12:38 ` [PATCH 07/20] parse-options: clarify the limitations of PARSE_OPT_NODASH SZEDER Gábor
2022-07-25 12:38 ` [PATCH 08/20] parse-options: drop leading space from '--git-completion-helper' output SZEDER Gábor
2022-07-25 12:38 ` [PATCH 09/20] parse-options: add support for parsing subcommands SZEDER Gábor
2022-07-25 14:43   ` Ævar Arnfjörð Bjarmason
2022-07-25 19:29     ` SZEDER Gábor
2022-07-25 19:41       ` Ævar Arnfjörð Bjarmason
2022-07-25 21:02         ` SZEDER Gábor
2022-08-12 15:15         ` SZEDER Gábor
2022-07-25 17:37   ` Junio C Hamano
2022-07-25 12:38 ` [PATCH 10/20] builtin/bundle.c: let parse-options parse subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 11/20] builtin/commit-graph.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 12/20] builtin/gc.c: let parse-options parse 'git maintenance's subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 13/20] builtin/hook.c: let parse-option parse subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 14/20] builtin/multi-pack-index.c: let parse-options " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 15/20] builtin/notes.c: " SZEDER Gábor
2022-07-25 16:49   ` Junio C Hamano
2022-07-25 12:38 ` [PATCH 16/20] builtin/reflog.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 17/20] builtin/remote.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 18/20] builtin/sparse-checkout.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 19/20] builtin/stash.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 20/20] builtin/worktree.c: " SZEDER Gábor
2022-07-25 13:15 ` [PATCH 00/20] parse-options: handle subcommands Derrick Stolee
2022-07-25 16:00   ` SZEDER Gábor
2022-07-25 16:08     ` Derrick Stolee
2022-07-25 17:13 ` Ævar Arnfjörð Bjarmason
2022-07-25 17:56 ` Junio C Hamano
2022-07-26 15:42   ` Johannes Schindelin
2022-07-26 18:02     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:03 ` [PATCH v2 " SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 01/20] git.c: update NO_PARSEOPT markings SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 02/20] t3301-notes.sh: check that default operation mode doesn't take arguments SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 03/20] t5505-remote.sh: check the behavior without a subcommand SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 04/20] t0040-parse-options: test parse_options() with various 'parse_opt_flags' SZEDER Gábor
2022-08-19 17:23     ` Ævar Arnfjörð Bjarmason
2022-08-20 11:14       ` SZEDER Gábor
2022-08-19 18:18     ` Junio C Hamano
2022-08-20 10:31       ` SZEDER Gábor
2022-08-20 21:27         ` Junio C Hamano
2022-08-19 16:03   ` [PATCH v2 05/20] api-parse-options.txt: fix description of OPT_CMDMODE SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 06/20] parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 07/20] parse-options: clarify the limitations of PARSE_OPT_NODASH SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 08/20] parse-options: drop leading space from '--git-completion-helper' output SZEDER Gábor
2022-08-19 17:30     ` Ævar Arnfjörð Bjarmason
2022-08-19 18:35       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 09/20] parse-options: add support for parsing subcommands SZEDER Gábor
2022-08-19 17:33     ` Ævar Arnfjörð Bjarmason
2022-08-19 19:03     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 10/20] builtin/bundle.c: let parse-options parse subcommands SZEDER Gábor
2022-08-19 17:50     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 11/20] builtin/commit-graph.c: " SZEDER Gábor
2022-08-19 17:53     ` Ævar Arnfjörð Bjarmason
2022-08-19 17:56       ` Ævar Arnfjörð Bjarmason
2022-08-19 18:22       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 12/20] builtin/gc.c: let parse-options parse 'git maintenance's subcommands SZEDER Gábor
2022-08-19 20:59     ` Junio C Hamano
2022-08-19 16:04   ` [PATCH v2 13/20] builtin/hook.c: let parse-options parse subcommands SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 14/20] builtin/multi-pack-index.c: " SZEDER Gábor
2022-08-19 17:57     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 15/20] builtin/notes.c: " SZEDER Gábor
2022-08-19 18:01     ` Ævar Arnfjörð Bjarmason
2022-08-21 17:56       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 16/20] builtin/reflog.c: " SZEDER Gábor
2022-08-19 18:08     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` SZEDER Gábor [this message]
2022-08-19 16:04   ` [PATCH v2 18/20] builtin/sparse-checkout.c: " SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 19/20] builtin/stash.c: " SZEDER Gábor
2022-08-19 19:06     ` Ævar Arnfjörð Bjarmason
2022-08-20 10:27       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 20/20] builtin/worktree.c: " SZEDER Gábor
2022-09-05 18:50   ` [PATCH 0/5] parse-options: minor cleanups for handling subcommands SZEDER Gábor
2022-09-05 18:50     ` [PATCH 1/5] t0040-parse-options: remove leftover debugging SZEDER Gábor
2022-09-05 18:50     ` [PATCH 2/5] test-parse-options.c: don't use for loop initial declaration SZEDER Gábor
2022-09-05 18:50     ` [PATCH 3/5] test-parse-options.c: fix style of comparison with zero SZEDER Gábor
2022-09-05 18:50     ` [PATCH 4/5] notes: simplify default operation mode arguments check SZEDER Gábor
2022-09-05 18:50     ` [PATCH 5/5] notes, remote: show unknown subcommands between `' SZEDER Gábor
2022-09-07 19:12     ` [PATCH 0/5] parse-options: minor cleanups for handling subcommands Junio C Hamano
2022-09-07 21:22       ` SZEDER Gábor

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=20220819160411.1791200-18-szeder.dev@gmail.com \
    --to=szeder.dev@gmail.com \
    --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).