Skip to content

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

Open
anandhu-eng wants to merge 5 commits into
mainfrom
feat/typo-detection
Open

anandhu-eng wants to merge 5 commits into
mainfrom
feat/typo-detection

Conversation

@anandhu-eng

@anandhu-eng anandhu-eng commented Jul 4, 2026 •

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
  • Candidates that start with the input (3+ chars) are suggested first, so abbreviations work: exp → experiment, dock → docker
  • Target typos are checked against the chosen action's own targets (the pre-parser no longer validates target), so hints never suggest a target the action rejects
  • 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 run: error: argument target: invalid choice: 'scrip' …

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

$ mlc dock run
Did you mean one of: 'docker', 'docker-run', 'doc'?
mlc: error: argument command: invalid choice: 'dock' …

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(); drop the pre-parser's shared target choices; add details/extra to the load subparser
tests/test_typo_mixin.py New — 37 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 tests pass (130 total, 37 in test_typo_mixin.py)
  • 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 prefix matches: exp → experiment, dock → docker first, remote → remote-*
  • Verified target hints use per-action choices: mlc docker cach (no invalid cache hint), mlc load cfgg → cfg
  • Scored cutoffs 0.5 / 0.55 / 0.6 on 100 realistic typos + 23 junk inputs; kept 0.6 (0.5 nearly doubled unwanted suggestions for +1 hit)
  • 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.

@arjunsuresh arjunsuresh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

anandhu-eng and others added 4 commits September 24, 2026 01:46
- 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

No deployments
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.

2 participants