complete

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: PostgreSQL Imports: 13 Imported by: 0

Documentation

Overview

Package complete provides imperative and composable shell completion.

Completing a command line consists of returning a set of propositions to shell by writing in stdout one proposition per line. The target features of this package includes:

  • Composable and imperative API.
  • Inline documentation of propositions.
  • Hide proposition by prefix until user type prefix. e.g hidden filename.
  • Non intrusive configuration, easy for packaging.
  • Propose --opt=false booleans.
  • Exclude --help if other flag are set. Exclude flags when --help is set.

Shells processes propositions differently:

  • Bash accepts proposition as full command line completion.
  • Zsh and fish accept proposition as single argument completion.
  • Fish accept a description after a tab.

Also, shells have different handling of multiple propositions:

  • Bash simply list, user needs to type a letter to further complete.
  • Zsh and fish allow to loop propositions.

POSIX flags accepts setting a value to a flag either with an equal sign or space. Shells behave differently on this matter.

  • Bash treat value after equal sign as a single command line argument.
  • Zsh and Fish wants completion of value to include the flag.

i.e. to complete --section=, Zsh and fish expect proposition `--section=data` while bash expects `data`.

The limitations of this package:

  • no completion for short options.
  • no quoting/escaping of path.
  • ignores command line after point.
  • ignores COMP_TYPE.

Index

Constants

This section is empty.

Variables

View Source
var Hidden string = "._-"

Hidden lists first chars of hidden propositions.

  • . for dot files.
  • _ for hidden commands.
  • - for flags.

If [proposer] produces only hidden propositions, complete shows them, even if user didn't type first character. This is the only cas where both propositions starting with ., _ or - are presented to user.

Functions

func Any

func Any(v ...proposer) []string

Any tries several proposers until one returns completions

func Complete

func Complete(c Completer, shell Formatter)

Complete sends command line argument propositions to shell.

Reads command line and point from COMP_ env vars. Parses line and ask completer to produce propositions. Filters propositions to match last argument prefix. Format proposition help.

Exits process.

func DirOnly

func DirOnly() pathoption

DirOnly excludes files from propositions

func Enum

func Enum(flag string, propositions ...string) enum

Enum associates static propositions to a flag

func PFlagValue

func PFlagValue(pflags *pflag.FlagSet, args []string, valuers ...Valuer) ([]string, bool)

PFlagValue proposes value for current flag.

Accepts a set of proposer for some flags in flagset. Check args to determine which flag is being completed. Returns corresponding values.

See Enum and [Dir] to define values for a flag.

func PFlags

func PFlags(pflags *pflag.FlagSet) ([]string, bool)

PFlags generates completion from sp13/pflag flagset

Flagset should have command line arguments parsed. Proposes only unset flags.

func ParsePFlags

func ParsePFlags(f *pflag.FlagSet, args []string)

ParsePFlags parses completion args

Ensures current placeholder is not parsed. Accepts errors and unknown flags (e.g. partial flags).

func Path

func Path(flag string, options ...pathoption) path

Path complete file-system path

Use WithRoot, DirOnly, [FileOnly] and WithStdIO to configure the propositions.

Ignores hidden dirs unless user types dot.

func Proposition

func Proposition(arg string, full bool, help string) string

Proposition formats an argument for completion

func Trace

func Trace(msg string, args ...any)

Trace logs if PGM_TRACE is defined

Unlike internal/logging, levelTrace is unusable when completing since slog is not configured.

func WithRoot

func WithRoot(r string) pathoption

WithRoot searches path relative to another directory

func WithStdIO

func WithStdIO() pathoption

WithStdIO accepts - for stdin or stdout

Types

type Completer

type Completer interface {
	// Complete returns the list of command line continuation
	//
	// args is the list of command line token **so far**.
	// arguments after cursor are not accessible.
	// If user begins a new argument,
	// an empty string terminates args list.
	//
	// Returns a list of propositions as strings.
	//
	// A proposition is a string with specific trails:
	//
	//   - a space to acknowledge a full proposition.
	//   - a tab and a help text.
	//
	// e.g.: "--section=" without space to let user set value right after equal.
	// e.g.: "--verbose " with space to move on next flag.
	// e.g.: "convert \tsub-command help" tab and help message.
	//
	// Use [Proposition] to format a proposition.
	// Use [Formatter] to present proposition.
	Complete(args []string) []string
}

Completer defines an object producing propositions from arguments.

type Formatter

type Formatter interface {
	// Generates configuration for the shell to interpret proposition.
	Configuration(command, completer string) string
	// Format a proposition for specific shell.
	Format(flag, proposition string, unique bool) string
}

Formatter render proposition for a specific shell

The formatting of proposition is tightly coupled with configuration. For example, zsh configuration is a shell function processing propositions as formatted by go completer.

func Shell

func Shell(name string) Formatter

Shell instanciate formatter for a known shell.

Accepts bash, fish or zsh. Returns nil otherwise.

type PropositionsFunc

type PropositionsFunc func() ([]string, bool)

PropositionsFunc implements [proposer] with a func.

func (PropositionsFunc) Propose

func (f PropositionsFunc) Propose() ([]string, bool)

func (PropositionsFunc) String

func (f PropositionsFunc) String() string

type Valuer

type Valuer interface {
	// Name of the flag for which values are produced.
	String() string
	// Propose values for command line argument.
	//
	// Each values is a regular [Proposition].
	Propose(current string) []string
}

Valuer generates propositions for a flag.

func PFlagBools

func PFlagBools(fs *pflag.FlagSet) []Valuer

PFlagBools generates proposers for boolean flags

Jump to

Keyboard shortcuts

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