Subcommands and Command Groups
Building multi-level CLI hierarchies
Subcommands and Command Groups is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 = trueDeprecated 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
Frequently asked questions
Is the “Subcommands and Command Groups” lesson free?
Yes — the full text of “Subcommands and Command Groups” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- Cobra Fundamentals
- Flags and Persistent Flags
- Subcommands and Command Groups
- Distributing CLI Tools