]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | # Go CLI Library [![GoDoc](https://godoc.org/github.com/mitchellh/cli?status.png)](https://godoc.org/github.com/mitchellh/cli) |
2 | ||
3 | cli is a library for implementing powerful command-line interfaces in Go. | |
4 | cli 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 | * Support for shell autocompletion of subcommands, flags, and arguments | |
22 | with callbacks in Go. You don't need to write any shell code. | |
23 | ||
24 | * Automatic help generation for listing subcommands | |
25 | ||
26 | * Automatic help flag recognition of `-h`, `--help`, etc. | |
27 | ||
28 | * Automatic version flag recognition of `-v`, `--version`. | |
29 | ||
30 | * Helpers for interacting with the terminal, such as outputting information, | |
31 | asking for input, etc. These are optional, you can always interact with the | |
32 | terminal however you choose. | |
33 | ||
34 | * Use of Go interfaces/types makes augmenting various parts of the library a | |
35 | piece of cake. | |
36 | ||
37 | ## Example | |
38 | ||
39 | Below is a simple example of creating and running a CLI | |
40 | ||
41 | ```go | |
42 | package main | |
43 | ||
44 | import ( | |
45 | "log" | |
46 | "os" | |
47 | ||
48 | "github.com/mitchellh/cli" | |
49 | ) | |
50 | ||
51 | func main() { | |
52 | c := cli.NewCLI("app", "1.0.0") | |
53 | c.Args = os.Args[1:] | |
54 | c.Commands = map[string]cli.CommandFactory{ | |
55 | "foo": fooCommandFactory, | |
56 | "bar": barCommandFactory, | |
57 | } | |
58 | ||
59 | exitStatus, err := c.Run() | |
60 | if err != nil { | |
61 | log.Println(err) | |
62 | } | |
63 | ||
64 | os.Exit(exitStatus) | |
65 | } | |
66 | ``` | |
67 |