spf13/cobra
Developer ToolsA Go library for building POSIX-compliant CLI apps with subcommands, flags, help and shell completion.
Overview
Cobra is a Go library for building command-line applications around commands, arguments and flags. Its structure maps naturally to APPNAME COMMAND ARG --FLAG, so applications read like sentences; the README uses hugo server --port=1313 and git clone URL --bare as examples.
It covers subcommand trees, POSIX-compliant short and long flags (via pflag), persistent and local flags, command aliases, typo suggestions, automatic help output, shell completion for bash/zsh/fish/PowerShell, man page generation, and optional integration with viper for 12-factor configuration. It is aimed at Go developers who need a conventional CLI structure rather than hand-rolled argument parsing. Large projects such as Kubernetes, Hugo and GitHub CLI use it, and a longer list is maintained in the repository.
Getting started
You need a working Go toolchain and a Go module for your application.
-
Add the library to your module:
go get -u github.com/spf13/cobra@latest -
Import it in your code:
import "github.com/spf13/cobra" -
Define a root command and any subcommands, then call
Execute(). The exact command/flag definitions are covered in the Cobra user guide and thecobra.Commandreference.
For scaffolding instead of hand-writing files, install the separate generator:
go install github.com/spf13/cobra-cli@latest
Its usage is documented in the Cobra-CLI README; the README does not reproduce the generator's commands here. More complete documentation is available at cobra.dev.
Constraints
- The library targets Go applications; the installation commands assume Go modules are in use.
- Flag behavior follows pflag, a POSIX-oriented fork of the standard
flagpackage, so semantics may differ from packages built on the standard library alone. - The generator
cobra-cliis a separate repository and must be installed independently of the library. - Cobra is released under the Apache 2.0 license; check
LICENSE.txtfor the full terms.
HotRepo's take
On 2026-11-06, Cobra appeared on the Go daily list with 128 stars gained and a best position of 20 for that day. There is no all-language daily ranking recorded for the project and no discussion thread was found, so the available evidence is a single language-list snapshot. That says little about long-term trends, but it does confirm that this long-standing Go CLI library was still seeing daily attention in the Go ecosystem at that time.
Written by AI from the project's README, updated . The project's own docs are the source of truth.