vendor/github.com/spf13/cobra/README.md
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
child 256 6d9efbef00a9
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
     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 
   150 
   155 
   151 ```go
   156 ```go
   152 package main
   157 package main
   153 
   158 
   154 import (
   159 import (
   155   "fmt"
       
   156   "os"
       
   157 
       
   158   "{pathToYourApp}/cmd"
   160   "{pathToYourApp}/cmd"
   159 )
   161 )
   160 
   162 
   161 func main() {
   163 func main() {
   162   cmd.Execute()
   164   cmd.Execute()
   263 
   265 
   264 ```go
   266 ```go
   265 package main
   267 package main
   266 
   268 
   267 import (
   269 import (
   268   "fmt"
       
   269   "os"
       
   270 
       
   271   "{pathToYourApp}/cmd"
   270   "{pathToYourApp}/cmd"
   272 )
   271 )
   273 
   272 
   274 func main() {
   273 func main() {
   275   cmd.Execute()
   274   cmd.Execute()
   337 ### Local Flags
   336 ### Local Flags
   338 
   337 
   339 A flag can also be assigned locally which will only apply to that specific command.
   338 A flag can also be assigned locally which will only apply to that specific command.
   340 
   339 
   341 ```go
   340 ```go
   342 rootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
   341 localCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
   343 ```
   342 ```
   344 
   343 
   345 ### Local Flag on Parent Commands
   344 ### Local Flag on Parent Commands
   346 
   345 
   347 By default Cobra only parses local flags on the target command, any local flags on
   346 By default Cobra only parses local flags on the target command, any local flags on
   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`)