golang-spf13-cobra

Builds and reviews Go CLI command trees using the spf13/cobra library.

Updated Jun 30, 2026
One-click install
npx skills add https://github.com/santoshkal/chezmoi --skill golang-spf13-cobra-santoshkal
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-spf13-cobra
Source: https://github.com/santoshkal/chezmoi/tree/main/private_dot_config/opencode/skills/Golang/skills/golang-spf13-cobra
Command: npx skills add https://github.com/santoshkal/chezmoi --skill golang-spf13-cobra-santoshkal

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires github.com/spf13/cobra, and includes references (resource) components.

What problem does it solve? Go developers building CLIs with spf13/cobra frequently misuse its API: using Run instead of RunE, writing manual len(args) checks, printing to os.Stdout, or losing parent hooks when a child defines PersistentPreRunE. This Skill encodes the correct patterns so generated and reviewed cobra code follows the library's intended design. ## Core Features & Use Cases - Command tree construction: root command setup, AddCommand/AddGroup ordering, SilenceUsage/SilenceErrors, and the full PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE hook chain with inheritance rules. - Args and flags: built-in validators (NoArgs, ExactArgs, MatchAll), persistent vs local flags, StringArray vs StringSlice, MarkFlagsMutuallyExclusive/RequiredTogether/OneRequired, and Changed() for explicit-value detection. - Completions, docs, and testing: ValidArgsFunction, RegisterFlagCompletionFunc, ShellCompDirective, man/markdown doc generation, cobra-cli scaffolding, and test isolation with SetArgs/SetOut/SetErr. - Use Case: Ask the agent to add a 'delete' subcommand requiring exactly one validated resource argument with dynamic shell completion, and it produces idiomatic cobra code with ExactArgs, ValidArgsFunction, and RunE error propagation. ## Quick Start Ask the agent to create or review a cobra-based Go CLI command, for example to add a subcommand with validated arguments, flag constraints, and shell completion.

Frequently Asked Questions about golang-spf13-cobra

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Should I use Run or RunE in a cobra command?▼

Always use RunE. Run cannot return an error, so the only escape is os.Exit or panic, which bypasses deferred cleanup. RunE returns the error to cobra, which handles exit codes and error output.

How do I validate positional arguments in cobra?▼

Set the Args field on the command, for example Args: cobra.ExactArgs(1), instead of checking len(args) inside RunE. Cobra validates before RunE runs and prints a standard error message. Compose validators with cobra.MatchAll.

What is the difference between cobra and viper?▼

Cobra owns the command tree, flags, arg validation, and shell completions. Viper handles layered configuration resolution from files, env vars, and defaults. They integrate via viper.BindPFlag but can be used independently.

Why does my child command's PersistentPreRunE skip the root's hook?▼

A child's PersistentPreRunE replaces the parent's entirely; cobra does not chain them. Call the parent's hook explicitly from inside the child's hook if both must run.

How do I test cobra commands without flag state leaking between tests?▼

Build a fresh command tree per test using a constructor function like newRootCmd(). Cobra accumulates flag state across Execute() calls on the same instance, so reusing a global root command causes values to leak between tests.

How do I add dynamic shell completion for cobra flag values?▼

Use RegisterFlagCompletionFunc for flag values and ValidArgsFunction for positional args. Return cobra.ShellCompDirectiveNoFileComp to suppress file fallback, and ShellCompDirectiveError when the completion lookup fails.