Skip to content

Commit ae8b7e1

Browse files
barroitgitster
authored andcommitted
parseopt: enable subcommand autocorrection for git-remote and git-notes
Add PARSE_OPT_SUBCOMMAND_AUTOCORR to enable autocorrection for subcommands parsed with PARSE_OPT_SUBCOMMAND_OPTIONAL. Use it for git-remote and git-notes, so mistyped subcommands can be automatically corrected, and builtin entry points no longer need to handle the unknown subcommand error path themselves. This is safe for these two builtins, because they either resolve to a single subcommand or take no subcommand at all. This means that if the subcommand parser encounters an unknown argument, it must be a mistyped subcommand. Signed-off-by: Jiamu Sun <39@barroit.sh> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent be9df6d commit ae8b7e1

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

builtin/notes.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,14 +1149,10 @@ int cmd_notes(int argc,
11491149

11501150
repo_config(the_repository, git_default_config, NULL);
11511151
argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1152-
PARSE_OPT_SUBCOMMAND_OPTIONAL);
1153-
if (!fn) {
1154-
if (argc) {
1155-
error(_("unknown subcommand: `%s'"), argv[0]);
1156-
usage_with_options(git_notes_usage, options);
1157-
}
1152+
PARSE_OPT_SUBCOMMAND_OPTIONAL |
1153+
PARSE_OPT_SUBCOMMAND_AUTOCORR);
1154+
if (!fn)
11581155
fn = list;
1159-
}
11601156

11611157
if (override_notes_ref) {
11621158
struct strbuf sb = STRBUF_INIT;

builtin/remote.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,15 +1953,11 @@ int cmd_remote(int argc,
19531953
};
19541954

19551955
argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1956-
PARSE_OPT_SUBCOMMAND_OPTIONAL);
1956+
PARSE_OPT_SUBCOMMAND_OPTIONAL |
1957+
PARSE_OPT_SUBCOMMAND_AUTOCORR);
19571958

1958-
if (fn) {
1959+
if (fn)
19591960
return !!fn(argc, argv, prefix, repo);
1960-
} else {
1961-
if (argc) {
1962-
error(_("unknown subcommand: `%s'"), argv[0]);
1963-
usage_with_options(builtin_remote_usage, options);
1964-
}
1961+
else
19651962
return !!show_all();
1966-
}
19671963
}

parse-options.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,16 @@ static enum parse_opt_result handle_subcommand(struct parse_opt_ctx_t *ctx,
698698
if (!err)
699699
return PARSE_OPT_SUBCOMMAND;
700700

701-
/*
702-
* arg is neither a short or long option nor a subcommand. Since this
703-
* command has a default operation mode, we have to treat this arg and
704-
* all remaining args as args meant to that default operation mode.
705-
* So we are done parsing.
706-
*/
707-
if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
701+
if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL &&
702+
!(ctx->flags & PARSE_OPT_SUBCOMMAND_AUTOCORR)) {
703+
/*
704+
* arg is neither a short or long option nor a subcommand.
705+
* Since this command has a default operation mode, we have to
706+
* treat this arg and all remaining args as args meant to that
707+
* default operation mode. So we are done parsing.
708+
*/
708709
return PARSE_OPT_DONE;
710+
}
709711

710712
find_subcommands(&cmds, options);
711713
assumed = autocorrect_subcommand(arg, &cmds);

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum parse_opt_flags {
4040
PARSE_OPT_ONE_SHOT = 1 << 5,
4141
PARSE_OPT_SHELL_EVAL = 1 << 6,
4242
PARSE_OPT_SUBCOMMAND_OPTIONAL = 1 << 7,
43+
PARSE_OPT_SUBCOMMAND_AUTOCORR = 1 << 8,
4344
};
4445

4546
enum parse_opt_option_flags {

0 commit comments

Comments
 (0)