builtin/sparse-checkout.c: let parse-options parse subcommands

'git sparse-checkout' parses its subcommands with a couple of if statements. parse-options has just learned to parse subcommands, so let's use that facility instead, with the benefits of shorter code, handling missing or unknown subcommands, and listing subcommands for Bash completion. Note that some of 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 function pointers. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Aug 19, 2022 at 18:04 UTC 1c3502b198a211031619ce22870490b1498c15dd
1 file changed +16 -28
builtin/sparse-checkout.c
+16 -28
@@ -48,7 +48,7 @@ static char const * const builtin_sparse_checkout_list_usage[] = {
48 NULL
49 };
50
51 -static int sparse_checkout_list(int argc, const char **argv)
51 +static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
52 {
53 static struct option builtin_sparse_checkout_list_options[] = {
54 OPT_END(),
@@ -431,7 +431,7 @@ static struct sparse_checkout_init_opts {
431 int sparse_index;
432 } init_opts;
433
434 -static int sparse_checkout_init(int argc, const char **argv)
434 +static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
435 {
436 struct pattern_list pl;
437 char *sparse_filename;
@@ -843,7 +843,8 @@ static struct sparse_checkout_reapply_opts {
843 int sparse_index;
844 } reapply_opts;
845
846 -static int sparse_checkout_reapply(int argc, const char **argv)
846 +static int sparse_checkout_reapply(int argc, const char **argv,
847 + const char *prefix)
848 {
849 static struct option builtin_sparse_checkout_reapply_options[] = {
850 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -876,7 +877,8 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
877 NULL
878 };
879
879 -static int sparse_checkout_disable(int argc, const char **argv)
880 +static int sparse_checkout_disable(int argc, const char **argv,
881 + const char *prefix)
882 {
883 static struct option builtin_sparse_checkout_disable_options[] = {
884 OPT_END(),
@@ -922,39 +924,25 @@ static int sparse_checkout_disable(int argc, const char **argv)
924
925 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
926 {
925 - static struct option builtin_sparse_checkout_options[] = {
927 + parse_opt_subcommand_fn *fn = NULL;
928 + struct option builtin_sparse_checkout_options[] = {
929 + OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
930 + OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
931 + OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
932 + OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
933 + OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
934 + OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
935 OPT_END(),
936 };
937
929 - if (argc == 2 && !strcmp(argv[1], "-h"))
930 - usage_with_options(builtin_sparse_checkout_usage,
931 - builtin_sparse_checkout_options);
932 -
938 argc = parse_options(argc, argv, prefix,
939 builtin_sparse_checkout_options,
935 - builtin_sparse_checkout_usage,
936 - PARSE_OPT_STOP_AT_NON_OPTION);
940 + builtin_sparse_checkout_usage, 0);
941
942 git_config(git_default_config, NULL);
943
944 prepare_repo_settings(the_repository);
945 the_repository->settings.command_requires_full_index = 0;
946
943 - if (argc > 0) {
944 - if (!strcmp(argv[0], "list"))
945 - return sparse_checkout_list(argc, argv);
946 - if (!strcmp(argv[0], "init"))
947 - return sparse_checkout_init(argc, argv);
948 - if (!strcmp(argv[0], "set"))
949 - return sparse_checkout_set(argc, argv, prefix);
950 - if (!strcmp(argv[0], "add"))
951 - return sparse_checkout_add(argc, argv, prefix);
952 - if (!strcmp(argv[0], "reapply"))
953 - return sparse_checkout_reapply(argc, argv);
954 - if (!strcmp(argv[0], "disable"))
955 - return sparse_checkout_disable(argc, argv);
956 - }
957 -
958 - usage_with_options(builtin_sparse_checkout_usage,
959 - builtin_sparse_checkout_options);
947 + return fn(argc, argv, prefix);
948 }