1# Go CLI Library [![GoDoc](https://godoc.org/github.com/mitchellh/cli?status.png)](https://godoc.org/github.com/mitchellh/cli) 2 3cli is a library for implementing powerful command-line interfaces in Go. 4cli is the library that powers the CLI for 5[Packer](https://github.com/mitchellh/packer), 6[Serf](https://github.com/hashicorp/serf), 7[Consul](https://github.com/hashicorp/consul), 8[Vault](https://github.com/hashicorp/vault), 9[Terraform](https://github.com/hashicorp/terraform), and 10[Nomad](https://github.com/hashicorp/nomad). 11 12## Features 13 14* Easy sub-command based CLIs: `cli foo`, `cli bar`, etc. 15 16* Support for nested subcommands such as `cli foo bar`. 17 18* Optional support for default subcommands so `cli` does something 19 other than error. 20 21* Automatic help generation for listing subcommands 22 23* Automatic help flag recognition of `-h`, `--help`, etc. 24 25* Automatic version flag recognition of `-v`, `--version`. 26 27* Helpers for interacting with the terminal, such as outputting information, 28 asking for input, etc. These are optional, you can always interact with the 29 terminal however you choose. 30 31* Use of Go interfaces/types makes augmenting various parts of the library a 32 piece of cake. 33 34## Example 35 36Below is a simple example of creating and running a CLI 37 38```go 39package main 40 41import ( 42 "log" 43 "os" 44 45 "github.com/mitchellh/cli" 46) 47 48func main() { 49 c := cli.NewCLI("app", "1.0.0") 50 c.Args = os.Args[1:] 51 c.Commands = map[string]cli.CommandFactory{ 52 "foo": fooCommandFactory, 53 "bar": barCommandFactory, 54 } 55 56 exitStatus, err := c.Run() 57 if err != nil { 58 log.Println(err) 59 } 60 61 os.Exit(exitStatus) 62} 63``` 64 65