Skip to content

Typo auto-correction for unknown flags #146

Description

@toiroakr

Summary

When a user provides an unknown flag (e.g., --verbos instead of --verbose), politty should suggest the closest matching known flag using string similarity (Levenshtein distance).

Motivation

This is a common UX improvement found in many CLI frameworks (e.g., @effect/cli, git). It reduces user frustration from typos and helps users discover correct flag names without consulting help text.

Currently, politty handles unknown flags based on the Zod schema's unknown keys mode (strict/strip/passthrough), but does not suggest corrections.

Proposed Behavior

$ mycli --verbos
error: Unknown flag '--verbos'. Did you mean '--verbose'?
  • When an unknown flag is detected (in strict or strip mode), compute the Levenshtein distance between the unknown flag and all known flags
  • If a close match is found (e.g., distance ≤ 2 or relative threshold), include a "Did you mean ...?" suggestion in the error/warning message
  • Should work for both long flags (--flag) and short flags (-f)
  • Multiple suggestions may be shown if several flags are similarly close

Implementation Notes

  • The correction logic should integrate with the existing unknown flag handling in src/validator/error-formatter.ts (formatUnknownFlag / formatUnknownFlagWarning)
  • Known flags can be extracted from the Zod schema via src/core/schema-extractor.ts
  • A simple Levenshtein distance implementation is sufficient (no external dependency needed)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions