builtin/bundle.c: let parse-options parse subcommands
'git bundle' 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, handling missing or unknown subcommands, and listing subcommands for Bash completion. 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
aef7d75e5809eda765bbe407c7f8e0f8617f0fd0
1 file changed
+7
-18
builtin/bundle.c
+7
-18
@@ -195,30 +195,19 @@ cleanup:
195
196
int cmd_bundle(int argc, const char **argv, const char *prefix)
197
{
198
+ parse_opt_subcommand_fn *fn = NULL;
199
struct option options[] = {
200
+ OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
201
+ OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
202
+ OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
203
+ OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
204
OPT_END()
205
};
201
- int result;
206
207
argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
204
- PARSE_OPT_STOP_AT_NON_OPTION);
208
+ 0);
209
210
packet_trace_identity("bundle");
211
208
- if (argc < 2)
209
- usage_with_options(builtin_bundle_usage, options);
210
-
211
- else if (!strcmp(argv[0], "create"))
212
- result = cmd_bundle_create(argc, argv, prefix);
213
- else if (!strcmp(argv[0], "verify"))
214
- result = cmd_bundle_verify(argc, argv, prefix);
215
- else if (!strcmp(argv[0], "list-heads"))
216
- result = cmd_bundle_list_heads(argc, argv, prefix);
217
- else if (!strcmp(argv[0], "unbundle"))
218
- result = cmd_bundle_unbundle(argc, argv, prefix);
219
- else {
220
- error(_("Unknown subcommand: %s"), argv[0]);
221
- usage_with_options(builtin_bundle_usage, options);
222
- }
223
- return result ? 1 : 0;
212
+ return !!fn(argc, argv, prefix);
213
}