Skip to content
A Go library for implementing command-line interfaces.
Go Makefile
Branch: master
Clone or download

Latest commit

mitchellh Merge pull request #82 from mitchellh/noop-autocomplete
Noop autocomplete setup if we're not autocompleting
Latest commit 171fbb5 Apr 6, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Update go versions used in CI Mar 23, 2020
LICENSE Add MPL2 license Dec 10, 2013
Makefile Update go versions used in CI Mar 23, 2020
README.md update README for autocompletion Jul 17, 2017
autocomplete.go address feedback, remove TODO Jul 17, 2017
cli.go Noop autocomplete setup if we're not autocompleting Apr 3, 2020
cli_test.go Noop autocomplete setup if we're not autocompleting Apr 3, 2020
command.go Change from interface to a configuration key on CLI Sep 8, 2017
command_mock.go
command_mock_test.go Initial import from Serf, tests pass, works somewhat Nov 3, 2013
go.mod Update go versions used in CI Mar 23, 2020
go.sum Add go.mod and lets do v1.0.0 Aug 24, 2018
help.go Consistent capital 'U' in "Usage" Feb 1, 2017
ui.go Use cross-platform deps over crypto/terminal Dec 30, 2015
ui_colored.go Switch to using fatih/color for ANSI color handling. Apr 5, 2018
ui_concurrent.go add AskSecret for unechoed input Apr 29, 2015
ui_concurrent_test.go Add concurrnet Ui Nov 5, 2013
ui_mock.go Fix discrepancy between BasicUi and MockUi Dec 25, 2017
ui_mock_test.go Fix discrepancy between BasicUi and MockUi Dec 25, 2017
ui_test.go Fix discrepancy between BasicUi and MockUi Dec 25, 2017
ui_writer.go fix potential panic [GH-26] Jan 8, 2016
ui_writer_test.go fix potential panic [GH-26] Jan 8, 2016

README.md

Go CLI Library GoDoc

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

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)
}
You can’t perform that action at this time.