golang-spf13-cobra

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Building a Go CLI with subcommands, flags, argument validation, and shell completions involves many cobra-specific pitfalls — using Run instead of RunE, writing manual len(args) checks, leaking flag state across tests, or losing the parent's PersistentPreRunE hook. This Skill encodes the correct patterns so generated cobra code follows the library's intended design. ## Core Features & Use Cases - Command tree and hook wiring: Guides root command setup, AddCommand/AddGroup ordering, the PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE lifecycle, and the child-hook-replaces-parent trap. - Flags, args, and completions: Covers persistent vs local flags, StringArray vs StringSlice, MarkFlagsMutuallyExclusive/RequiredTogether/OneRequired, Args validators composed with MatchAll, ValidArgsFunction, RegisterFlagCompletionFunc, and ShellCompDirective. - Testing and doc generation: Shows SetArgs/SetOut/SetErr test patterns, fresh command tree per test for isolation, golden files, and man-page/markdown doc generation via cobra/doc and cobra-cli. - Use Case: When adding a 'delete' subcommand that requires exactly one validated resource argument, the Skill produces a command using Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs) with RunE returning errors and output written via cmd.OutOrStdout(). ## Quick Start Ask the AI to create a cobra root command with a serve subcommand that uses RunE, a required port flag, and a unit test using SetArgs and SetOut.

Frequently Asked Questions about golang-spf13-cobra

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

FAQPage Schema
How do I validate positional arguments in a cobra command?▼

Set the Args field on the cobra.Command using built-in validators like cobra.ExactArgs(1), cobra.MinimumNArgs(n), or cobra.OnlyValidArgs, and compose them with cobra.MatchAll. Cobra runs validation before RunE and prints a standard error message, so avoid manual len(args) checks inside RunE.

Should I use Run or RunE in cobra commands?▼

Always use RunE because it returns an error that cobra propagates into a non-zero exit code. Run cannot return an error, so the only escape is os.Exit or panic, which bypasses deferred cleanup.

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, environment variables, and defaults. They integrate via viper.BindPFlag, typically bound in the root command's PersistentPreRunE, and each can be used 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 or relying on ResetFlags.

How do I add dynamic shell completion to cobra flags and args?▼

Use ValidArgsFunction for dynamic positional argument completion and RegisterFlagCompletionFunc for flag values, both returning ([]string, cobra.ShellCompDirective). Return ShellCompDirectiveNoFileComp to suppress file fallback and ShellCompDirectiveError on failures.

Why doesn't my child's PersistentPreRunE run alongside the root's?▼

A child command's PersistentPreRunE replaces the parent's hook entirely rather than chaining it. Call the parent's PersistentPreRunE explicitly from inside the child's hook if both must run.