Skip to content

Add generate completions to auto-generate extern definitions, with completions, from carapace, zsh, fish, man pages and --help - #19314

Merged
fdncred merged 5 commits into
nushell:mainfrom
fdncred:learn_extern
Oct 24, 2026
Merged

fdncred merged 5 commits into
nushell:mainfrom
fdncred:learn_extern

Conversation

@fdncred

@fdncred fdncred commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Nushell checks a call to an internal command before it runs, but an external command only gets that once someone writes an extern for it, flag by flag, and completions only once someone writes a completer for it. Other tools already describe thousands of commands: carapace specs, fish and zsh completion files, man pages, and each command's --help. generate completions <command> reads one of them and returns the text of a module of export extern definitions, with completer commands.

You save that text and use or source it, or save it in an autoload dir. For use, the file needs a name other than the tool's, such as git-completions.nu, because use names the module after the file and a module can't export an extern with its own name.

  • carapace (carapace <tool> export), fish and zsh give the whole subcommand tree. man and --help give the flags of the named command, so generate completions "git status" reads man git-status or git status --help.
  • Without --source, the first source that knows any flags wins, in the order carapace, fish, zsh, man, help. --source all merges every source that knows any flags. When sources disagree, the earlier source wins. A flag they disagree on taking a value becomes a switch, and a flag any source says takes an optional value is left undeclared.
  • Piped text is read as the source --source names, as in ^git status -h | generate completions "git status" --source help.
  • A signature can't declare flags with an optional value (--color[=when]) or single-dash long options (-name). Those are listed in each extern's extra description, and they still reach the tool.

With @complete external, an external completer like carapace also lists the flags the signature declares, so flag completion showed each of them twice. Flag-name completion now drops command-wide suggestions that repeat a declared flag.

User-facing changes (Release notes)

Generate external command definitions and completions

The new generate completions command generates a module of extern definitions and completers from carapace specs, fish or zsh completions, man pages, or --help text. Save the output and load it with use or source, or place it in an autoload directory:

generate completions git | save ($nu.user-autoload-dirs | first | path join git-completions.nu)

When using use, choose a filename other than the tool's name, such as git-completions.nu. Generated modules provide flag-value and argument completions where the source supports them, including fixed lists, git branches, remotes and tags from fish completers, users and hosts, and native completions from Cobra-based tools such as gh, docker and kubectl.

By default, the first source that knows any flags is used, in the order carapace, fish, zsh, man, then help. Use --source all to merge sources. Carapace, fish and zsh provide subcommand trees; man and help provide flags for the named command. You can also pipe source text, as in ^git status -h | generate completions "git status" --source help.

Flag-name completion also no longer lists a flag twice when an extern with @complete external declares it and the external completer suggests it too.

Additional notes

  • Every module generated from fish's 1,070 completion files (782 modules, 186 with completers), zsh's 966 (681 modules) and Homebrew's 139 zsh files (129 modules) passes nu-check --as-module. So does every module from each source for git, ssh, make, rg, gh, tar, brew, curl, cargo, kubectl and docker, and from --source all.
  • generate completions refuses a command named like a nushell keyword (fish ships completions for if, source, while), since an extern can't have that name.
  • bash and PowerShell aren't sources. bash completions are code that mostly calls _parse_help on --help, which the help source already reads, and PowerShell's completers for native commands are script blocks registered at runtime.
  • The help source runs <command> --help with no input, the man source runs man with MANPAGER=cat, and a tool with a completion subcommand is asked <tool> __complete "" to find out whether it's built with cobra.

@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Oct 24, 2026
@fdncred
fdncred merged commit e68ab52 into nushell:main Oct 24, 2026
11 checks passed
@fdncred
fdncred deleted the learn_extern branch October 24, 2026 12:05
@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant