Automatically print usage for Golang Cobra CLI sub-commands

When you run a Cobra (Golang CLI) application, it will print out nicely-formatted usage information derived from the root command. However it is up to you to print out subsequent usage information for commands underneath and their subcommands.

$ go run main.go
Various functionality for doing things from the CLI.

Usage:
  your-binary [command]

Available Commands:
  ...

By accessing fields in the command and the sub-commands you can print similar usage information as the root command.

$ go run main.go exp
Testing various functionality for this application.

Usage:
 your-binary test [subcommand]

Available Subcommands:
  delete  Delete a test
  release Release a test

Add comment