Skip to content

Added dynamic dispatch for % sigil commands - #18158

Merged
fdncred merged 3 commits into
nushell:mainfrom
fdncred:dynamic_percent_cmds
May 5, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
fdncred:dynamic_percent_cmds

Conversation

@fdncred

@fdncred fdncred commented May 4, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds dynamic builtin dispatch for the percent sigil so users can write:

  • %($cmd) arg1 arg2
  • %$cmd arg1 arg2

Previously, percent dispatch only worked when the command name was statically known at parse time. With this change, the builtin name can come from a runtime expression while still preserving percent semantics: resolve and run builtin commands only.

User-facing changes (Release notes)

Nushell now supports dynamic percent builtin dispatch with forms like %($cmd) and %$cmd.

This preserves percent safety semantics while allowing runtime command selection:

  • only builtins are eligible targets
  • non-builtin/custom/alias targets are rejected
  • arguments are forwarded as parsed values (not string-evaluated like shell eval)

This should work very similarly to dynamic dispatch of externals. The same model was followed to implement it.

Here's a couple examples that are also tests.

Built-ins resolve

❯ let cmd = 'echo'; %$cmd 'hello'
hello
❯ %('echo') 'world'
world
❯ let cmd = 'echo'; %($cmd) 'hello'
hello

Missing commands fail

❯ let cmd = 'my_nonexistent_cmd'; %($cmd)
Error: nu::shell::command_not_found

  × Command not found
   ╭─[repl_entry #7:1:33]
 1 │ let cmd = 'my_nonexistent_cmd'; %($cmd)
   ·                                 ───┬───
   ·                                    ╰── command not found
   ╰────

Custom commands aren't built-in commands so they fail too

❯ def custom_cmd [] { 'nope' }; let cmd = 'custom_cmd'; %($cmd)
Error: nu::shell::command_not_found

  × Command not found
   ╭─[repl_entry #8:1:55]
 1 │ def custom_cmd [] { 'nope' }; let cmd = 'custom_cmd'; %($cmd)
   ·                                                       ───┬───
   ·                                                          ╰── command not found
   ╰────

Additional notes

closes #18150

@fdncred
fdncred merged commit 64ab31b into nushell:main May 5, 2026
17 checks passed
@fdncred
fdncred deleted the dynamic_percent_cmds branch May 5, 2026 22:54
@github-actions github-actions Bot added this to the v0.113.0 milestone May 5, 2026
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:other Noted in "Other changes" section labels May 5, 2026
@cptpiepmatz cptpiepmatz changed the title implement dynamic dispatch for % sigil commands Added dynamic dispatch for % sigil commands May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:other Noted in "Other changes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow us to create wrapper commands with the builtin sigil %, using parameterized variables like ($arg1)

1 participant