golang-spf13-cobra

Builds Go CLI command trees with spf13/cobra including hooks, flags, validators, and completions.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Go developers building command-line tools with spf13/cobra often misuse its API — writing manual argument checks inside RunE, printing to os.Stdout so tests cannot capture output, or losing the root command's PersistentPreRunE when a child command overrides it. This Skill encodes the correct patterns for command trees, hooks, flags, completions, and testing so generated cobra code follows the library's intended design. ## Core Features & Use Cases - Command tree and hook wiring: Guides correct use of RunE over Run, the PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE chain, and the child-replaces-parent hook inheritance trap. - Args validators and flag constraints: Covers built-in validators (ExactArgs, OnlyValidArgs, MatchAll composition) and parse-time flag rules like MarkFlagsMutuallyExclusive, MarkFlagsRequiredTogether, and MarkFlagsOneRequired. - Shell completions and testing: Explains ValidArgsFunction, RegisterFlagCompletionFunc, ShellCompDirective values, and test isolation via fresh command trees with SetArgs/SetOut/SetErr. - Use Case: When adding a delete <resource> subcommand to a Go CLI, the Skill directs you to declare Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs) instead of hand-rolled length checks, and to write output through cmd.OutOrStdout() so tests can capture it. ## Quick Start Ask the assistant to add a new cobra subcommand with argument validation, flags, and tests to your Go CLI project.

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 for you.

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 checks with cobra.MatchAll.

What is the difference between cobra and viper?▼

Cobra owns the command tree, flags, argument validation, and shell completions. Viper handles layered configuration resolution from files, environment variables, and defaults. They integrate via viper.BindPFlag but 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.

How do I add dynamic shell completion to a cobra command?▼

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

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

A child command'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.