Skip to content

feat: add TypoMixin for 'Did you mean?' suggestions on invalid commands#255

Open
anandhu-eng wants to merge 1 commit into
mainfrom
feat/typo-detection
Open

feat: add TypoMixin for 'Did you mean?' suggestions on invalid commands#255
anandhu-eng wants to merge 1 commit into
mainfrom
feat/typo-detection

Conversation

@anandhu-eng

Copy link
Copy Markdown
Contributor

Summary

  • Adds mlc/typo_mixin.py — a TypoMixin class that overrides ArgumentParser.error() to print a "Did you mean …?" hint when the user mistypes an action or target
  • Wires it into mlc/main.py via TypoArgumentParser(TypoMixin, argparse.ArgumentParser); subparsers inherit the class automatically so both action-level and target-level typos are caught
  • No new runtime dependencies — uses difflib.get_close_matches from the Python stdlib

Example output

$ mlc rune script
Did you mean one of: 'prune', 'run'?
mlc: error: argument command: invalid choice: 'rune' …

$ mlc run scrip
Did you mean 'script'?
mlc: error: argument target: invalid choice: 'scrip' …

$ mlc find cach
Did you mean 'cache'?
mlc: error: argument target: invalid choice: 'cach' …

Changes

File Change
mlc/typo_mixin.py New — TypoMixin with suggest() and error() override
mlc/main.py Use TypoArgumentParser in build_pre_parser() and build_parser()
tests/test_typo_mixin.py New — 33 tests (unit + CLI subprocess)
docs/typo_detection.md New — full documentation with examples and tuning guide
README.md Mention typo detection in Key Features
.github/workflows/test-unit.yml Update step description to reflect expanded test scope

Test plan

  • python -m unittest discover -s tests -p "test_*.py" -v — all 33 tests pass
  • Existing tests (meta validation, cache mark-tmp) — no regressions
  • Manually verified action typos: rune, pul, fidn, serach, lst
  • Manually verified target typos: scrip, cach, rpo
  • Verified garbage input (e.g. xyzzy123) shows no hint — standard error only
  • Exit code remains 2; usage block and error message unchanged

🤖 Generated with Claude Code

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>
@anandhu-eng anandhu-eng requested a review from a team as a code owner July 4, 2026 18:47
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

🤖 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant