Gen

Calling Gen on the root command is sufficient to enable completion script generation using the Hidden Subcommand.

import (
    "github.com/carapace-sh/carapace"
)

carapace.Gen(rootCmd)

Additionally invoke carapace.Test in a test to verify configuration during build time.

func TestCarapace(t *testing.T) {
    carapace.Test(t)
}

Hidden Subcommand

When Gen is invoked a hidden subcommand (_carapace) is added. This handles completion script generation and callbacks.

Completion

SHELL is optional and will be detected by parent process name.

command _carapace [SHELL]
# bash
source <(command _carapace)

# elvish
eval (command _carapace | slurp)

# fish
command _carapace | source

# nushell (update config.nu according to output)
command _carapace nushell

# oil
source <(command _carapace)

# powershell
Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" }
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
command _carapace | Out-String | Invoke-Expression

# tcsh
set autolist
eval `command _carapace tcsh`

# xonsh
COMPLETIONS_CONFIRM=True
exec($(command _carapace))

# zsh
source <(command _carapace)

Directly sourcing multiple completions in your shell init script increases startup time considerably. See lazycomplete for a solution to this problem.