Skip to content

⌨️ 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 --help to 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.

go
type ShellType string

const (
	BashShell ShellType = "bash"
	ZshShell  ShellType = "zsh"
	FishShell ShellType = "fish"
)

Method Signatures

MethodSignatureDescription
⌨️ GenerateCompletionfunc (c *Composer) GenerateCompletion(shell ShellType) (string, error)Generate completion script for specified shell
⚙️ GenerateCompletionWithOptionsfunc (c *Composer) GenerateCompletionWithOptions(shell ShellType, options map[string]string) (string, error)Generate completion script with custom options
📋 ListCommandsfunc (c *Composer) ListCommands() (string, error)List all commands via composer list
❓ GetCommandHelpfunc (c *Composer) GetCommandHelp(command string) (string, error)Command help via composer help <command>

Parameters

GenerateCompletion / GenerateCompletionWithOptions

ParameterTypeDescription
shellShellTypeBashShell / ZshShell / FishShell
optionsmap[string]stringAdditional options (e.g., --shell)

GetCommandHelp

ParameterTypeDescription
commandstringSubcommand name, e.g., require, install

Examples

Generate and Save bash Completion Script

go
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

go
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

go
out, err := comp.ListCommands()
if err != nil {
	log.Fatal(err)
}
fmt.Println(out)

Query Command Help

go
help, err := comp.GetCommandHelp("require")
if err != nil {
	log.Fatal(err)
}
fmt.Println(help)

Generate Completion with Options

go
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.

Released under the MIT License