vendor/github.com/spf13/cobra/user_guide.md
changeset 256 6d9efbef00a9
parent 251 1c52a0eeb952
child 260 445e01aede7e
equal deleted inserted replaced
255:4f153a23adab 256:6d9efbef00a9
       
     1 # User Guide
       
     2 
       
     3 While you are welcome to provide your own organization, typically a Cobra-based
       
     4 application will follow the following organizational structure:
       
     5 
       
     6 ```
       
     7   ▾ appName/
       
     8     ▾ cmd/
       
     9         add.go
       
    10         your.go
       
    11         commands.go
       
    12         here.go
       
    13       main.go
       
    14 ```
       
    15 
       
    16 In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.
       
    17 
       
    18 ```go
       
    19 package main
       
    20 
       
    21 import (
       
    22   "{pathToYourApp}/cmd"
       
    23 )
       
    24 
       
    25 func main() {
       
    26   cmd.Execute()
       
    27 }
       
    28 ```
       
    29 
       
    30 ## Using the Cobra Generator
       
    31 
       
    32 Cobra provides its own program that will create your application and add any
       
    33 commands you want. It's the easiest way to incorporate Cobra into your application.
       
    34 
       
    35 [Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it.
       
    36 
       
    37 ## Using the Cobra Library
       
    38 
       
    39 To manually implement Cobra you need to create a bare main.go file and a rootCmd file.
       
    40 You will optionally provide additional commands as you see fit.
       
    41 
       
    42 ### Create rootCmd
       
    43 
       
    44 Cobra doesn't require any special constructors. Simply create your commands.
       
    45 
       
    46 Ideally you place this in app/cmd/root.go:
       
    47 
       
    48 ```go
       
    49 var rootCmd = &cobra.Command{
       
    50   Use:   "hugo",
       
    51   Short: "Hugo is a very fast static site generator",
       
    52   Long: `A Fast and Flexible Static Site Generator built with
       
    53                 love by spf13 and friends in Go.
       
    54                 Complete documentation is available at http://hugo.spf13.com`,
       
    55   Run: func(cmd *cobra.Command, args []string) {
       
    56     // Do Stuff Here
       
    57   },
       
    58 }
       
    59 
       
    60 func Execute() {
       
    61   if err := rootCmd.Execute(); err != nil {
       
    62     fmt.Fprintln(os.Stderr, err)
       
    63     os.Exit(1)
       
    64   }
       
    65 }
       
    66 ```
       
    67 
       
    68 You will additionally define flags and handle configuration in your init() function.
       
    69 
       
    70 For example cmd/root.go:
       
    71 
       
    72 ```go
       
    73 package cmd
       
    74 
       
    75 import (
       
    76 	"fmt"
       
    77 	"os"
       
    78 
       
    79 	"github.com/spf13/cobra"
       
    80 	"github.com/spf13/viper"
       
    81 )
       
    82 
       
    83 var (
       
    84 	// Used for flags.
       
    85 	cfgFile     string
       
    86 	userLicense string
       
    87 
       
    88 	rootCmd = &cobra.Command{
       
    89 		Use:   "cobra",
       
    90 		Short: "A generator for Cobra based Applications",
       
    91 		Long: `Cobra is a CLI library for Go that empowers applications.
       
    92 This application is a tool to generate the needed files
       
    93 to quickly create a Cobra application.`,
       
    94 	}
       
    95 )
       
    96 
       
    97 // Execute executes the root command.
       
    98 func Execute() error {
       
    99 	return rootCmd.Execute()
       
   100 }
       
   101 
       
   102 func init() {
       
   103 	cobra.OnInitialize(initConfig)
       
   104 
       
   105 	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
       
   106 	rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
       
   107 	rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
       
   108 	rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
       
   109 	viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
       
   110 	viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
       
   111 	viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
       
   112 	viper.SetDefault("license", "apache")
       
   113 
       
   114 	rootCmd.AddCommand(addCmd)
       
   115 	rootCmd.AddCommand(initCmd)
       
   116 }
       
   117 
       
   118 func initConfig() {
       
   119 	if cfgFile != "" {
       
   120 		// Use config file from the flag.
       
   121 		viper.SetConfigFile(cfgFile)
       
   122 	} else {
       
   123 		// Find home directory.
       
   124 		home, err := os.UserHomeDir()
       
   125 		cobra.CheckErr(err)
       
   126 
       
   127 		// Search config in home directory with name ".cobra" (without extension).
       
   128 		viper.AddConfigPath(home)
       
   129 		viper.SetConfigType("yaml")
       
   130 		viper.SetConfigName(".cobra")
       
   131 	}
       
   132 
       
   133 	viper.AutomaticEnv()
       
   134 
       
   135 	if err := viper.ReadInConfig(); err == nil {
       
   136 		fmt.Println("Using config file:", viper.ConfigFileUsed())
       
   137 	}
       
   138 }
       
   139 ```
       
   140 
       
   141 ### Create your main.go
       
   142 
       
   143 With the root command you need to have your main function execute it.
       
   144 Execute should be run on the root for clarity, though it can be called on any command.
       
   145 
       
   146 In a Cobra app, typically the main.go file is very bare. It serves one purpose: to initialize Cobra.
       
   147 
       
   148 ```go
       
   149 package main
       
   150 
       
   151 import (
       
   152   "{pathToYourApp}/cmd"
       
   153 )
       
   154 
       
   155 func main() {
       
   156   cmd.Execute()
       
   157 }
       
   158 ```
       
   159 
       
   160 ### Create additional commands
       
   161 
       
   162 Additional commands can be defined and typically are each given their own file
       
   163 inside of the cmd/ directory.
       
   164 
       
   165 If you wanted to create a version command you would create cmd/version.go and
       
   166 populate it with the following:
       
   167 
       
   168 ```go
       
   169 package cmd
       
   170 
       
   171 import (
       
   172   "fmt"
       
   173 
       
   174   "github.com/spf13/cobra"
       
   175 )
       
   176 
       
   177 func init() {
       
   178   rootCmd.AddCommand(versionCmd)
       
   179 }
       
   180 
       
   181 var versionCmd = &cobra.Command{
       
   182   Use:   "version",
       
   183   Short: "Print the version number of Hugo",
       
   184   Long:  `All software has versions. This is Hugo's`,
       
   185   Run: func(cmd *cobra.Command, args []string) {
       
   186     fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
       
   187   },
       
   188 }
       
   189 ```
       
   190 
       
   191 ### Returning and handling errors
       
   192 
       
   193 If you wish to return an error to the caller of a command, `RunE` can be used.
       
   194 
       
   195 ```go
       
   196 package cmd
       
   197 
       
   198 import (
       
   199   "fmt"
       
   200 
       
   201   "github.com/spf13/cobra"
       
   202 )
       
   203 
       
   204 func init() {
       
   205   rootCmd.AddCommand(tryCmd)
       
   206 }
       
   207 
       
   208 var tryCmd = &cobra.Command{
       
   209   Use:   "try",
       
   210   Short: "Try and possibly fail at something",
       
   211   RunE: func(cmd *cobra.Command, args []string) error {
       
   212     if err := someFunc(); err != nil {
       
   213 	return err
       
   214     }
       
   215     return nil
       
   216   },
       
   217 }
       
   218 ```
       
   219 
       
   220 The error can then be caught at the execute function call.
       
   221 
       
   222 ## Working with Flags
       
   223 
       
   224 Flags provide modifiers to control how the action command operates.
       
   225 
       
   226 ### Assign flags to a command
       
   227 
       
   228 Since the flags are defined and used in different locations, we need to
       
   229 define a variable outside with the correct scope to assign the flag to
       
   230 work with.
       
   231 
       
   232 ```go
       
   233 var Verbose bool
       
   234 var Source string
       
   235 ```
       
   236 
       
   237 There are two different approaches to assign a flag.
       
   238 
       
   239 ### Persistent Flags
       
   240 
       
   241 A flag can be 'persistent', meaning that this flag will be available to the
       
   242 command it's assigned to as well as every command under that command. For
       
   243 global flags, assign a flag as a persistent flag on the root.
       
   244 
       
   245 ```go
       
   246 rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
       
   247 ```
       
   248 
       
   249 ### Local Flags
       
   250 
       
   251 A flag can also be assigned locally, which will only apply to that specific command.
       
   252 
       
   253 ```go
       
   254 localCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
       
   255 ```
       
   256 
       
   257 ### Local Flag on Parent Commands
       
   258 
       
   259 By default, Cobra only parses local flags on the target command, and any local flags on
       
   260 parent commands are ignored. By enabling `Command.TraverseChildren`, Cobra will
       
   261 parse local flags on each command before executing the target command.
       
   262 
       
   263 ```go
       
   264 command := cobra.Command{
       
   265   Use: "print [OPTIONS] [COMMANDS]",
       
   266   TraverseChildren: true,
       
   267 }
       
   268 ```
       
   269 
       
   270 ### Bind Flags with Config
       
   271 
       
   272 You can also bind your flags with [viper](https://github.com/spf13/viper):
       
   273 ```go
       
   274 var author string
       
   275 
       
   276 func init() {
       
   277   rootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")
       
   278   viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
       
   279 }
       
   280 ```
       
   281 
       
   282 In this example, the persistent flag `author` is bound with `viper`.
       
   283 **Note**: the variable `author` will not be set to the value from config,
       
   284 when the `--author` flag is not provided by user.
       
   285 
       
   286 More in [viper documentation](https://github.com/spf13/viper#working-with-flags).
       
   287 
       
   288 ### Required flags
       
   289 
       
   290 Flags are optional by default. If instead you wish your command to report an error
       
   291 when a flag has not been set, mark it as required:
       
   292 ```go
       
   293 rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
       
   294 rootCmd.MarkFlagRequired("region")
       
   295 ```
       
   296 
       
   297 Or, for persistent flags:
       
   298 ```go
       
   299 rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
       
   300 rootCmd.MarkPersistentFlagRequired("region")
       
   301 ```
       
   302 
       
   303 ## Positional and Custom Arguments
       
   304 
       
   305 Validation of positional arguments can be specified using the `Args` field
       
   306 of `Command`.
       
   307 
       
   308 The following validators are built in:
       
   309 
       
   310 - `NoArgs` - the command will report an error if there are any positional args.
       
   311 - `ArbitraryArgs` - the command will accept any args.
       
   312 - `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`.
       
   313 - `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args.
       
   314 - `MaximumNArgs(int)` - the command will report an error if there are more than N positional args.
       
   315 - `ExactArgs(int)` - the command will report an error if there are not exactly N positional args.
       
   316 - `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`
       
   317 - `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.
       
   318 
       
   319 An example of setting the custom validator:
       
   320 
       
   321 ```go
       
   322 var cmd = &cobra.Command{
       
   323   Short: "hello",
       
   324   Args: func(cmd *cobra.Command, args []string) error {
       
   325     if len(args) < 1 {
       
   326       return errors.New("requires a color argument")
       
   327     }
       
   328     if myapp.IsValidColor(args[0]) {
       
   329       return nil
       
   330     }
       
   331     return fmt.Errorf("invalid color specified: %s", args[0])
       
   332   },
       
   333   Run: func(cmd *cobra.Command, args []string) {
       
   334     fmt.Println("Hello, World!")
       
   335   },
       
   336 }
       
   337 ```
       
   338 
       
   339 ## Example
       
   340 
       
   341 In the example below, we have defined three commands. Two are at the top level
       
   342 and one (cmdTimes) is a child of one of the top commands. In this case the root
       
   343 is not executable, meaning that a subcommand is required. This is accomplished
       
   344 by not providing a 'Run' for the 'rootCmd'.
       
   345 
       
   346 We have only defined one flag for a single command.
       
   347 
       
   348 More documentation about flags is available at https://github.com/spf13/pflag
       
   349 
       
   350 ```go
       
   351 package main
       
   352 
       
   353 import (
       
   354   "fmt"
       
   355   "strings"
       
   356 
       
   357   "github.com/spf13/cobra"
       
   358 )
       
   359 
       
   360 func main() {
       
   361   var echoTimes int
       
   362 
       
   363   var cmdPrint = &cobra.Command{
       
   364     Use:   "print [string to print]",
       
   365     Short: "Print anything to the screen",
       
   366     Long: `print is for printing anything back to the screen.
       
   367 For many years people have printed back to the screen.`,
       
   368     Args: cobra.MinimumNArgs(1),
       
   369     Run: func(cmd *cobra.Command, args []string) {
       
   370       fmt.Println("Print: " + strings.Join(args, " "))
       
   371     },
       
   372   }
       
   373 
       
   374   var cmdEcho = &cobra.Command{
       
   375     Use:   "echo [string to echo]",
       
   376     Short: "Echo anything to the screen",
       
   377     Long: `echo is for echoing anything back.
       
   378 Echo works a lot like print, except it has a child command.`,
       
   379     Args: cobra.MinimumNArgs(1),
       
   380     Run: func(cmd *cobra.Command, args []string) {
       
   381       fmt.Println("Echo: " + strings.Join(args, " "))
       
   382     },
       
   383   }
       
   384 
       
   385   var cmdTimes = &cobra.Command{
       
   386     Use:   "times [string to echo]",
       
   387     Short: "Echo anything to the screen more times",
       
   388     Long: `echo things multiple times back to the user by providing
       
   389 a count and a string.`,
       
   390     Args: cobra.MinimumNArgs(1),
       
   391     Run: func(cmd *cobra.Command, args []string) {
       
   392       for i := 0; i < echoTimes; i++ {
       
   393         fmt.Println("Echo: " + strings.Join(args, " "))
       
   394       }
       
   395     },
       
   396   }
       
   397 
       
   398   cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
       
   399 
       
   400   var rootCmd = &cobra.Command{Use: "app"}
       
   401   rootCmd.AddCommand(cmdPrint, cmdEcho)
       
   402   cmdEcho.AddCommand(cmdTimes)
       
   403   rootCmd.Execute()
       
   404 }
       
   405 ```
       
   406 
       
   407 For a more complete example of a larger application, please checkout [Hugo](http://gohugo.io/).
       
   408 
       
   409 ## Help Command
       
   410 
       
   411 Cobra automatically adds a help command to your application when you have subcommands.
       
   412 This will be called when a user runs 'app help'. Additionally, help will also
       
   413 support all other commands as input. Say, for instance, you have a command called
       
   414 'create' without any additional configuration; Cobra will work when 'app help
       
   415 create' is called.  Every command will automatically have the '--help' flag added.
       
   416 
       
   417 ### Example
       
   418 
       
   419 The following output is automatically generated by Cobra. Nothing beyond the
       
   420 command and flag definitions are needed.
       
   421 
       
   422     $ cobra help
       
   423 
       
   424     Cobra is a CLI library for Go that empowers applications.
       
   425     This application is a tool to generate the needed files
       
   426     to quickly create a Cobra application.
       
   427 
       
   428     Usage:
       
   429       cobra [command]
       
   430 
       
   431     Available Commands:
       
   432       add         Add a command to a Cobra Application
       
   433       help        Help about any command
       
   434       init        Initialize a Cobra Application
       
   435 
       
   436     Flags:
       
   437       -a, --author string    author name for copyright attribution (default "YOUR NAME")
       
   438           --config string    config file (default is $HOME/.cobra.yaml)
       
   439       -h, --help             help for cobra
       
   440       -l, --license string   name of license for the project
       
   441           --viper            use Viper for configuration (default true)
       
   442 
       
   443     Use "cobra [command] --help" for more information about a command.
       
   444 
       
   445 
       
   446 Help is just a command like any other. There is no special logic or behavior
       
   447 around it. In fact, you can provide your own if you want.
       
   448 
       
   449 ### Defining your own help
       
   450 
       
   451 You can provide your own Help command or your own template for the default command to use
       
   452 with following functions:
       
   453 
       
   454 ```go
       
   455 cmd.SetHelpCommand(cmd *Command)
       
   456 cmd.SetHelpFunc(f func(*Command, []string))
       
   457 cmd.SetHelpTemplate(s string)
       
   458 ```
       
   459 
       
   460 The latter two will also apply to any children commands.
       
   461 
       
   462 ## Usage Message
       
   463 
       
   464 When the user provides an invalid flag or invalid command, Cobra responds by
       
   465 showing the user the 'usage'.
       
   466 
       
   467 ### Example
       
   468 You may recognize this from the help above. That's because the default help
       
   469 embeds the usage as part of its output.
       
   470 
       
   471     $ cobra --invalid
       
   472     Error: unknown flag: --invalid
       
   473     Usage:
       
   474       cobra [command]
       
   475 
       
   476     Available Commands:
       
   477       add         Add a command to a Cobra Application
       
   478       help        Help about any command
       
   479       init        Initialize a Cobra Application
       
   480 
       
   481     Flags:
       
   482       -a, --author string    author name for copyright attribution (default "YOUR NAME")
       
   483           --config string    config file (default is $HOME/.cobra.yaml)
       
   484       -h, --help             help for cobra
       
   485       -l, --license string   name of license for the project
       
   486           --viper            use Viper for configuration (default true)
       
   487 
       
   488     Use "cobra [command] --help" for more information about a command.
       
   489 
       
   490 ### Defining your own usage
       
   491 You can provide your own usage function or template for Cobra to use.
       
   492 Like help, the function and template are overridable through public methods:
       
   493 
       
   494 ```go
       
   495 cmd.SetUsageFunc(f func(*Command) error)
       
   496 cmd.SetUsageTemplate(s string)
       
   497 ```
       
   498 
       
   499 ## Version Flag
       
   500 
       
   501 Cobra adds a top-level '--version' flag if the Version field is set on the root command.
       
   502 Running an application with the '--version' flag will print the version to stdout using
       
   503 the version template. The template can be customized using the
       
   504 `cmd.SetVersionTemplate(s string)` function.
       
   505 
       
   506 ## PreRun and PostRun Hooks
       
   507 
       
   508 It is possible to run functions before or after the main `Run` function of your command. The `PersistentPreRun` and `PreRun` functions will be executed before `Run`. `PersistentPostRun` and `PostRun` will be executed after `Run`.  The `Persistent*Run` functions will be inherited by children if they do not declare their own.  These functions are run in the following order:
       
   509 
       
   510 - `PersistentPreRun`
       
   511 - `PreRun`
       
   512 - `Run`
       
   513 - `PostRun`
       
   514 - `PersistentPostRun`
       
   515 
       
   516 An example of two commands which use all of these features is below.  When the subcommand is executed, it will run the root command's `PersistentPreRun` but not the root command's `PersistentPostRun`:
       
   517 
       
   518 ```go
       
   519 package main
       
   520 
       
   521 import (
       
   522   "fmt"
       
   523 
       
   524   "github.com/spf13/cobra"
       
   525 )
       
   526 
       
   527 func main() {
       
   528 
       
   529   var rootCmd = &cobra.Command{
       
   530     Use:   "root [sub]",
       
   531     Short: "My root command",
       
   532     PersistentPreRun: func(cmd *cobra.Command, args []string) {
       
   533       fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)
       
   534     },
       
   535     PreRun: func(cmd *cobra.Command, args []string) {
       
   536       fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)
       
   537     },
       
   538     Run: func(cmd *cobra.Command, args []string) {
       
   539       fmt.Printf("Inside rootCmd Run with args: %v\n", args)
       
   540     },
       
   541     PostRun: func(cmd *cobra.Command, args []string) {
       
   542       fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)
       
   543     },
       
   544     PersistentPostRun: func(cmd *cobra.Command, args []string) {
       
   545       fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)
       
   546     },
       
   547   }
       
   548 
       
   549   var subCmd = &cobra.Command{
       
   550     Use:   "sub [no options!]",
       
   551     Short: "My subcommand",
       
   552     PreRun: func(cmd *cobra.Command, args []string) {
       
   553       fmt.Printf("Inside subCmd PreRun with args: %v\n", args)
       
   554     },
       
   555     Run: func(cmd *cobra.Command, args []string) {
       
   556       fmt.Printf("Inside subCmd Run with args: %v\n", args)
       
   557     },
       
   558     PostRun: func(cmd *cobra.Command, args []string) {
       
   559       fmt.Printf("Inside subCmd PostRun with args: %v\n", args)
       
   560     },
       
   561     PersistentPostRun: func(cmd *cobra.Command, args []string) {
       
   562       fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args)
       
   563     },
       
   564   }
       
   565 
       
   566   rootCmd.AddCommand(subCmd)
       
   567 
       
   568   rootCmd.SetArgs([]string{""})
       
   569   rootCmd.Execute()
       
   570   fmt.Println()
       
   571   rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
       
   572   rootCmd.Execute()
       
   573 }
       
   574 ```
       
   575 
       
   576 Output:
       
   577 ```
       
   578 Inside rootCmd PersistentPreRun with args: []
       
   579 Inside rootCmd PreRun with args: []
       
   580 Inside rootCmd Run with args: []
       
   581 Inside rootCmd PostRun with args: []
       
   582 Inside rootCmd PersistentPostRun with args: []
       
   583 
       
   584 Inside rootCmd PersistentPreRun with args: [arg1 arg2]
       
   585 Inside subCmd PreRun with args: [arg1 arg2]
       
   586 Inside subCmd Run with args: [arg1 arg2]
       
   587 Inside subCmd PostRun with args: [arg1 arg2]
       
   588 Inside subCmd PersistentPostRun with args: [arg1 arg2]
       
   589 ```
       
   590 
       
   591 ## Suggestions when "unknown command" happens
       
   592 
       
   593 Cobra will print automatic suggestions when "unknown command" errors happen. This allows Cobra to behave similarly to the `git` command when a typo happens. For example:
       
   594 
       
   595 ```
       
   596 $ hugo srever
       
   597 Error: unknown command "srever" for "hugo"
       
   598 
       
   599 Did you mean this?
       
   600         server
       
   601 
       
   602 Run 'hugo --help' for usage.
       
   603 ```
       
   604 
       
   605 Suggestions are automatic based on every subcommand registered and use an implementation of [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion.
       
   606 
       
   607 If you need to disable suggestions or tweak the string distance in your command, use:
       
   608 
       
   609 ```go
       
   610 command.DisableSuggestions = true
       
   611 ```
       
   612 
       
   613 or
       
   614 
       
   615 ```go
       
   616 command.SuggestionsMinimumDistance = 1
       
   617 ```
       
   618 
       
   619 You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don't want aliases. Example:
       
   620 
       
   621 ```
       
   622 $ kubectl remove
       
   623 Error: unknown command "remove" for "kubectl"
       
   624 
       
   625 Did you mean this?
       
   626         delete
       
   627 
       
   628 Run 'kubectl help' for usage.
       
   629 ```
       
   630 
       
   631 ## Generating documentation for your command
       
   632 
       
   633 Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).
       
   634 
       
   635 ## Generating shell completions
       
   636 
       
   637 Cobra can generate a shell-completion file for the following shells: bash, zsh, fish, PowerShell. If you add more information to your commands, these completions can be amazingly powerful and flexible.  Read more about it in [Shell Completions](shell_completions.md).