Usage Message

When the user provides an invalid flag or invalid command, Cobra responds by
showing the user the ‘usage’.

Example

You may recognize this from the help above. That’s because the default help
embeds the usage as part of its output.

  1. $ cobra --invalid
  2. Error: unknown flag: --invalid
  3. Usage:
  4. cobra [command]
  5. Available Commands:
  6. add Add a command to a Cobra Application
  7. help Help about any command
  8. init Initialize a Cobra Application
  9. Flags:
  10. -a, --author string author name for copyright attribution (default "YOUR NAME")
  11. --config string config file (default is $HOME/.cobra.yaml)
  12. -h, --help help for cobra
  13. -l, --license string name of license for the project
  14. --viper use Viper for configuration (default true)
  15. Use "cobra [command] --help" for more information about a command.

Defining your own usage

You can provide your own usage function or template for Cobra to use.
Like help, the function and template are overridable through public methods:

  1. cmd.SetUsageFunc(f func(*Command) error)
  2. cmd.SetUsageTemplate(s string)