[PATCH v4 00/10] parseopt: add subcommand autocorrection
- From
- Jiamu Sun <39@barroit.sh>
- Date
- Mar 16, 2026, 15:36 UTC
- Message-ID
- <SY0P300MB080186A23FB9582AD793F0D1CE40A@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
- In-Reply-To
- <SY0P300MB0801C6F21C2D8F49892DF8E7CE46A@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, 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 split string literals are also combined so the full text is easier to grep for.
Changes 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 issuesNote on the autocorrection thresholds: In v3, there was a discussion about why specific thresholds were chosen. While attempting to document the legacy magic penalties (0, 2, 1, 3), I realized those weights exist in a system where prefix matches are completely ignored (due to a historical side-effect). Applying them to subcommands (which do evaluate prefix matches) makes the tolerance overly permissive. Therefore, v4 abandons the legacy weights in favor of a standard Damerau-Levenshtein distance and common practice length-based thresholds.
Changes 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 | 32 ++++++ builtin/notes.c | 10 +- builtin/remote.c | 12 +-- help.c | 107 ++++---------------- meson.build | 1 + parse-options.c | 162 ++++++++++++++++++++++-------- parse-options.h | 1 + t/meson.build | 1 + t/t9004-autocorrect-subcommand.sh | 51 ++++++++++ 11 files changed, 325 insertions(+), 142 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