feat: add TypoMixin for 'Did you mean?' suggestions on invalid commands - #255
Open
anandhu-eng wants to merge 5 commits into
Open
anandhu-eng wants to merge 5 commits into
anandhu-eng wants to merge 5 commits into
Conversation
When a user mistypes an action (e.g. 'rune') or target (e.g. 'scrip'), the CLI now prints a 'Did you mean ...?' hint between the usage block and the standard argparse error, using difflib.get_close_matches (stdlib, no new dependencies). - mlc/typo_mixin.py: new TypoMixin class that overrides ArgumentParser.error() - mlc/main.py: introduce TypoArgumentParser(TypoMixin, ArgumentParser) and use it in both build_pre_parser() and build_parser(); subparsers inherit the class automatically so target-level typos are also caught - tests/test_typo_mixin.py: 33 tests across unit (suggest()) and CLI subprocess suites covering action typos, target typos, no-match cases, and regression checks on usage/error output - docs/typo_detection.md: full documentation with examples and tuning guide - README.md: mention typo detection in Key Features Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅ |
🤖 AI PR Review Summary\n\nAdds a typo detection feature to the CLI by extending argparse.ArgumentParser with a mixin that suggests close matches for mistyped commands or targets. This improves user experience by providing helpful hints on invalid inputs. The change includes a new TypoMixin class, integration into the main parser, documentation, and tests. Risks are minimal as the mixin only overrides the error method and uses standard library difflib. Design is clean and modular, leveraging Python's MRO and argparse's parser_class inheritance to cover subcommands automatically. |
- Drop `choices` from the pre-parser's `target` so target typos are reported by the action's own subparser with its real choices (`mlc docker cach` no longer suggests the invalid 'cache'; `mlc load cfgg` now suggests 'cfg'). - Add `details`/`extra` to the `load` subparser, which previously crashed once 'cfg' got past the pre-parser. - Add a prefix rule to TypoMixin.suggest(): candidates that start with the input (3+ chars) are listed first, so `exp` -> 'experiment' and `dock` -> 'docker' ahead of 'doc'. - Fix the README/docs example for `mlc rune script`, which actually prints "Did you mean one of: 'prune', 'run'?", and update other doc examples to match real output. - Add regression tests for the above (37 tests in test_typo_mixin.py). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mlc/typo_mixin.py— aTypoMixinclass that overridesArgumentParser.error()to print a "Did you mean …?" hint when the user mistypes an action or targetmlc/main.pyviaTypoArgumentParser(TypoMixin, argparse.ArgumentParser); subparsers inherit the class automatically so both action-level and target-level typos are caughtexp→experiment,dock→dockertarget), so hints never suggest a target the action rejectsdifflib.get_close_matchesfrom the Python stdlibExample output
Changes
mlc/typo_mixin.pyTypoMixinwithsuggest()anderror()overridemlc/main.pyTypoArgumentParserinbuild_pre_parser()andbuild_parser(); drop the pre-parser's sharedtargetchoices; adddetails/extrato theloadsubparsertests/test_typo_mixin.pydocs/typo_detection.mdREADME.md.github/workflows/test-unit.ymlTest plan
python -m unittest discover -s tests -p "test_*.py" -v— all tests pass (130 total, 37 intest_typo_mixin.py)rune,pul,fidn,serach,lstscrip,cach,rpoexp→experiment,dock→dockerfirst,remote→remote-*mlc docker cach(no invalidcachehint),mlc load cfgg→cfgxyzzy123) shows no hint — standard error only🤖 Generated with Claude Code