Cobra
Library for creating powerful Go CLI applications with subcommands, flags, and generated help.
Official Documentation ↗v1.8. Check the official docs for the latest API surface.Installation
go get github.com/spf13/cobra@latestMethods
No methods match this filter.
Library for creating powerful Go CLI applications with subcommands, flags, and generated help.
Overview
Cobra structures CLAs around commands: each cobra.Command declares usage, flags, argument validation, and a run function, and commands nest into subcommand trees. Help, usage errors, and shell completion generate from those declarations.
Persistent flags flow down the tree for global options, hooks like PersistentPreRun set up logging or config once, and Args validators reject bad input before your code runs. It pairs naturally with Viper for flag, env, and file configuration.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
cobra.Command— Declares a command with usage text, description, and the entry function to run.AddCommand— Attaches subcommands; each carries its own flags, help, and Run function.Flags— Defines local or persistent flags; persistent flags propagate to all subcommands.Args validation— Validates positional argument count and values with built-in validators before Run executes.
Usage Notes
- Use RunE everywhere and centralize exit-code handling in main.
- Put shared setup in PersistentPreRunE so subcommands inherit it exactly once.
- Combine Args validators with ValidArgs to get precise usage errors for free.
Common Pitfalls
- Flags read before flag parsing completes, in init or package vars, see default values only.
- Swallowing Execute errors in main yields exit code 0 on failures and breaks CI.
- Flag name collisions between persistent and local flags shadow one another silently.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cobra.dev/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:56.317Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Battle-Tested Production-Grade Reliability POSIX-compliant flag parsing ensures universal compatibility, while extensive validation systems prevent runtime errors. (https://cobra.dev/)
- Learn More doc.GenMarkdownTree(rootCmd, "./docs") “ class=“text-warning”> Generating markdown documentation… (https://cobra.dev/)
- Learn More fang.Run(rootCmd) “ class=“text-warning”> 🎨 Converting CLI to interactive TUI… (https://cobra.dev/)
FAQ
- Run versus RunE, which do I use?
- RunE returns an error that Execute propagates to main for a single exit-code policy. Run handles its own errors. Prefer RunE so every failure path funnels through one place and prints consistently.
- What is the difference between Flags and PersistentFlags?
- Flags defined on a command apply only to it. PersistentFlags apply to the command and every subcommand beneath it, which is how global options like --config or --verbose reach nested commands.
- How do I disable the completion command?
- Set rootCmd.CompletionOptions.DisableDefaultCmd = true, or use HiddenDefaultCmd to keep it functional but out of help. The generated completion is useful, so hide rather than disable when in doubt.
Report a problem with this API docOpens a pre-filled issue on GitHub.