1 ![cobra logo](https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png) |
1 ![cobra logo](https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png) |
2 |
2 |
3 Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files. |
3 Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files. |
4 |
4 |
5 Many of the most widely used Go projects are built using Cobra including: |
5 Many of the most widely used Go projects are built using Cobra, such as: |
6 |
6 [Kubernetes](http://kubernetes.io/), |
7 * [Kubernetes](http://kubernetes.io/) |
7 [Hugo](http://gohugo.io), |
8 * [Hugo](http://gohugo.io) |
8 [rkt](https://github.com/coreos/rkt), |
9 * [rkt](https://github.com/coreos/rkt) |
9 [etcd](https://github.com/coreos/etcd), |
10 * [etcd](https://github.com/coreos/etcd) |
10 [Moby (former Docker)](https://github.com/moby/moby), |
11 * [Moby (former Docker)](https://github.com/moby/moby) |
11 [Docker (distribution)](https://github.com/docker/distribution), |
12 * [Docker (distribution)](https://github.com/docker/distribution) |
12 [OpenShift](https://www.openshift.com/), |
13 * [OpenShift](https://www.openshift.com/) |
13 [Delve](https://github.com/derekparker/delve), |
14 * [Delve](https://github.com/derekparker/delve) |
14 [GopherJS](http://www.gopherjs.org/), |
15 * [GopherJS](http://www.gopherjs.org/) |
15 [CockroachDB](http://www.cockroachlabs.com/), |
16 * [CockroachDB](http://www.cockroachlabs.com/) |
16 [Bleve](http://www.blevesearch.com/), |
17 * [Bleve](http://www.blevesearch.com/) |
17 [ProjectAtomic (enterprise)](http://www.projectatomic.io/), |
18 * [ProjectAtomic (enterprise)](http://www.projectatomic.io/) |
18 [Giant Swarm's gsctl](https://github.com/giantswarm/gsctl), |
19 * [GiantSwarm's swarm](https://github.com/giantswarm/cli) |
19 [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack), |
20 * [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack) |
20 [rclone](http://rclone.org/), |
21 * [rclone](http://rclone.org/) |
21 [nehm](https://github.com/bogem/nehm), |
22 * [nehm](https://github.com/bogem/nehm) |
22 [Pouch](https://github.com/alibaba/pouch), |
23 * [Pouch](https://github.com/alibaba/pouch) |
23 [Istio](https://istio.io), |
|
24 [Prototool](https://github.com/uber/prototool), |
|
25 [mattermost-server](https://github.com/mattermost/mattermost-server), |
|
26 [Gardener](https://github.com/gardener/gardenctl), |
|
27 etc. |
24 |
28 |
25 [![Build Status](https://travis-ci.org/spf13/cobra.svg "Travis CI status")](https://travis-ci.org/spf13/cobra) |
29 [![Build Status](https://travis-ci.org/spf13/cobra.svg "Travis CI status")](https://travis-ci.org/spf13/cobra) |
26 [![CircleCI status](https://circleci.com/gh/spf13/cobra.png?circle-token=:circle-token "CircleCI status")](https://circleci.com/gh/spf13/cobra) |
30 [![CircleCI status](https://circleci.com/gh/spf13/cobra.png?circle-token=:circle-token "CircleCI status")](https://circleci.com/gh/spf13/cobra) |
27 [![GoDoc](https://godoc.org/github.com/spf13/cobra?status.svg)](https://godoc.org/github.com/spf13/cobra) |
31 [![GoDoc](https://godoc.org/github.com/spf13/cobra?status.svg)](https://godoc.org/github.com/spf13/cobra) |
28 |
32 |
43 * [Usage Message](#usage-message) |
47 * [Usage Message](#usage-message) |
44 * [PreRun and PostRun Hooks](#prerun-and-postrun-hooks) |
48 * [PreRun and PostRun Hooks](#prerun-and-postrun-hooks) |
45 * [Suggestions when "unknown command" happens](#suggestions-when-unknown-command-happens) |
49 * [Suggestions when "unknown command" happens](#suggestions-when-unknown-command-happens) |
46 * [Generating documentation for your command](#generating-documentation-for-your-command) |
50 * [Generating documentation for your command](#generating-documentation-for-your-command) |
47 * [Generating bash completions](#generating-bash-completions) |
51 * [Generating bash completions](#generating-bash-completions) |
|
52 * [Generating zsh completions](#generating-zsh-completions) |
48 - [Contributing](#contributing) |
53 - [Contributing](#contributing) |
49 - [License](#license) |
54 - [License](#license) |
50 |
55 |
51 # Overview |
56 # Overview |
52 |
57 |
393 - `ArbitraryArgs` - the command will accept any args. |
392 - `ArbitraryArgs` - the command will accept any args. |
394 - `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`. |
393 - `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`. |
395 - `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args. |
394 - `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args. |
396 - `MaximumNArgs(int)` - the command will report an error if there are more than N positional args. |
395 - `MaximumNArgs(int)` - the command will report an error if there are more than N positional args. |
397 - `ExactArgs(int)` - the command will report an error if there are not exactly N positional args. |
396 - `ExactArgs(int)` - the command will report an error if there are not exactly N positional args. |
|
397 - `ExactValidArgs(int)` - the command will report an error if there are not exactly N positional args OR if there are any positional args that are not in the `ValidArgs` field of `Command` |
398 - `RangeArgs(min, max)` - the command will report an error if the number of args is not between the minimum and maximum number of expected args. |
398 - `RangeArgs(min, max)` - the command will report an error if the number of args is not between the minimum and maximum number of expected args. |
399 |
399 |
400 An example of setting the custom validator: |
400 An example of setting the custom validator: |
401 |
401 |
402 ```go |
402 ```go |
403 var cmd = &cobra.Command{ |
403 var cmd = &cobra.Command{ |
404 Short: "hello", |
404 Short: "hello", |
405 Args: func(cmd *cobra.Command, args []string) error { |
405 Args: func(cmd *cobra.Command, args []string) error { |
406 if len(args) < 1 { |
406 if len(args) < 1 { |
407 return errors.New("requires at least one arg") |
407 return errors.New("requires a color argument") |
408 } |
408 } |
409 if myapp.IsValidColor(args[0]) { |
409 if myapp.IsValidColor(args[0]) { |
410 return nil |
410 return nil |
411 } |
411 } |
412 return fmt.Errorf("invalid color specified: %s", args[0]) |
412 return fmt.Errorf("invalid color specified: %s", args[0]) |
462 fmt.Println("Print: " + strings.Join(args, " ")) |
462 fmt.Println("Print: " + strings.Join(args, " ")) |
463 }, |
463 }, |
464 } |
464 } |
465 |
465 |
466 var cmdTimes = &cobra.Command{ |
466 var cmdTimes = &cobra.Command{ |
467 Use: "times [# times] [string to echo]", |
467 Use: "times [string to echo]", |
468 Short: "Echo anything to the screen more times", |
468 Short: "Echo anything to the screen more times", |
469 Long: `echo things multiple times back to the user by providing |
469 Long: `echo things multiple times back to the user by providing |
470 a count and a string.`, |
470 a count and a string.`, |
471 Args: cobra.MinimumNArgs(1), |
471 Args: cobra.MinimumNArgs(1), |
472 Run: func(cmd *cobra.Command, args []string) { |
472 Run: func(cmd *cobra.Command, args []string) { |
719 |
719 |
720 ## Generating bash completions |
720 ## Generating bash completions |
721 |
721 |
722 Cobra can generate a bash-completion file. If you add more information to your command, these completions can be amazingly powerful and flexible. Read more about it in [Bash Completions](bash_completions.md). |
722 Cobra can generate a bash-completion file. If you add more information to your command, these completions can be amazingly powerful and flexible. Read more about it in [Bash Completions](bash_completions.md). |
723 |
723 |
|
724 ## Generating zsh completions |
|
725 |
|
726 Cobra can generate zsh-completion file. Read more about it in |
|
727 [Zsh Completions](zsh_completions.md). |
|
728 |
724 # Contributing |
729 # Contributing |
725 |
730 |
726 1. Fork it |
731 1. Fork it |
727 2. Download your fork to your PC (`git clone https://github.com/your_username/cobra && cd cobra`) |
732 2. Download your fork to your PC (`git clone https://github.com/your_username/cobra && cd cobra`) |
728 3. Create your feature branch (`git checkout -b my-new-feature`) |
733 3. Create your feature branch (`git checkout -b my-new-feature`) |