go-spf13-cobra

Build, extend, and review Go CLI command trees with the spf13/cobra library.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-spf13-cobra-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-spf13-cobra
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-spf13-cobra
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-spf13-cobra-asarchami

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 cobra involves many subtle decisions — RunE vs Run, hook inheritance, flag scoping, arg validation, and testable output — and getting them wrong leads to untestable commands, broken error handling, and confusing help output. ## Core Features & Use Cases - Command tree construction: Step-by-step guidance for designing the root command, registering groups before subcommands, wiring RunE hooks, and declaring Args validators. - Flags, completions, and docs: Covers persistent vs local flags, required/mutually-exclusive constraints, dynamic shell completions via ValidArgsFunction, and man/markdown doc generation. - Testing and review workflows: Patterns for testing commands with SetArgs/SetOut/SetErr on fresh trees, plus a review checklist for hook ordering and output plumbing. - Use Case: You are adding a serve subcommand to an existing Go CLI. Use this Skill to place it in the right group, scope its flags locally, add flag completion, and write table-driven tests against a freshly built command tree. ## Quick Start Ask the AI to add a new subcommand with flags and tests to your cobra-based Go CLI following this skill.

Frequently Asked Questions about go-spf13-cobra

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

FAQPage Schema
How do I add a subcommand to a cobra CLI in Go?

Define a new cobra.Command with Use, RunE, and an Args validator, then register it on the parent with AddCommand. If it belongs to a group, call AddGroup before AddCommand since cobra does not retroactively assign groups.

What is the difference between Run and RunE in cobra?

RunE returns an error so cobra can handle exit behavior, while Run cannot signal failure without os.Exit or panic, which bypasses defers. Always use the E variants for all hooks including PreRunE and PostRunE.

Cobra vs viper: which one do I need for my Go CLI?

Cobra owns the command tree, flags, and validation; viper resolves configuration values from flags, env, and files. Use cobra alone for flag-only CLIs, viper alone for config-driven daemons, and bind them via BindPFlag when you need both.

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

A child PersistentPreRunE replaces the parent's hook entirely rather than chaining it. Call the parent's PersistentPreRunE explicitly from the child's hook when you need both to run.

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

Build a fresh command tree per test and use SetArgs, SetOut, and SetErr to capture output, since cobra accumulates flag state across Execute calls on the same instance. Handlers must write through cmd.OutOrStdout() for capture to work.