package
0.0.0-20240814004006-a9ca4b1d939d
Repository: https://github.com/ipinfo/cli.git
Documentation: pkg.go.dev

# README

complete

GoDoc

Writing bash completion scripts is a hard work, usually done in the bash scripting language. This package can help produce a binary which will serve as the auto-completion input/output facility: your shell will run your binary everytime completion is required with the input being the user's current command line, and the binary's output being the suggested completions.

Installation

Supported shells:

  • bash
  • zsh
  • fish

The installation of completion for a command line tool is done automatically by this library by running the command line tool with the COMP_INSTALL environment variable set. Uninstalling the completion is similarly done by the COMP_UNINSTALL environment variable. For example, if a tool called my-cli uses this library, the completion can install by running COMP_INSTALL=1 my-cli.

Example

import (
 	"flag"
 	"github.com/ipinfo/cli/lib/complete"
 	"github.com/ipinfo/cli/lib/complete/predict"
)

var (
 	// Add variables to the program.
 	name      = flag.String("name", "", "")
 	something = flag.String("something", "", "")
 	nothing   = flag.String("nothing", "", "")
)

func main() {
 	// Create the complete command.
 	// Here we define completion values for each flag.
 	cmd := &complete.Command{
	 	Flags: map[string]complete.Predictor{
 			"name":      predict.Set{"foo", "bar", "foo bar"},
 			"something": predict.Something,
 			"nothing":   predict.Nothing,
 		},
 	}
 	// Run the completion - provide it with the binary name.
 	cmd.Complete("my-program")
 	// Parse the flags.
 	flag.Parse()
 	// Program logic...
}

# Packages

Package install provide installation functions of command completion.
Package predict provides helper functions for completion predictors.

# Functions

Complete the command line arguments for the given command in the case that the program was invoked with COMP_LINE and COMP_POINT environment variables.
Parse parses a typed command line argument list, and returns a list of arguments.
Test is a testing helper function for testing bash completion of a given completer.

# Structs

Arg is typed a command line argument.
Command is an object that can be used to create complete options for a go executable that does not have a good binding to the `Completer` interface, or to use a Go program as complete binary for another executable.
Parsed contains information about the argument.
No description provided by the author

# Interfaces

Completer is an interface that a command line should implement in order to get bash completion.
Predictor can predict completion options.

# Type aliases

PredictFunc is a function that implements the Predictor interface.