Help Command

Cobra automatically adds a help command to your application when you have subcommands.
This will be called when a user runs ‘app help’. Additionally, help will also
support all other commands as input. Say, for instance, you have a command called
‘create’ without any additional configuration; Cobra will work when ‘app help
create’ is called. Every command will automatically have the ‘—help’ flag added.

Example

The following output is automatically generated by Cobra. Nothing beyond the
command and flag definitions are needed.

  1. $ cobra help
  2. Cobra is a CLI library for Go that empowers applications.
  3. This application is a tool to generate the needed files
  4. to quickly create a Cobra application.
  5. Usage:
  6. cobra [command]
  7. Available Commands:
  8. add Add a command to a Cobra Application
  9. help Help about any command
  10. init Initialize a Cobra Application
  11. Flags:
  12. -a, --author string author name for copyright attribution (default "YOUR NAME")
  13. --config string config file (default is $HOME/.cobra.yaml)
  14. -h, --help help for cobra
  15. -l, --license string name of license for the project
  16. --viper use Viper for configuration (default true)
  17. Use "cobra [command] --help" for more information about a command.

Help is just a command like any other. There is no special logic or behavior
around it. In fact, you can provide your own if you want.

Defining your own help

You can provide your own Help command or your own template for the default command to use
with following functions:

  1. cmd.SetHelpCommand(cmd *Command)
  2. cmd.SetHelpFunc(f func(*Command, []string))
  3. cmd.SetHelpTemplate(s string)

The latter two will also apply to any children commands.