completion

package
v1.42.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package completion writes shell completions from a parser's own declaration.

A parser already knows everything a completion needs: the options and what each one is for, whether it takes an argument, the values it accepts, which options rule out which others, and what the subcommands are. A hand-written completion has to be told all of that again, and drifts the first time an option is added. Generating it means the completion cannot disagree with what the program accepts, because both are read from the same declaration.

The generated script is static: it encodes the declaration and calls nothing back. That keeps completion working when the binary is slow to start, or absent, and makes the output something a package can ship. Values that are only knowable at run time are the exception, and belong to the program rather than here.

Index

Constants

View Source
const (
	Zsh  = "zsh"
	Bash = "bash"
)

The shells that can be written.

Variables

View Source
var Shells = []string{Zsh, Bash}

Shells are the shells this package writes, in the order the help lists them.

zsh comes first because it is the one that can express the whole declaration: descriptions against each option, the values an option accepts, and options that rule one another out. bash's completion is a coarser thing and says less, which is a limit of the shell rather than of what is known here.

Functions

func Option

func Option(shell *string) option.Option

Option is the option a program adds to offer completions of itself.

It is opt-in rather than built into the parser: a program that never ships a completion should not carry the scripts to write one, and the shell the caller wants is a choice only they can make.

var shell string
parser.Options = append(parser.Options, completion.Option(&shell))
parser.ParseOrExit()
if shell != "" {
	return completion.Write(os.Stdout, parser, shell)
}

It is hidden: writing a completion is done once, when the program is installed, and an option that serves that has no business in the help of every invocation afterwards. It is accepted exactly as any other, and a program that would rather advertise it can declare its own.

func Write

func Write(writer io.Writer, parser *argument_parser.Parser, shell string) error

Write writes the completion for the parser to w.

Types

type UnsupportedShellError

type UnsupportedShellError struct {
	Shell string
}

UnsupportedShellError is a shell this package cannot write.

func (*UnsupportedShellError) Error

func (unsupportedShellError *UnsupportedShellError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL