PluginBench
Skill
Pass
Audit score 90

golang-spf13-cobra

samber/cc-skills-golang

Golang CLI command tree library with subcommands, flags, validation, completions, and doc generation.

What is golang-spf13-cobra?

spf13/cobra is the standard Go library for building command-line interfaces with nested subcommands, flag parsing, argument validation, and shell completion. Use it when building or extending a CLI application that needs a professional command tree, help text, and Unix-native behavior.

  • Define command trees with root and nested subcommands using cobra.Command and AddCommand
  • Validate positional arguments with built-in validators (NoArgs, ExactArgs, MatchAll) or custom functions before RunE executes
  • Parse flags with persistent (inherited by subcommands) and local flag scopes via pflag integration
  • Execute hooks in order: PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE, with error handling via *E variants
  • Generate shell completions dynamically via ValidArgsFunction and RegisterFlagCompletionFunc with ShellCompDirective control
  • Customize usage and help text templates, and auto-generate man pages and markdown documentation

How to install golang-spf13-cobra

npx skills add https://github.com/samber/cc-skills-golang --skill golang-spf13-cobra
Prerequisites
  • Go installed and configured
  • github.com/spf13/cobra imported in your project (install via `go get github.com/spf13/cobra@latest`)
Claude Code
Cursor
Windsurf
Cline

How to use golang-spf13-cobra

  1. 1.Define a root cobra.Command with Use, Short, and SilenceUsage/SilenceErrors set appropriately
  2. 2.Register subcommands to the root using AddCommand; use AddGroup before AddCommand to organize help output
  3. 3.Implement PersistentPreRunE on the root for config initialization and auth checks that run before every subcommand
  4. 4.Set Args validators (NoArgs, ExactArgs, MatchAll, or custom) on commands to validate positional arguments before RunE
  5. 5.Use PersistentFlags() for inherited flags and Flags() for command-local flags; bind to viper in PersistentPreRunE if needed
  6. 6.Implement RunE handlers using cmd.OutOrStdout() and cmd.ErrOrStderr() for testable output, never os.Stdout directly
  7. 7.Add ValidArgsFunction and RegisterFlagCompletionFunc for dynamic shell completions with ShellCompDirective control
  8. 8.Test commands by creating a fresh command tree per test, calling SetArgs and SetOut, then Execute()

Use cases

Good for
  • Building a new CLI tool from scratch with multiple subcommands and inherited flags
  • Adding dynamic shell completion to an existing cobra CLI based on runtime state or external data
  • Validating command-line arguments and flags before business logic runs, with standard error messages
  • Organizing large CLIs into command groups for clearer help output and user navigation
  • Testing CLI commands programmatically by setting args and capturing output without touching os.Stdout directly
Who it's for
  • Go developers building command-line tools or extending existing cobra CLIs
  • DevOps engineers creating multi-command utilities with configuration and subcommand hierarchies
  • Contributors to Go projects that already import github.com/spf13/cobra

golang-spf13-cobra FAQ

Should I use cobra alone or with viper?

Use cobra alone if you only need flags and args. Use cobra with viper when you need both CLI flags and configuration file/environment resolution; bind flags to viper in PersistentPreRunE. See the golang-spf13-viper skill for viper integration.

Why should I use RunE instead of Run?

Run cannot return errors — the only escape is os.Exit or panic, which bypasses defers and cleanup. Always use RunE variants (*E) to handle errors properly.

How do I validate positional arguments?

Use the Args field with built-in validators (NoArgs, ExactArgs, MinimumNArgs, RangeArgs) or custom functions. Never check len(args) inside RunE; Args validators run first and provide standard error messages.

How do I make flags inherited by subcommands?

Use PersistentFlags() on the parent command; local flags use Flags(). Child PersistentPreRunE replaces the parent's — call the parent explicitly if you need both.

How do I test a cobra command without printing to stdout?

Use cmd.SetOut(buffer) and cmd.SetArgs([]string{...}) before Execute(), then read from the buffer. Never use os.Stdout directly in handlers — use cmd.OutOrStdout() instead.

Full instructions (SKILL.md)

Source of truth, from samber/cc-skills-golang.


name: golang-spf13-cobra description: "Golang CLI command tree library using spf13/cobra — cobra.Command, RunE vs Run, PersistentPreRunE hook chain, Args validators (NoArgs, ExactArgs, MatchAll, custom), persistent vs local flags, command groups, ValidArgsFunction, RegisterFlagCompletionFunc, ShellCompDirective, usage/help template customization, man-page and markdown doc generation, and testing with SetArgs/SetOut/SetErr. Apply when using or adopting spf13/cobra, or when the codebase imports github.com/spf13/cobra. For configuration layering alongside cobra, see the samber/cc-skills-golang@golang-spf13-viper skill. For general CLI architecture (project layout, exit codes, signal handling, I/O patterns), see samber/cc-skills-golang@golang-cli." user-invocable: true license: MIT compatibility: Designed for Claude Code, Codex or similar harness, and for projects using Golang. metadata: author: samber version: "1.1.2" openclaw: emoji: "🐍" homepage: https://github.com/samber/cc-skills-golang requires: bins: - go install: [] skill-library-version: "1.10.2" allowed-tools: Read Edit Write Glob Grep Bash(go:) Bash(golangci-lint:) Bash(git:) Agent WebFetch mcp__context7__resolve-library-id mcp__context7__query-docs Bash(godig:) Bash(gopls:) LSP mcp__gopls__ paths:

  • "**/*.go"

Persona: You are a Go CLI engineer building command trees that feel native to the Unix shell. You design the user-facing surface first, then wire behavior into the right hook.

Modes:

  • Build — creating a new CLI from scratch: follow command tree setup, hook wiring, and flag sections sequentially.
  • Extend — adding subcommands, flags, or completions to an existing CLI: read the current command tree first, then apply changes consistent with the existing structure.
  • Review — auditing an existing CLI: check the Common Mistakes table, verify RunE usage, OutOrStdout(), hook chain ordering, and args validation.

Using spf13/cobra for CLI command trees in Go

Cobra is the de facto standard for Go CLI applications. It provides the command/subcommand tree, flag parsing (via pflag), args validation, shell completion generation, and documentation generation. It does not handle configuration layering — that's viper's job.

Official Resources:

This skill is not exhaustive — refer to library documentation and code examples for more information:

  • For Go package docs, symbols, versions, importers, and known vulnerabilities, → See samber/cc-skills-golang@golang-pkg-go-dev skill (godig), preferred over Context7 for Go package facts.
  • To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See samber/cc-skills-golang@golang-gopls skill (gopls).
  • Context7 remains a fallback for docs not indexed on pkg.go.dev.
go get github.com/spf13/cobra@latest

Cobra vs. viper

These libraries do fundamentally different things and can be used independently.

Concerncobraviper
OwnsCommand tree, flags, arg validation, completionsConfiguration value resolution
User-facing?Yes — subcommands, flags, help textNo — purely a key-value resolver
Without the other?Yes — a CLI with flags only needs cobraYes — a daemon reading YAML + env needs only viper
Integration seamHands pflag.Flag to viper via BindPFlagTreats the cobra flag as the highest-precedence layer

Use cobra alone when your binary takes flags and args but needs no config file or env resolution. Use viper alone when you have a long-running service reading config from YAML + env with no CLI subcommands. Use both when you need both — bind at PersistentPreRunE on the root command.

→ See samber/cc-skills-golang@golang-spf13-viper for the viper side of this integration.

Command tree

Every cobra CLI has a root command plus zero or more subcommands registered with AddCommand. The root command name is the binary name.

var rootCmd = &cobra.Command{
    Use:          "myapp",
    Short:        "One-line summary",
    SilenceUsage: true,  // ✓ prevents usage wall on every error
    SilenceErrors: true, // ✓ lets you control error output format
}

Use AddGroup to label subcommands in help output — register groups before the AddCommand calls that reference them; cobra does not retroactively assign groups.

The Run* family

Cobra commands have five run hooks executed in order:

PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE

Always use *E variants — the non-E forms cannot return errors. Key rules:

  • PersistentPreRunE on the root runs before every subcommand — use it for config init and auth checks.
  • A child PersistentPreRunE replaces the parent's entirely — call the parent explicitly if you need both.
  • PostRunE runs only if RunE succeeded.

For the full lifecycle and inheritance rules, see commands-and-args.md.

Args validators

Cobra validates positional arguments before RunE runs. Never write len(args) checks inside RunE — that bypasses cobra's standard error messages and arg count tracking.

Built-ins: NoArgs, ExactArgs(n), MinimumNArgs(n), MaximumNArgs(n), RangeArgs(min,max), OnlyValidArgs, ExactValidArgs(n). Compose with MatchAll(v1, v2). Custom validator: func(cmd *cobra.Command, args []string) error.

For the full validator set with examples and MatchAll patterns, see commands-and-args.md.

Flags primer

Cobra delegates flag parsing to pflag. Persistent flags (PersistentFlags()) are inherited by all subcommands; local flags (Flags()) apply only to the declaring command.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path") // inherited by all subcommands
serveCmd.Flags().IntVar(&port, "port", 8080, "listen port")                     // local to serveCmd only
serveCmd.MarkFlagRequired("port")
serveCmd.MarkFlagsMutuallyExclusive("json", "yaml")

For pflag types, custom flag values, flag groups, and viper binding, see flags.md.

Completions primer

Cobra generates shell completions automatically. Extend them with:

  • ValidArgs []string — static positional arg completion.
  • ValidArgsFunction — dynamic: func(cmd, args, toComplete string) ([]string, ShellCompDirective). Return ShellCompDirectiveNoFileComp to suppress file fallback.
  • RegisterFlagCompletionFunc(name, fn) — flag value completion.

For ShellCompDirective values, annotations, and testing, see completions.md.

Testing commands

Test commands by executing them programmatically. Never use os.Stdout / os.Stderr directly in command handlers — use cmd.OutOrStdout() / cmd.ErrOrStderr() so tests can redirect output.

func TestServeCmd(t *testing.T) {
    buf := new(bytes.Buffer)
    rootCmd.SetOut(buf)
    rootCmd.SetArgs([]string{"serve", "--port", "9090"})
    require.NoError(t, rootCmd.Execute())
    assert.Contains(t, buf.String(), "listening on :9090")
}

Cobra accumulates flag state across Execute() calls — build a fresh command tree per test. For isolation patterns, golden files, and testing completions, see testing.md.

Best Practices

  1. Always use RunE, never Run — Run cannot return an error; the only escape is os.Exit or panic, bypassing defers.
  2. Put config initialization in PersistentPreRunE — it runs before every subcommand; the right place for viper binding and auth checks.
  3. Validate positional args with Args, not inside RunE — Args gives cobra's standard error messages; MatchAll composes validators.
  4. Use cmd.OutOrStdout() / cmd.ErrOrStderr() for all output — direct os.Stdout writes cannot be captured by tests.
  5. Re-create the command tree per test — cobra accumulates flag state across Execute() calls on the same instance.

Common Mistakes

MistakeWhy it failsFix
Using Run instead of RunECannot return an error — only escape is os.Exit or panic, bypassing defersUse RunE — return the error, let cobra handle the exit
Writing len(args) checks in RunEBypasses cobra's standard error messages ("accepts 1 arg, received 2")Declare Args: cobra.ExactArgs(1) on the command
Writing to os.Stdout directlyTests cannot capture output — os-level file handles can't be redirectedUse cmd.OutOrStdout() / cmd.ErrOrStderr()
Child PersistentPreRunE silently drops parent'sCobra does not chain — the child replaces the parent's hook entirelyCall parent.PersistentPreRunE(cmd, args) from the child's hook
Reusing a root command across testsCobra accumulates flag state; second Execute() sees flags from the firstBuild a fresh command tree per test

Further Reading

  • commands-and-args.md — full PreRun*/PostRun* chain, every Args validator, PersistentPreRunE inheritance rules
  • flags.md — pflag types, required/exclusive/oneRequired groups, custom value types, viper binding
  • completions.md — ShellCompDirective set, annotation-based completions, testing completions
  • generators.md — man page, markdown, YAML, RST doc generation; cobra-cli scaffolder
  • testing.md — isolation patterns, golden files, testing completions, table-driven command tests

Cross-References

  • → See samber/cc-skills-golang@golang-cli skill for general CLI architecture — project layout, exit codes, signal handling, I/O patterns
  • → See samber/cc-skills-golang@golang-spf13-viper skill for configuration layering alongside cobra (flag → env → file → default precedence)
  • → See samber/cc-skills-golang@golang-testing skill for general Go testing patterns

If you encounter a bug or unexpected behavior in spf13/cobra, open an issue at https://github.com/spf13/cobra/issues.