maintenance: add parse-options boilerplate for subcommands

Several of the git-maintenance subcommands don't take any options, so they don't bother looking at argv at all. This means they'll silently accept garbage, like: $ git maintenance register --foo [no output] $ git maintenance stop bar [no output] Let's give them the basic boilerplate to detect and handle these cases: $ git maintenance register --foo error: unknown option `foo' usage: git maintenance register $ git maintenance stop bar usage: git maintenance stop We could reduce the number of lines of code here a bit with a shared helper function. But it's worth building out the boilerplate, as it may serve as the base for adding options later. Note one complication: maintenance_start() calls directly into maintenance_register(), so it now needs to pass a plausible argv (we don't care, but parse_options() is expecting there to at least be an argv[0] program name). This is an extra line of code, but it eliminates the need for an explanatory comment. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 25, 2022 at 06:51 UTC 0d330a53f31a0885954ac48c5e6d24efd3969039
1 file changed +42 -1
builtin/gc.c
+42 -1
@@ -1465,14 +1465,28 @@ static char *get_maintpath(void)
1465 return strbuf_detach(&sb, NULL);
1466 }
1467
1468 +static char const * const builtin_maintenance_register_usage[] = {
1469 + N_("git maintenance register"),
1470 + NULL
1471 +};
1472 +
1473 static int maintenance_register(int argc, const char **argv, const char *prefix)
1474 {
1475 + struct option options[] = {
1476 + OPT_END(),
1477 + };
1478 int rc;
1479 char *config_value;
1480 struct child_process config_set = CHILD_PROCESS_INIT;
1481 struct child_process config_get = CHILD_PROCESS_INIT;
1482 char *maintpath = get_maintpath();
1483
1484 + argc = parse_options(argc, argv, prefix, options,
1485 + builtin_maintenance_register_usage, 0);
1486 + if (argc)
1487 + usage_with_options(builtin_maintenance_register_usage,
1488 + options);
1489 +
1490 /* Disable foreground maintenance */
1491 git_config_set("maintenance.auto", "false");
1492
@@ -1509,12 +1523,26 @@ done:
1523 return rc;
1524 }
1525
1526 +static char const * const builtin_maintenance_unregister_usage[] = {
1527 + N_("git maintenance unregister"),
1528 + NULL
1529 +};
1530 +
1531 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1532 {
1533 + struct option options[] = {
1534 + OPT_END(),
1535 + };
1536 int rc;
1537 struct child_process config_unset = CHILD_PROCESS_INIT;
1538 char *maintpath = get_maintpath();
1539
1540 + argc = parse_options(argc, argv, prefix, options,
1541 + builtin_maintenance_unregister_usage, 0);
1542 + if (argc)
1543 + usage_with_options(builtin_maintenance_unregister_usage,
1544 + options);
1545 +
1546 config_unset.git_cmd = 1;
1547 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1548 "--fixed-value", "maintenance.repo", maintpath, NULL);
@@ -2496,6 +2524,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
2524 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2525 OPT_END()
2526 };
2527 + const char *register_args[] = { "register", NULL };
2528
2529 argc = parse_options(argc, argv, prefix, options,
2530 builtin_maintenance_start_usage, 0);
@@ -2505,13 +2534,25 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
2534 opts.scheduler = resolve_scheduler(opts.scheduler);
2535 validate_scheduler(opts.scheduler);
2536
2508 - if (maintenance_register(0, NULL, NULL)) /* It doesn't take any args */
2537 + if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2538 warning(_("failed to add repo to global config"));
2539 return update_background_schedule(&opts, 1);
2540 }
2541
2542 +static const char *const builtin_maintenance_stop_usage[] = {
2543 + N_("git maintenance stop"),
2544 + NULL
2545 +};
2546 +
2547 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2548 {
2549 + struct option options[] = {
2550 + OPT_END()
2551 + };
2552 + argc = parse_options(argc, argv, prefix, options,
2553 + builtin_maintenance_stop_usage, 0);
2554 + if (argc)
2555 + usage_with_options(builtin_maintenance_stop_usage, options);
2556 return update_background_schedule(NULL, 0);
2557 }
2558