diff mbox series

[v2,16/20] builtin/reflog.c: let parse-options parse subcommands

Message ID 20220819160411.1791200-17-szeder.dev@gmail.com (mailing list archive)
State Accepted
Commit 729b97332b05564133e4039fbab9f694c25097a2
Headers show
Series parse-options: handle subcommands | expand

Commit Message

SZEDER Gábor Aug. 19, 2022, 4:04 p.m. UTC
'git reflog' parses its subcommands with a couple 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,
and listing subcommands for Bash completion.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 builtin/reflog.c | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)

Comments

Ævar Arnfjörð Bjarmason Aug. 19, 2022, 6:08 p.m. UTC | #1
On Fri, Aug 19 2022, SZEDER Gábor wrote:

> +	parse_opt_subcommand_fn *fn = NULL;

Re the comment on notes.c this is a bit like that pattern...

> -log_reflog:
> -	return cmd_log_reflog(argc, argv, prefix);
> +			     PARSE_OPT_KEEP_UNKNOWN_OPT);
> +	if (fn)
> +		return fn(argc - 1, argv + 1, prefix);
> +	else
> +		return cmd_log_reflog(argc, argv, prefix);
>  }

Maybe more obvious (untested):

	if (!fn) {
		argc--;
		argv++;
		fn = cmd_log_reflog;
	}
	return fn(argc, argv, prefix);
diff mbox series

Patch

diff --git a/builtin/reflog.c b/builtin/reflog.c
index b8b1f4f8ea..d3f6d903fb 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -404,40 +404,21 @@  static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
 
 int cmd_reflog(int argc, const char **argv, const char *prefix)
 {
+	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
+		OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
+		OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
+		OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
+		OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
 		OPT_END()
 	};
 
 	argc = parse_options(argc, argv, prefix, options, reflog_usage,
+			     PARSE_OPT_SUBCOMMAND_OPTIONAL |
 			     PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
-			     PARSE_OPT_KEEP_UNKNOWN_OPT |
-			     PARSE_OPT_NO_INTERNAL_HELP);
-
-	/*
-	 * With "git reflog" we default to showing it. !argc is
-	 * impossible with PARSE_OPT_KEEP_ARGV0.
-	 */
-	if (argc == 1)
-		goto log_reflog;
-
-	if (!strcmp(argv[1], "-h"))
-		usage_with_options(reflog_usage, options);
-	else if (*argv[1] == '-')
-		goto log_reflog;
-
-	if (!strcmp(argv[1], "show"))
-		return cmd_reflog_show(argc - 1, argv + 1, prefix);
-	else if (!strcmp(argv[1], "expire"))
-		return cmd_reflog_expire(argc - 1, argv + 1, prefix);
-	else if (!strcmp(argv[1], "delete"))
-		return cmd_reflog_delete(argc - 1, argv + 1, prefix);
-	else if (!strcmp(argv[1], "exists"))
-		return cmd_reflog_exists(argc - 1, argv + 1, prefix);
-
-	/*
-	 * Fall-through for e.g. "git reflog -1", "git reflog master",
-	 * as well as the plain "git reflog" above goto above.
-	 */
-log_reflog:
-	return cmd_log_reflog(argc, argv, prefix);
+			     PARSE_OPT_KEEP_UNKNOWN_OPT);
+	if (fn)
+		return fn(argc - 1, argv + 1, prefix);
+	else
+		return cmd_log_reflog(argc, argv, prefix);
 }