Cobra

The commander library that powers Kubernetes, Hugo, and GitHub CLI's command-line interfaces.

Framework
Go
vv1.10.2
44,721 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
53 /100 Fair
Development Activity 16
Maintenance 20
Community 76
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
88 /100 Excellent
Architecture 88
Code Quality 85
Innovation 90
Learning Curve 95

Cobra is a Go library for building modern, POSIX-compliant command-line applications with nested subcommands, persistent and local flags, and automatic help generation. It provides both an interface for defining commands and a controller for organizing application code, following the APPNAME COMMAND ARG --FLAG pattern used by tools like git and go.

Cobra powers CLIs for some of the most widely used projects in the Go ecosystem, including Kubernetes, Hugo, and the GitHub CLI. Beyond command dispatch, it auto-generates shell completions for bash, zsh, fish, and PowerShell, plus man pages, Markdown, REST, and YAML documentation directly from the same command definitions.

What You Get

  • A Command struct with a full lifecycle (PersistentPreRun -> PreRun -> Run/RunE -> PostRun -> PersistentPostRun) for organizing CLI logic
  • POSIX-compliant flag parsing via the bundled pflag library, with cascading persistent and local flags
  • Auto-generated shell completions for bash, zsh, fish, and PowerShell
  • Auto-generated man pages, Markdown, REST, and YAML documentation from your command definitions
  • The cobra-cli generator to scaffold new CLI projects and commands

Common Use Cases

  • Building a git- or kubectl-style CLI with nested subcommands and per-command flags
  • Adding a controller layer that organizes a Go application’s command-line entry points
  • Shipping shell autocompletion alongside a CLI without hand-writing completion scripts
  • Generating consistent man pages and reference docs directly from command source

Under The Hood

Architecture

Cobra’s core abstraction is the Command struct (command.go, 2000+ lines) organized into a tree: each Command holds child Commands, its own pflag.FlagSet, and a chain of lifecycle hooks (PersistentPreRun -> PreRun -> Run/RunE -> PostRun -> PersistentPostRun). Execute() walks up to the root Command via Root(), then ExecuteC() resolves os.Args against the Command tree, parses flags through the embedded pflag.FlagSet, and dispatches into the matched command’s lifecycle chain. That same tree is reused as the single source of truth for shell completion generation (completions.go plus per-shell files for bash/zsh/fish/powershell) and documentation generation (the doc/ package, which walks Use/Short/Long/Example fields to emit man pages, Markdown, REST, and YAML docs). This ‘one struct drives execution, completion, and docs’ design is Cobra’s central architectural decision.

Tech Stack

go.mod declares module github.com/spf13/cobra targeting Go 1.15 for broad compatibility, with a deliberately minimal dependency set: github.com/spf13/pflag (Cobra’s own POSIX-compliant flag-parsing fork, doing the actual flag work), github.com/cpuguy83/go-md2man/v2 (Markdown-to-man-page conversion for generated docs), github.com/inconshreveable/mousetrap (a Windows-only check that warns users who double-click the binary instead of running it from a terminal), and go.yaml.in/yaml/v3 (YAML doc generation). There is no runtime framework dependency beyond pflag - the library compiles straight into consumer code, and a Makefile plus a GitHub Actions workflow drive linting, testing, and doc generation in CI.

Code Quality

The repository ships 11 _test.go files at the package root (command_test.go, args_test.go, completions_test.go, bash_completions_test.go, flag_groups_test.go, and more) plus a further test suite inside doc/ for the generated-docs package, covering command resolution, argument validation, flag groups, and every completion shell. Error handling is idiomatic Go - RunE/PreRunE/PersistentPreRunE variants return error rather than panicking, and Args validators (args.go) return descriptive errors on mismatch (ExactArgs, MinimumNArgs, OnlyValidArgs). Naming is consistent and exported identifiers are documented with godoc comments throughout; command.go is the one large file (2000+ lines) that centralizes most of the Command type’s behavior rather than splitting it across files, a defensible but monolithic choice for a struct this central.

API Design Cobra’s public API is declarative: a consumer builds cobra.Command{Use, Short, Long, RunE: ...} values and calls AddCommand/Execute, with almost no required boilerplate to get a working CLI with --help, -h, and command suggestions (‘did you mean…’) for free. Persistent and local flags cascade automatically from parent to child commands, and the same struct fields (Use, Short, Long, Example, ValidArgs) simultaneously drive --help output, shell completions, and generated docs, so documenting a command once documents it everywhere. The companion cobra-cli generator scaffolds new commands/projects, further lowering the barrier to a consistent project layout - this consistency is what most reviewers cite as Cobra’s strongest developer-experience trait.

Used by 63 apps in this directory

Go
81%
GPL 3.0

Navidrome

File Storage · Music Audio

24,331

Run your own personal Spotify — stream your entire music collection from any device, anywhere, forever.

View details
91
Repo Health
81
Technical
66
Dependency
Built with
Go 81%
JavaScript 15%
Updated 6 days ago
Go
95%
Other

NetBird

Security

30,175

Replace your VPN with a zero-trust WireGuard overlay network that auto-connects devices, enforces SSO and posture checks, and deploys in under 5 minutes.

View details
92
Repo Health
82
Technical
66
Dependency
Built with
Go 95%
Updated 5 days ago
Go
97%
Other

Netmaker

Automation · Security

11,836

Automate secure WireGuard mesh networks from homelab to enterprise scale without manual configuration.

View details
92
Repo Health
79
Technical
70
Dependency
Built with
Go 97%
Updated 6 days ago
Go
93%
MIT

NornicDB

AI Development · AI Memory · Databases

916

A single graph+vector+temporal database for AI workloads — Neo4j-compatible, sub-millisecond hybrid search, and built-in memory decay.

View details
80
Repo Health
82
Technical
71
Dependency
Built with
Go 93%
Updated 5 days ago
Go
63%
MIT

Obot

AI Agents · Mcp

1,120

An open-source MCP platform for organizations — host MCP servers, run MCP registries, monitor usage, and build agents and chatbots on top of the Model Context Protocol from one self-hosted deployment.

View details
88
Repo Health
70
Technical
70
Dependency
Built with
Go 63%
Svelte 21%
TypeScript 15%
Updated 6 days ago
Go
99%
Apache 2.0

Okteto

Developer Tools · Devops

3,554

Develop applications directly inside your Kubernetes cluster with real-time file sync and instant hot-reload — no more docker build/redeploy cycles.

View details
91
Repo Health
79
Technical
69
Dependency
Built with
Go 99%
Updated 6 days ago
Go
72%
MIT

Ollama

AI Development · Developer Tools

184,509

Run Llama, Gemma, DeepSeek, and other open LLMs on your own machine with one command and an OpenAI-compatible API.

View details
92
Repo Health
85
Technical
67
Dependency
Built with
Go 72%
C 19%
Updated 6 days ago
Python
64%
Other

Onyx

AI Agents · AI Assistants · Knowledge Management

32,935

Self-hostable AI platform with agentic RAG, 50+ connectors, deep research, code execution, and support for every major LLM provider.

View details
92
Repo Health
83
Technical
68
Dependency
Built with
Python 64%
TypeScript 26%
Updated 5 days ago
Go
52%
Apache 2.0

opencloud

File Storage

6,218

Open source file management and collaboration platform that keeps your data under your control, no database required.

View details
86
Repo Health
80
Technical
68
Dependency
Built with
Go 52%
Gherkin 33%
PHP 11%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers