git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v6 08/10] parseopt: enable subcommand autocorrection for git-remote and git-notes

From
JSJiamu Sun <39@barroit.sh>
Date
Apr 23, 2026, 01:37 UTC
Message-ID
<SY0P300MB08014BED919401CACCE547FDCE2A2@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
In-Reply-To
<SY0P300MB0801677A2A1E0FD38D06A841CE2A2@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>

Add PARSE_OPT_SUBCOMMAND_AUTOCORRECT to enable autocorrection for subcommands parsed with PARSE_OPT_SUBCOMMAND_OPTIONAL.

Apply this to 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. Both builtins either resolve to a single subcommand or take no subcommand at all, meaning any unknown argument encountered by the parser must be a mistyped subcommand.

Signed-off-by: Jiamu Sun <39@barroit.sh>
---
 builtin/notes.c  | 10 +++-------
 builtin/remote.c | 12 ++++--------
 parse-options.c  | 16 +++++++++-------
 parse-options.h  |  1 +
 4 files changed, 17 insertions(+), 22 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 9af602bdd7b4..f9bf350df4e8 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -1149,14 +1149,10 @@ int cmd_notes(int argc,
 
 	repo_config(the_repository, git_default_config, NULL);
 	argc = parse_options(argc, argv, prefix, options, git_notes_usage,
-			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
-	if (!fn) {
-		if (argc) {
-			error(_("unknown subcommand: `%s'"), argv[0]);
-			usage_with_options(git_notes_usage, options);
-		}
+			     PARSE_OPT_SUBCOMMAND_OPTIONAL |
+			     PARSE_OPT_SUBCOMMAND_AUTOCORRECT);
+	if (!fn)
 		fn = list;
-	}
 
 	if (override_notes_ref) {
 		struct strbuf sb = STRBUF_INIT;
diff --git a/builtin/remote.c b/builtin/remote.c
index de989ea3ba96..6a78ab8f4cd2 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1953,15 +1953,11 @@ int cmd_remote(int argc,
 	};
 
 	argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
-			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
+			     PARSE_OPT_SUBCOMMAND_OPTIONAL |
+			     PARSE_OPT_SUBCOMMAND_AUTOCORRECT);
 
-	if (fn) {
+	if (fn)
 		return !!fn(argc, argv, prefix, repo);
-	} else {
-		if (argc) {
-			error(_("unknown subcommand: `%s'"), argv[0]);
-			usage_with_options(builtin_remote_usage, options);
-		}
+	else
 		return !!show_all();
-	}
 }
diff --git a/parse-options.c b/parse-options.c
index 4370d9c623e4..d60e7bd3c977 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -720,14 +720,16 @@ static enum parse_opt_result handle_subcommand(struct parse_opt_ctx_t *ctx,
 	if (!err)
 		return PARSE_OPT_SUBCOMMAND;
 
-	/*
-	 * arg is neither a short or long option nor a subcommand.  Since this
-	 * command has a default operation mode, we have to treat this arg and
-	 * all remaining args as args meant to that default operation mode.
-	 * So we are done parsing.
-	 */
-	if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
+	if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL &&
+	    !(ctx->flags & PARSE_OPT_SUBCOMMAND_AUTOCORRECT)) {
+		/*
+		 * arg is neither a short or long option nor a subcommand.
+		 * Since this command has a default operation mode, we have to
+		 * treat this arg and all remaining args as args meant to that
+		 * default operation mode.  So we are done parsing.
+		 */
 		return PARSE_OPT_DONE;
+	}
 
 	find_subcommands(&cmds, options);
 	assumed = autocorrect_subcommand(arg, &cmds);
diff --git a/parse-options.h b/parse-options.h
index 706de9729f6b..e5fd4da4055b 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -40,6 +40,7 @@ enum parse_opt_flags {
 	PARSE_OPT_ONE_SHOT = 1 << 5,
 	PARSE_OPT_SHELL_EVAL = 1 << 6,
 	PARSE_OPT_SUBCOMMAND_OPTIONAL = 1 << 7,
+	PARSE_OPT_SUBCOMMAND_AUTOCORRECT = 1 << 8,
 };
 
 enum parse_opt_option_flags {
-- 
2.54.0
Previous: Jiamu SunNext: Jiamu Sun
Message 92 of 96 in “parseopt: add subcommand autocorrection”
  1. 0/5 parseopt: add subcommand autocorrectionJiamu Sun, Mar 8, 2026
  2. 1/5 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 8, 2026
  3. Junio C HamanoMar 8, 2026
  4. Jiamu SunMar 9, 2026
  5. 2/5 help: refactor command autocorrection handlingJiamu Sun, Mar 8, 2026
  6. Junio C HamanoMar 8, 2026
  7. Jiamu SunMar 9, 2026
  8. 3/5 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 8, 2026
  9. Junio C HamanoMar 9, 2026
  10. Jiamu SunMar 9, 2026
  11. 4/5 parseopt: enable subcommand autocorrect for remote and notesJiamu Sun, Mar 8, 2026
  12. 5/5 help: add tests for subcommand autocorrectionJiamu Sun, Mar 8, 2026
  13. Aaron PlattnerMar 11, 2026
  14. Jiamu SunMar 11, 2026
  15. Junio C HamanoMar 12, 2026
  16. Junio C HamanoMar 8, 2026
  17. Jiamu SunMar 8, 2026
  18. 0/5 parseopt: add subcommand autocorrectionJiamu Sun, Mar 8, 2026
  19. 1/5 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 8, 2026
  20. 2/5 help: refactor command autocorrection handlingJiamu Sun, Mar 8, 2026
  21. 3/5 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 8, 2026
  22. 4/5 parseopt: enable subcommand autocorrect for remote and notesJiamu Sun, Mar 8, 2026
  23. 5/5 help: add tests for subcommand autocorrectionJiamu Sun, Mar 8, 2026
  24. 0/8 parseopt: add subcommand autocorrectionJiamu Sun, Mar 10, 2026
  25. 1/8 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 10, 2026
  26. Karthik NayakMar 10, 2026
  27. Jiamu SunMar 11, 2026
  28. Junio C HamanoMar 11, 2026
  29. Junio C HamanoMar 11, 2026
  30. Jiamu SunMar 11, 2026
  31. 2/8 help: make autocorrect handling reusableJiamu Sun, Mar 10, 2026
  32. Karthik NayakMar 10, 2026
  33. Junio C HamanoMar 10, 2026
  34. Jiamu SunMar 11, 2026
  35. Jiamu SunMar 11, 2026
  36. 3/8 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Mar 10, 2026
  37. Karthik NayakMar 10, 2026
  38. Jiamu SunMar 11, 2026
  39. Jiamu SunMar 12, 2026
  40. 4/8 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTONLYJiamu Sun, Mar 10, 2026
  41. Karthik NayakMar 10, 2026
  42. Jiamu SunMar 11, 2026
  43. 5/8 autocorrect: provide config resolution APIJiamu Sun, Mar 10, 2026
  44. Karthik NayakMar 10, 2026
  45. 6/8 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 10, 2026
  46. Junio C HamanoMar 10, 2026
  47. Jiamu SunMar 11, 2026
  48. Jiamu SunMar 11, 2026
  49. Junio C HamanoMar 12, 2026
  50. Jiamu SunMar 12, 2026
  51. 7/8 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Mar 10, 2026
  52. 8/8 help: add tests for subcommand autocorrectionJiamu Sun, Mar 10, 2026
  53. Junio C HamanoMar 11, 2026
  54. Jiamu SunMar 11, 2026
  55. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Mar 16, 2026
  56. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 16, 2026
  57. 02/10 help: make autocorrect handling reusableJiamu Sun, Mar 16, 2026
  58. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Mar 16, 2026
  59. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Mar 16, 2026
  60. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Mar 16, 2026
  61. 06/10 autocorrect: provide config resolution APIJiamu Sun, Mar 16, 2026
  62. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 16, 2026
  63. Junio C HamanoMar 16, 2026
  64. Jiamu SunMar 17, 2026
  65. Junio C HamanoApr 15, 2026
  66. Jiamu SunApr 16, 2026
  67. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Mar 16, 2026
  68. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Mar 16, 2026
  69. 10/10 doc: document autocorrect APIJiamu Sun, Mar 16, 2026
  70. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Apr 22, 2026
  71. 02/10 help: make autocorrect handling reusableJiamu Sun, Apr 22, 2026
  72. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Apr 22, 2026
  73. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Apr 22, 2026
  74. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Apr 22, 2026
  75. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Apr 22, 2026
  76. 06/10 autocorrect: provide config resolution APIJiamu Sun, Apr 22, 2026
  77. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Apr 22, 2026
  78. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Apr 22, 2026
  79. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Apr 22, 2026
  80. 10/10 doc: document autocorrect APIJiamu Sun, Apr 22, 2026
  81. Junio C HamanoApr 23, 2026
  82. Jiamu SunApr 23, 2026
  83. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Apr 23, 2026
  84. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Apr 23, 2026
  85. 02/10 help: make autocorrect handling reusableJiamu Sun, Apr 23, 2026
  86. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Apr 23, 2026
  87. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Apr 23, 2026
  88. 06/10 autocorrect: provide config resolution APIJiamu Sun, Apr 23, 2026
  89. 10/10 doc: document autocorrect APIJiamu Sun, Apr 23, 2026
  90. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Apr 23, 2026
  91. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Apr 23, 2026
  92. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Apr 23, 2026
  93. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Apr 23, 2026
  94. Junio C HamanoMay 11, 2026
  95. Jiamu SunMay 15, 2026
  96. Junio C HamanoJun 6, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.