⌨️ Completion
Generate shell auto-completion scripts, list all available commands, and query command help — for integrating Composer into your shell or toolchain.
composer completion has built-in support for bash / zsh / fish shells. The generated scripts can be activated via source or by writing them to ~/.bashrc etc.
When to Use
- 🐚 Configure Composer command completion for users in automated installation scripts.
- 📖 Build interactive Composer wrappers that need to enumerate all subcommands for menus.
- ❓ Programmatically query a command's
--helpto extract available options for argument completion. - 🚀 Write completion scripts to user config directories in Go CLI tools.
Structured Types
ShellType
Shell type constants defined in completion.go.
type ShellType string
const (
BashShell ShellType = "bash"
ZshShell ShellType = "zsh"
FishShell ShellType = "fish"
)Method Signatures
| Method | Signature | Description |
|---|---|---|
| ⌨️ GenerateCompletion | func (c *Composer) GenerateCompletion(shell ShellType) (string, error) | Generate completion script for specified shell |
| ⚙️ GenerateCompletionWithOptions | func (c *Composer) GenerateCompletionWithOptions(shell ShellType, options map[string]string) (string, error) | Generate completion script with custom options |
| 📋 ListCommands | func (c *Composer) ListCommands() (string, error) | List all commands via composer list |
| ❓ GetCommandHelp | func (c *Composer) GetCommandHelp(command string) (string, error) | Command help via composer help <command> |
Parameters
GenerateCompletion / GenerateCompletionWithOptions
| Parameter | Type | Description |
|---|---|---|
shell | ShellType | BashShell / ZshShell / FishShell |
options | map[string]string | Additional options (e.g., --shell) |
GetCommandHelp
| Parameter | Type | Description |
|---|---|---|
command | string | Subcommand name, e.g., require, install |
Examples
Generate and Save bash Completion Script
package main
import (
"fmt"
"log"
"os"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
script, err := comp.GenerateCompletion(composer.BashShell)
if err != nil {
log.Fatalf("Failed to generate completion script: %v", err)
}
if err := os.WriteFile("/etc/bash_completion.d/composer", []byte(script), 0644); err != nil {
log.Fatal(err)
}
fmt.Println("✅ bash completion script installed")
}Generate zsh Completion
script, err := comp.GenerateCompletion(composer.ZshShell)
if err != nil {
log.Fatal(err)
}
// Write to user's zsh completion directory
_ = os.WriteFile(os.Getenv("HOME")+"/.zsh/completions/_composer", []byte(script), 0644)List All Commands
out, err := comp.ListCommands()
if err != nil {
log.Fatal(err)
}
fmt.Println(out)Query Command Help
help, err := comp.GetCommandHelp("require")
if err != nil {
log.Fatal(err)
}
fmt.Println(help)Generate Completion with Options
script, err := comp.GenerateCompletionWithOptions(composer.FishShell, map[string]string{
"shell": "fish",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(script)Advanced
Activating Completion Scripts
The generated scripts don't auto-activate — users need to source them in their shell config, or write them to the corresponding completion directory and reopen the terminal. bash uses /etc/bash_completion.d/, zsh uses ~/.zsh/completions/, fish uses ~/.config/fish/completions/.
ListCommands Returns Raw Text
ListCommands directly returns the multi-line text from composer list without parsing. For structured command lists, split by lines yourself and filter out header lines like Available commands:, or refer to related helper methods in Parsing Functions.