[PATCH v2 0/5] parseopt: add subcommand autocorrection
- From
- Jiamu Sun <39@barroit.sh>
- Date
- Mar 8, 2026, 23:16 UTC
- Message-ID
- <SY0P300MB08013E35DCA8FC31B0662125CE78A@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
- In-Reply-To
- <SY0P300MB08019805A8304105FA805EB1CE78A@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
Git currently provides auto-correction for builtins and aliases, but lacks this functionality for subcommands parsed via the parse-options API. Subcommands are also commands, and typos will occur, too. Like:
git remote add-rul
So, this series introduces subcommand auto-correction.
By default, this implementation enables autocorrection for builtins with mandatory subcommands. However, for those using PARSE_OPT_SUBCOMMAND_OPTIONAL, autocorrection is skipped to avoid misinterpreting legitimate unknown arguments as mistyped subcommands.
To allow builtins with optional subcommands to explicitly opt in, this series adds the PARSE_OPT_SUBCOMMAND_AUTOCORR flag. This flag is subsequently enabled for git remote and git notes.
Additionally, the existing autocorrection logic is extracted from help.c so subcommand handling can reuse the same config parsing and prompt/delay logic.
Changes in v2: - Reworded the explanation of default autocorrection behavior.
Jiamu Sun (5): parseopt: extract subcommand handling from parse_options_step() help: refactor command autocorrection handling parseopt: autocorrect mistyped subcommands parseopt: enable subcommand autocorrect for remote and notes help: add tests for subcommand autocorrection
Makefile | 1 + autocorrect.c | 92 +++++++++++++++++++ autocorrect.h | 23 +++++ builtin/notes.c | 10 +- builtin/remote.c | 12 +-- help.c | 106 ++++----------------- parse-options.c | 147 +++++++++++++++++++++++------- parse-options.h | 1 + t/t9004-autocorrect-subcommand.sh | 49 ++++++++++ 9 files changed, 306 insertions(+), 135 deletions(-) create mode 100644 autocorrect.c create mode 100644 autocorrect.h create mode 100755 t/t9004-autocorrect-subcommand.sh
base-commit: 795c338de725e13bd361214c6b768019fc45a2c1
-- 2.53.0