Building a Go CLI with Cobra: commands, subcommands and flags

Cobra is the Go library behind the CLIs of kubectl, hugo and gh. You describe a tree of commands, and Cobra parses arguments, handles POSIX-style flags (through pflag), generates --help output and shell completions, and suggests a command when the user mistypes one.
This post was checked against Cobra v1.10.2.
Minimal app by hand
Section titled “Minimal app by hand”Cobra doesn’t need a generator. A single file is enough to see how it fits together:
mkdir greet && cd greetgo mod init example.com/greetgo get github.com/spf13/cobra@latestpackage main
import ( "fmt" "os"
"github.com/spf13/cobra")
func main() { var name string var verbose bool
rootCmd := &cobra.Command{ Use: "greet", Short: "A tiny example CLI", }
// Persistent flags are inherited by every subcommand. rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
helloCmd := &cobra.Command{ Use: "hello [extra words...]", Short: "Say hello", Args: cobra.ArbitraryArgs, Run: func(cmd *cobra.Command, args []string) { if verbose { fmt.Fprintln(os.Stderr, "running hello") } fmt.Printf("Hello, %s! %v\n", name, args) }, }
// Local flag: only valid on "hello". helloCmd.Flags().StringVarP(&name, "name", "n", "world", "who to greet")
rootCmd.AddCommand(helloCmd)
if err := rootCmd.Execute(); err != nil { os.Exit(1) }}go run . hello --name Gophergo run . hello -n Gopher -vgo run . helo # Error: unknown command "helo" ... Did you mean this? hellogo run . hello --helpExecute() already prints the error and usage, so main only needs to set the exit code.
Scaffolding with cobra-cli
Section titled “Scaffolding with cobra-cli”For larger apps, the generator lays out one file per command. It moved out of the main repo into cobra-cli. The old go get github.com/spf13/cobra/cobra command from older tutorials no longer works.
go install github.com/spf13/cobra-cli@latest
mkdir my-cli-app && cd my-cli-appgo mod init example.com/my-cli-appcobra-cli initcobra-cli init must run inside a Go module. It takes the package name from go.mod, runs go get for Cobra, and creates main.go and cmd/root.go. Pass --license apache (or mit, etc.) to add a license header and file.
Add commands with cobra-cli add:
cobra-cli add servecobra-cli add configcobra-cli add create -p 'configCmd'Each command gets its own file under cmd/. The -p flag sets the parent command. It takes the Go variable name of the parent, and the generator appends Cmd to the name, so the parent of create is configCmd, not config. The result is my-cli-app config create.
A generated command looks roughly like this. You fill in Run and register flags in init():
var port int
var serveCmd = &cobra.Command{ Use: "serve", Short: "Start the server", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("listening on :%d\n", port) },}
func init() { rootCmd.AddCommand(serveCmd) serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "port to listen on")}cmd.Flags()are local to that command.cmd.PersistentFlags()apply to the command and all its subcommands.- Bind flags to variables with the
...Var/...VarPfunctions (StringVarP,IntVarP,BoolVarP). ThePvariants take a one-letter shorthand. - If you don’t bind a variable, read the value inside
Runwithcmd.Flags().GetString("name"). It returns(string, error), and the error is non-nil only if the flag doesn’t exist or has a different type. - Mark a flag as mandatory with
cmd.MarkFlagRequired("name"). - Validate positional arguments with
Args:cobra.NoArgs,cobra.ExactArgs(1),cobra.MinimumNArgs(1), or your own function.
Gotchas
Section titled “Gotchas”- Use
RunEinstead ofRunwhen the command can fail. Returning an error lets Cobra print it and makesExecute()return it, instead of you callingos.Exitdeep inside a command. - By default, Cobra prints the full usage text on every error, which is noisy for runtime failures. Setting
SilenceUsage: trueon the root command turns that off. A common pattern is to setcmd.SilenceUsage = trueinPersistentPreRun, after flags have parsed, so usage still appears for bad flags. - Cobra adds a
completioncommand automatically.my-cli-app completion bash(orzsh,fish,powershell) prints a completion script.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)