Skip to content

Smart routing for alias arguments + dry-run routing display - #2304

Merged
max-sixty merged 19 commits into
mainfrom
alias-args
Apr 19, 2026
Merged

max-sixty merged 19 commits into
mainfrom
alias-args

Conversation

@max-sixty

@max-sixty max-sixty commented Apr 19, 2026 •

Copy link
Copy Markdown
Owner

Routes --KEY=VALUE tokens based on what the template references. Tokens whose KEY appears as {{ KEY }} in the alias bind to that variable; everything else joins {{ args }}. Space form --KEY VALUE is equivalent.

Key changes:

  • Parser uses template reference introspection (via referenced_vars_for_config) to decide binding at parse time.
  • Post-alias --yes retired — use wt -y <alias> (the global form) instead. --dry-run on an alias invocation is also retired; wt config alias show/dry-run <name> replaces it (landed separately in feat(config): add wt config alias show and dry-run subcommands #2291).
  • Hyphens in keys canonicalize to underscores, so --my-var=x binds {{ my_var }}.
  • -- is a literal-forward escape — everything after it forwards to {{ args }} regardless of bindings.
  • wt <alias> --help / -h prints a hint pointing at wt config alias show/dry-run rather than silently forwarding the flag into {{ args }}. wt <alias> -- --help still forwards.

Advisory warnings (printed on stderr, don't affect execution):

  • --KEY VALUE with ---prefixed VALUE: almost always a typo where --KEY=VALUE was meant.
  • wt config alias show/dry-run on a name that shadows a top-level built-in (e.g. list): the alias is unreachable via wt <name>.

wt config alias dry-run now prints # bound: and # args: routing comments above the rendered command so users can see how each token was interpreted.

Docs rewrite of the aliases section: concrete fly preview-env example, "Passing values" section (renamed from the opaque "How arguments are routed"), routing mechanism moved above the introspection tools, up rebase recipe restored, since-main alias added.

Test plan

  • cargo test --lib --bins commands::alias::tests — unit tests (new: duplicate-key precedence, footgun warning, multi-= value)
  • cargo test --test integration step_alias — integration tests (new: shadow warning on show and dry-run, --help intercept, retired --dry-run via wt step)
  • cargo test --test integration test_command_pages_and_skill_files_are_in_sync — skill file sync
  • cargo run -- hook pre-merge --yes — full test suite + lints green

🤖 Generated with Claude Code

max-sixty and others added 14 commits April 17, 2026 13:58
… --var

The alias argument parser now uses the template's referenced variables as
the binding signal: `--KEY=VALUE` binds to `{{ KEY }}` iff the template
references it, else forwards as a positional. The `--` literal-forward
escape stops routing (including built-in `--yes`/`--dry-run`), and the
`--var KEY=VALUE` / `--var=KEY=VALUE` special cases are removed — they're
just `--KEY=VALUE` under the new grammar.

Before: `wt rm my-branch` errored ("Unexpected argument"). Users had to
pre-declare vars via `--var` even though templates already declare which
variables they use.

After: Every post-name token has a valid destination — bound var or
positional forwarded into `{{ args }}`. No token is ever silently dropped.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts:
#	src/commands/alias.rs
#	src/config/mod.rs
#	src/main.rs
# Conflicts:
#	src/commands/alias.rs
#	tests/integration_tests/step_alias.rs
Follow-up to the main-branch merge that combined PR D (template-ref
routing) with #2290 (post-alias --yes cutover). Under the new grammar,
`--yes` after an alias name falls through to the positional branch and
forwards into `{{ args }}` — it doesn't error as "Unknown flag" (main's
expectation) nor does it skip approval (its old semantics).

- Migrate four PR-D tests from inline `--yes` to global `-y` via
  `make_snapshot_cmd_with_global_flags`
- Rename `test_post_alias_yes_no_longer_supported` to
  `test_post_alias_yes_does_not_skip_approval` and assert the approval
  prompt surfaces instead of an unknown-flag error

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`--KEY VAL` now consumes two tokens whenever a next token exists,
mirroring the atomic `--KEY=VALUE` form: bind if KEY is referenced,
else forward both as positionals. Removes the previous "next starts
with --" safety check.

Tradeoff: `--env --dry-run` with `env` referenced binds
`env=--dry-run` rather than activating dry-run. Workaround is the
`=` form or putting flags before the bound key.

Co-Authored-By: Claude <[email protected]>
# Conflicts:
#	docs/content/extending.md
#	skills/worktrunk/reference/extending.md
#	src/commands/alias.rs
#	tests/integration_tests/approval_ui.rs
#	tests/integration_tests/step_alias.rs
#	tests/snapshots/integration__integration_tests__step_alias__alias_double_dash_stops_routing.snap
#	tests/snapshots/integration__integration_tests__step_alias__step_alias_binds_space_separated_var.snap
#	tests/snapshots/integration__integration_tests__step_alias__step_alias_with_var.snap
# Conflicts:
#	src/config/expansion.rs
Collapse the five-row routing table to two rows (bound vs forwarded),
trim the space-form surprise note to one sentence, and remove the
"Escaping with --" subsection — the table row covers the mechanism and
`--` is a POSIX convention.

Co-Authored-By: Claude <[email protected]>
# Conflicts:
#	docs/content/extending.md
#	skills/worktrunk/reference/extending.md
#	src/commands/alias.rs
#	tests/snapshots/integration__integration_tests__step_alias__step_alias_binds_space_separated_var.snap
Drop the dry-run forwarding/vars.* caveat paragraph (the bullet above
covers the main point) and the minijinja-parses-as-subtraction
explanation (behavior is self-evident from the example).

Co-Authored-By: Claude <[email protected]>
Runtime polish on top of the smart-routing parser:

- Parser now returns `(AliasOptions, Vec<String>)`; callers emit the
  warnings before execution. Keeps `parse` pure so tests can assert on
  both halves.
- Warn when `--KEY VALUE` binds VALUE to a `--`-prefixed next token —
  almost always a typo where `--KEY=--VALUE` was meant.
- Warn in `wt config alias show/dry-run` when the alias name shadows a
  top-level built-in (reachable only via `wt step <name>`).
- `wt config alias dry-run` prints `# bound:` and `# args:` routing
  comments above the rendered command so users can see how each token
  was interpreted.

Docs: rewrite the aliases section — concrete fly preview-env example,
"Passing values" renamed from the opaque "How arguments are routed",
routing mechanism moves above the introspection tools. Restore the `up`
rebase recipe and add a `since-main` alias example.

New tests: duplicate-key precedence, footgun warning, shadow warning.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Comment thread src/commands/config/alias.rs Resolved
max-sixty and others added 4 commits April 19, 2026 12:03
These were superseded by tests with parallel names taken from main during
the merge — CI's `cargo insta test --unreferenced reject` caught them
after the branch pushed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…adow warning

Review cleanup found two user-visible issues in the alias subsystem:

- `wt config alias dry-run --help` referenced `--var`, which was retired;
  aliases only accept `--KEY=VALUE`. This text propagates to docs via
  test_command_pages_and_skill_files_are_in_sync.
- `warn_if_shadowed` used backticks + format!, bypassing the <bold> styling
  other warnings use for commands/identifiers.

Co-Authored-By: Claude <[email protected]>
Round 2 review follow-ups on PR #2304:

- Intercept `--help`/`-h` in alias dispatch (both top-level and `wt step`
  paths). Print a hint pointing at `wt config alias show/dry-run` rather
  than silently forwarding the flag into `{{ args }}`. `wt <alias> -- --help`
  still forwards, documented alongside. Tests cover both paths.
- Drop the "invoke via `wt step <name>`" suggestion from the shadow
  warning — the step path exists as an escape, not something to
  advertise. A TODO on `step_alias` flags it for deprecation consideration.
- `--KEY --VALUE` footgun warning now shows the user's typed form
  (`--my-env`) throughout, not the canonicalized template name (`my_env`).
- New parser test for `--foo=a=b=c` (multi-`=` value) to lock in
  `split_once('=')` behavior.
- Snapshot test for `wt config alias dry-run <shadowed>`, mirroring the
  existing `show` coverage.
- Snapshot test for retired `--dry-run` reaching `step_alias` (shared
  parser already handled it; this pins the dispatch route).
- Docs: `{{ args }}` now appears in the multi-step pipeline example, and
  a sentence notes every step sees the same args.

Co-Authored-By: Claude <[email protected]>
The help-hint output in `emit_alias_help_hint` used bare `println!`, which
the stdout-guard test rejects — `src/commands/` code must use
`worktrunk::styling::println` (or be explicitly allowlisted for stdout).

Switch to the styling re-export and collapse the four `println!` calls
into a single multiline invocation. Add `alias.rs` to `STDOUT_ALLOWED_PATHS`
since the substring scanner can't distinguish `worktrunk::styling::println!`
from `std::println!` — `--help` output conventionally goes to stdout, so
this file belongs in the allowlist alongside `config/alias.rs`.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 68229ce into main Apr 19, 2026
23 checks passed
@max-sixty
max-sixty deleted the alias-args branch April 19, 2026 21:39
max-sixty added a commit that referenced this pull request Apr 19, 2026
…2306)

## Summary

Four alias-typo surfaces rendered in two formats. After #2304's round-2
review, this aligns `config alias show/dry-run` with `wt <typo>` and `wt
step <typo>`.

Before:
- `wt deplyo` / `wt step deplyo` → clap-native `error: unrecognized
subcommand 'X'` + `tip: …`, exit 2
- `wt config alias show deplyo` / `dry-run deplyo` → custom anyhow
gutter `✗ unknown alias 'X'` + gutter bars, exit 1

After (all four surfaces):
- Same clap-native `error:` / `tip:` / `Usage:` layout, exit 2
- `config alias show/dry-run` say "alias" / "aliases" instead of
"subcommand" / "subcommands" — the positional is an alias name, not a
subcommand

## How

`unknown_alias_error` builds a real `clap::Error` with
`ErrorKind::InvalidSubcommand`, renders it through clap, then
substitutes clap's fixed phrases (`unrecognized subcommand` →
`unrecognized alias`, `similar subcommands` → `similar aliases`,
`similar subcommand` → `similar alias`). Writing through
`anstream::AutoStream::auto(stderr)` gets NO_COLOR / TTY detection and
clap's singular-vs-plural phrasing automatically. Returns
`WorktrunkError::AlreadyDisplayed { exit_code: 2 }` so `main`'s
`finish_command` still runs `terminate_output` + `write_if_verbose`.

Substitutions are deliberately narrow — an alias name or typo containing
the literal substring `subcommand` (e.g. `my-subcommand`) is echoed
verbatim, not mangled.

## Changes

- `src/commands/config/alias.rs`: replace the anyhow-returning
`unknown_alias_error` (exit 1, custom gutter) with a clap-rendering
version (exit 2, matches clap-native surfaces). Parameterized by `sub`
so the Usage line reads `wt config alias <sub> <NAME>`.
- New tests: `test_config_alias_dry_run_unknown_suggests`,
`test_config_alias_show_unknown_singular_suggestion`,
`test_config_alias_show_unknown_preserves_user_input`.
- Refreshed `test_config_alias_show_unknown_suggests` and
`test_config_alias_show_unknown_no_suggestions` snapshots.

## Test plan

- [x] `cargo test --test integration step_alias` (58 pass)
- [x] `cargo test --lib --bins` (598 pass)
- [x] `pre-commit run --all-files` clean
- [x] Manual smoke on all four paths (exit 2; ANSI stripped under
`NO_COLOR=1 | cat`; preserved under `CLICOLOR_FORCE=1`)
- [x] Aliases containing `subcommand` echoed verbatim under typo

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@worktrunk-bot worktrunk-bot mentioned this pull request Apr 19, 2026
max-sixty pushed a commit that referenced this pull request Oct 1, 2026
)

`wt <alias> --<Tab>` (and `wt step <alias> --<Tab>`) offered `--dry-run`
and `--var`, but neither does anything useful after an alias.
`AliasOptions::parse` rejects `--dry-run` with "`--dry-run` is no longer
supported; use `wt config alias dry-run <name>` instead" (unless the
template references `dry_run`), and passes `--var KEY=VALUE` through to
the alias as plain positional arguments. Completion was steering users
into an error or a flag that silently does nothing.

The flags come from `build_alias_completion_command` in
`src/completion.rs`, a stub that predates alias argument routing (added
in #1641; `--dry-run` was retired for aliases in #2291 and #2304). This
PR removes the stub's own `--dry-run`, `--yes` and `--var` declarations,
so the stub declares nothing beyond the alias name and help text.
Aliases that wrap a built-in (`co = "wt switch {{ args }}"`) still
mirror that built-in's flags through `mirror_alias_command`, which this
PR doesn't touch.

**Test:** `test_complete_step_alias_shows_flags`, which asserted the
stale flags were present, becomes
`test_complete_step_alias_offers_no_misleading_flags` and asserts
`--dry-run` and `--var` are absent. It fails on `main` (`Unexpected
--dry-run flag`) and passes with the fix. All 80 completion integration
tests pass.

**Not covered:** `--yes` still completes after an alias, and so do
`--config` and `--verbose`, because they are clap `global = true` flags
on the root command and propagate into every subcommand's completions.
After an alias they also forward as arguments (the alias parser tests
note that `global = true` doesn't cross `external_subcommand`). Removing
them would mean hiding globals per alias stub, which is a separate
decision. The stub's own `--yes` declaration duplicated the global, so
dropping it changes nothing.

Co-authored-by: worktrunk-bot <[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.

2 participants