Skip to content

Restore clap-native error for unrecognized subcommands - #2212

Merged
max-sixty merged 1 commit into
mainfrom
wt-s
Apr 14, 2026
Merged

max-sixty merged 1 commit into
mainfrom
wt-s

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

#[command(external_subcommand)] (added in #2054 for wt-<name> dispatch) captures every unknown subcommand, so clap's native InvalidSubcommand error path was dead — wt s printed a custom git-style line instead of clap's formatted error with suggestions and Usage block.

This PR synthesizes a real clap::Error{InvalidSubcommand} when no wt-<name> binary is on PATH and routes it through enhance_and_exit_error, so the output matches what clap would have produced:

```
error: unrecognized subcommand 's'
tip: some similar subcommands exist: 'step', 'switch'
Usage: wt [OPTIONS] [COMMAND]
For more information, try '--help'.
```

Nested-subcommand hints (wt squash → wt step squash) now layer on top of clap's error via the same path instead of a separate branch, so all unrecognized subcommands flow through one code path.

Swap Levenshtein for Jaro-Winkler > 0.7 to match clap's internal did_you_mean, so short names like s get suggestions that match short-prefix typos.

Exit code is now 2 (clap standard), matching pre-#2054 behavior.

Test plan

  • cargo run -- s / siwtch / squash / unknown → verified output and exit codes
  • wt hook pre-merge --yes (full test suite, 3178 tests)
  • Updated integration tests in tests/integration_tests/external.rs
  • Regenerated 5 snapshots (4 nested-suggestion help + 1 shell source-flag)

`#[command(external_subcommand)]` (added in #2054 for `wt-<name>` dispatch)
captures every unknown subcommand, so clap's native `InvalidSubcommand`
error path was dead — `wt s` printed a custom git-style line instead of
clap's formatted error with suggestions and Usage block.

Synthesize a real `clap::Error{InvalidSubcommand}` when no `wt-<name>`
binary is on PATH and route it through `enhance_and_exit_error`, so the
output matches what clap would have produced:

    error: unrecognized subcommand 's'
      tip: some similar subcommands exist: 'step', 'switch'
    Usage: wt [OPTIONS] [COMMAND]
    For more information, try '--help'.

Nested-subcommand hints (`wt squash` → `wt step squash`) now layer on top
of clap's error via the same path instead of a separate branch, so all
unrecognized subcommands flow through one code path.

Swap Levenshtein for Jaro-Winkler > 0.7 to match clap's internal
`did_you_mean`, so short names like `s` get suggestions that match
short-prefix typos.

Exit code is now 2 (clap standard), matching pre-#2054 behavior.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 7c03072 into main Apr 14, 2026
21 of 22 checks passed
@max-sixty
max-sixty deleted the wt-s branch April 14, 2026 01:32
max-sixty added a commit that referenced this pull request Apr 14, 2026
Follow-up to #2212. That PR restored clap-native errors for top-level
`wt <typo>`; this one applies the same treatment one level down so `wt
step <typo>` reads identically.

The three custom `bail!` branches in `step_alias` (`Unknown step command
X — perhaps Y?`, `Unknown step command X (no aliases configured)`,
`Unknown alias X (available: …)`) are replaced by a synthesized
`clap::Error{InvalidSubcommand}` routed through
`enhance_and_exit_error`. Output now reads:

```
error: unrecognized subcommand 'sqush'

  tip: some similar subcommands exist: 'squash', 'push'

Usage: wt step <COMMAND>

For more information, try '--help'.
```

Exit code is 2 (clap convention) instead of 1.

User-configured aliases are fed into `SuggestedSubcommand`, so a typo
like `wt step deplyo` still suggests `deploy` when `deploy` is a user
alias — `wt step u` suggests `up` (user alias) alongside
`push`/`squash`. The full alias list isn't crammed into the error; `wt
step --help` (already augmented by `augment_step_help`) is the discovery
surface, and the `Usage:` block points there.

Trade-off considered: the old `Unknown alias X (available: …)` wording
matched the "alias I configured" mental model better. Decided
consistency wins — `tip:` covers typo-suggestion equally well, and a
single error format for `wt <typo>` and `wt step <typo>` is easier to
learn than two.

Verified with `cargo run -- hook pre-merge --yes`: 3181 tests pass, no
lint or doc warnings. Five snapshot updates in
`tests/snapshots/integration__integration_tests__step_alias__*` reflect
the new format.

Co-authored-by: Claude <[email protected]>
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