Categorygithub.com/mitchellh/cli
modulepackage
1.1.5
Repository: https://github.com/mitchellh/cli.git
Documentation: pkg.go.dev

# README

Go CLI Library GoDoc

cli is a library for implementing command-line interfaces in Go. cli is the library that powers the CLI for Packer, Consul, Vault, Terraform, Nomad, and more.

Features

  • Easy sub-command based CLIs: cli foo, cli bar, etc.

  • Support for nested subcommands such as cli foo bar.

  • Optional support for default subcommands so cli does something other than error.

  • Support for shell autocompletion of subcommands, flags, and arguments with callbacks in Go. You don't need to write any shell code.

  • Automatic help generation for listing subcommands.

  • Automatic help flag recognition of -h, --help, etc.

  • Automatic version flag recognition of -v, --version.

  • Helpers for interacting with the terminal, such as outputting information, asking for input, etc. These are optional, you can always interact with the terminal however you choose.

  • Use of Go interfaces/types makes augmenting various parts of the library a piece of cake.

Example

Below is a simple example of creating and running a CLI

package main

import (
	"log"
	"os"

	"github.com/mitchellh/cli"
)

func main() {
	c := cli.NewCLI("app", "1.0.0")
	c.Args = os.Args[1:]
	c.Commands = map[string]cli.CommandFactory{
		"foo": fooCommandFactory,
		"bar": barCommandFactory,
	}

	exitStatus, err := c.Run()
	if err != nil {
		log.Println(err)
	}

	os.Exit(exitStatus)
}

# Functions

BasicHelpFunc generates some basic help output that is usually good enough for most CLI applications.
FilteredHelpFunc will filter the commands to only include the keys in the include parameter.
NewClI returns a new CLI instance with sensible defaults.
NewMockUi returns a fully initialized MockUi instance which is safe for concurrent use.

# Constants

RunResultHelp is a value that can be returned from Run to signal to the CLI to render the help output.

# Variables

A list of colors that are useful.
A list of colors that are useful.
A list of colors that are useful.
A list of colors that are useful.
A list of colors that are useful.
A list of colors that are useful.
A list of colors that are useful.

# Structs

BasicUi is an implementation of Ui that just outputs to the given writer.
CLI contains the state necessary to run subcommands and parse the command line arguments.
ColoredUi is a Ui implementation that colors its output according to the given color schemes for the given type of output.
ConcurrentUi is a wrapper around a Ui interface (and implements that interface) making the underlying Ui concurrency safe.
MockCommand is an implementation of Command that can be used for tests.
MockCommandAutocomplete is an implementation of CommandAutocomplete.
MockCommandHelpTemplate is an implementation of CommandHelpTemplate.
MockUi is a mock UI that is used for tests and is exported publicly for use in external tests if needed as well.
PrefixedUi is an implementation of Ui that prefixes messages.
UiColor is a posix shell color code to use.
UiWriter is an io.Writer implementation that can be used with loggers that writes every line of log output data to a Ui at the Info level.

# Interfaces

A command is a runnable sub-command of a CLI.
CommandAutocomplete is an extension of Command that enables fine-grained autocompletion.
CommandHelpTemplate is an extension of Command that also has a function for returning a template for the help rather than the help itself.
Ui is an interface for interacting with the terminal, or "interface" of a CLI.

# Type aliases

CommandFactory is a type of function that is a factory for commands.
HelpFunc is the type of the function that is responsible for generating the help output when the CLI must show the general help text.