Find your next open-source project

Search by name, purpose, or keyword — or paste a GitHub repository URL

spf13/cobra

Developer Tools

A Go library for building POSIX-compliant CLI apps with subcommands, flags, help and shell completion.

44,845
4,478
Go
Apache-2.0
13 years old

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.

  1. Add the library to your module:

    go get -u github.com/spf13/cobra@latest
    
  2. Import it in your code:

    import "github.com/spf13/cobra"
    
  3. Define a root command and any subcommands, then call Execute(). The exact command/flag definitions are covered in the Cobra user guide and the cobra.Command reference.

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 flag package, so semantics may differ from packages built on the standard library alone.
  • The generator cobra-cli is a separate repository and must be installed independently of the library.
  • Cobra is released under the Apache 2.0 license; check LICENSE.txt for 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.

Hacker News

Cobra v1.5.0 Is Released
▲ 4 · 0 comments · June 21, 2022
Cobra: A Commander for Modern Go CLI Interactions
▲ 3 · 0 comments · March 2, 2018
Cobra: Build Modern CLI Apps in Go
▲ 2 · 0 comments · April 18, 2016