[PATCH v6 00/10] parseopt: add subcommand autocorrection
- From
- Jiamu Sun <39@barroit.sh>
- Date
- Apr 23, 2026, 01:37 UTC
- Message-ID
- <SY0P300MB0801677A2A1E0FD38D06A841CE2A2@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
- In-Reply-To
- <SY0P300MB0801AE56F740AD087D22B35ACE2D2@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_AUTOCORRECT flag, and enables it 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.
Some string literals are also combined so the full text is easier to grep for.
Changes in v6: - Adjust existing tests to fit subcommand autocorrection behavior - Change the similar subcommand hint exit code to 129
Changes in v5:
- Make subcommand autocorrection behave the same as command
autocorrection
- Rename PARSE_OPT_SUBCOMMAND_AUTOCORR to
PARSE_OPT_SUBCOMMAND_AUTOCORRECT
- Adjust subcommand autocorrection tests to fit new behaviorChanges in v4:
- Add missing files to Meson build
- Change API prefix from autocorr to autocorrect
- Split the commit that moves tty code
- Add API documentation
- Use standard Damerau-Levenshtein distance and common practice
fuzziness thresholds
- Rename AUTOCORRECT_HINTONLY to AUTOCORRECT_HINT
- Change commit subject prefix for tests from "help:" to "parseopt:"
- Fix coding style issuesChanges in v3: - Align with the coding guildline - Split patch so diffs don't get hidden by code movement - Improve commit messages
Changes in v2: - Reword the explanation of default autocorrection behavior
Jiamu Sun (10):
parseopt: extract subcommand handling from parse_options_step()
help: make autocorrect handling reusable
help: move tty check for autocorrection to autocorrect.c
autocorrect: use mode and delay instead of magic numbers
autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINT
autocorrect: provide config resolution API
parseopt: autocorrect mistyped subcommands
parseopt: enable subcommand autocorrection for git-remote and
git-notes
parseopt: add tests for subcommand autocorrection
doc: document autocorrect APIMakefile | 1 + autocorrect.c | 89 +++++++++++++++ autocorrect.h | 36 ++++++ builtin/notes.c | 10 +- builtin/remote.c | 12 +- help.c | 120 ++++---------------- meson.build | 1 + parse-options.c | 183 +++++++++++++++++++++++------- parse-options.h | 1 + t/meson.build | 1 + t/t0040-parse-options.sh | 5 +- t/t7900-maintenance.sh | 4 +- t/t9004-autocorrect-subcommand.sh | 58 ++++++++++ 13 files changed, 366 insertions(+), 155 deletions(-) create mode 100644 autocorrect.c create mode 100644 autocorrect.h create mode 100755 t/t9004-autocorrect-subcommand.sh
base-commit: f65aba1e87db64413b6d1ed5ae5a45b5a84a0997
-- 2.54.0