golang-spf13-cobra

Build and test Go CLI command trees using the spf13/cobra library.

1|Updated Jun 2, 2026
One-click install
npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-spf13-cobra-verifiedorganic
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-spf13-cobra
Source: https://github.com/VerifiedOrganic/onboard/tree/main/.agents/skills/golang-spf13-cobra
Command: npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-spf13-cobra-verifiedorganic

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers building command-line tools with spf13/cobra repeatedly hit the same pitfalls: using Run instead of RunE, writing manual len(args) checks, printing to os.Stdout so tests cannot capture output, and losing parent PersistentPreRunE hooks when a child overrides them. This Skill encodes the correct patterns for command trees, hooks, args validators, flags, completions, and testing. ## Core Features & Use Cases - Command tree and hook guidance: Covers the PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE lifecycle, inheritance rules, command groups, and SilenceUsage/SilenceErrors configuration. - Flags, validators, and completions: Explains persistent vs local flags, MarkFlagsMutuallyExclusive/RequiredTogether/OneRequired, StringArray vs StringSlice, MatchAll validator composition, ValidArgsFunction, and RegisterFlagCompletionFunc. - Testing patterns: Shows SetArgs/SetOut/SetErr test harnesses, fresh command tree per test for isolation, golden files, and completion testing via __complete. - Use Case: When adding a 'delete' subcommand that requires exactly one validated resource argument, the Skill directs you to declare Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs) instead of hand-rolled checks inside RunE. ## Quick Start Ask the agent to review or scaffold a cobra command in your Go project, for example by requesting a new subcommand with proper RunE error handling, args validation, and testable output.

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 the non-zero exit.

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 runs validators before RunE and prints a standard error message. Compose multiple validators with cobra.MatchAll.

What is the difference between cobra and viper?▼

Cobra owns the command tree, flags, arg validation, and shell completions, while viper resolves configuration values from files, env vars, and defaults. They integrate via viper.BindPFlag, typically in the root command's PersistentPreRunE, and each works independently.

Why does my cobra test see flag values from a previous test?▼

Cobra accumulates flag state across Execute() calls on the same command instance. Build a fresh command tree per test using a constructor function like newRootCmd() rather than reusing a global root command.

How do I add dynamic shell completion to a cobra flag or argument?▼

Use ValidArgsFunction for positional args and RegisterFlagCompletionFunc for flag values. Both return ([]string, cobra.ShellCompDirective); return ShellCompDirectiveNoFileComp to suppress file fallback when your list is exhaustive.

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

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