Go Academy · Lesson

Subcommands and Command Groups

Building multi-level CLI hierarchies

Lesson 3 of 413 steps

Subcommands and Command Groups is a free Go Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

Hierarchical commands

Cobra supports arbitrary nesting: root → subcommand → sub-subcommand. Users call app resource action like kubectl get pods.

Adding subcommands

Build each subcommand as a separate cobra.Command and register it with its parent:

var userCmd = &cobra.Command{Use: "user", Short: "Manage users"}
var userCreateCmd = &cobra.Command{Use: "create", Short: "Create user", RunE: createUser}
func init() {
    userCmd.AddCommand(userCreateCmd)
    rootCmd.AddCommand(userCmd)
}

Group display (Cobra v1.6+)

Assign commands to display groups in the help output for better organisation:

rootCmd.AddGroup(&cobra.Group{ID: "mgmt", Title: "Management Commands"})
userCmd.GroupID = "mgmt"

Aliases

Add aliases so users can use short or alternative names for commands:

var userCmd = &cobra.Command{
    Use:     "user",
    Aliases: []string{"users", "u"},
}

Hidden commands

Mark debugging or internal commands as hidden so they do not appear in the help but still work:

cmd.Hidden = true

Deprecated commands

Mark old commands as deprecated to display a warning when used:

cmd.Deprecated = "use 'new-command' instead"

Pre and Post hooks

Use PersistentPreRunE and PostRunE for setup/teardown that applies to a command and all its subcommands (e.g., initialising a DB connection before any subcommand runs).

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
    return initDB()
}

Disabling help for leaf commands

Cobra adds help as a subcommand by default. Disable it if your app has its own help command or the subcommand structure would be confusing.

rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

Accessing parent command

Inside a Run function, cmd.Parent() returns the parent command, useful for accessing flags defined on the parent.

Command traversal

Use rootCmd.Commands() to iterate all registered subcommands. Useful for programmatic introspection or auto-generating documentation.

Man page and markdown generation

Cobra can generate man pages and markdown documentation for all commands with the cobra/doc package:

doc.GenMarkdownTree(rootCmd, "./docs")

Quick Check

How do you make a Cobra subcommand available under multiple names?

Recap: Subcommands and Groups

Key points:

  • AddCommand nests commands hierarchically
  • GroupID organises help display in Cobra v1.6+
  • Aliases for short forms; Hidden for internal commands
  • PersistentPreRunE for shared setup across subcommands
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Subcommands and Command Groups” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Subcommands and Command Groups”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Subcommands and Command Groups”?

Building multi-level CLI hierarchies You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Subcommands and Command Groups” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cobra Fundamentals
  2. Flags and Persistent Flags
  3. Subcommands and Command Groups
  4. Distributing CLI Tools
← Back to Go Academy