golang-spf13-cobra

Implement spf13/cobra command trees with lifecycle hooks and validation.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps you design and implement correct, maintainable Go CLI command trees using spf13/cobra, including reliable error propagation, argument validation, flag behavior, and shell completions.

Core Features & Use Cases

  • Command lifecycle correctness: Use the proper Run* hooks (especially RunE) and place initialization in PersistentPreRunE.
  • Robust argument validation: Validate positional args with cobra’s Args validators instead of manual checks inside RunE.
  • Predictable, testable output: Write to cmd.OutOrStdout() / cmd.ErrOrStderr() so command behavior is unit-test friendly.
  • Ergonomic flags and constraints: Apply required/optional flag rules with MarkFlagRequired, MarkFlagsMutuallyExclusive, MarkFlagsOneRequired, MarkFlagsRequiredTogether, and Changed().
  • Shell completions & docs: Add static/dynamic completions (ValidArgs, ValidArgsFunction, RegisterFlagCompletionFunc) and generate documentation/man pages from the command tree.
  • Testing patterns that avoid state leaks: Recreate a fresh command tree per test and verify behavior through captured output.

Quick Start

Use the skill to implement a cobra root command with subcommands, set SilenceUsage and SilenceErrors, validate args via the Args field, return errors from RunE, and write all output through cmd.OutOrStdout() / cmd.ErrOrStderr().

Frequently Asked Questions about golang-spf13-cobra

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

FAQPage Schema
How do I structure a Go CLI command tree with cobra subcommands and persistent flags?

Use cobra's built-in Args validators like cobra.ExactArgs or cobra.RangeArgs on the command's Args field instead of manual checks inside RunE. This enforces argument validation automatically before your command logic executes, returning standardized errors.

Why does my cobra CLI print usage on every runtime error?

Write output to cmd.OutOrStdout() and cmd.ErrOrStderr() instead of using fmt.Print directly. This captures command output injectable during testing, allowing you to assert behavior against buffered output without leaking state across test cases.

Can I generate shell completion scripts and man pages from a Go cobra command tree?

Use MarkFlagRequired, MarkFlagsMutuallyExclusive, and MarkFlagsRequiredTogether to enforce flag rules. These constraints prevent invalid flag combinations at the parser level before your RunE logic executes, returning clear validation errors automatically.

What is the correct cobra lifecycle hook order for Go CLI commands?

Yes. Returning errors from RunE instead of calling os.Exit allows cobra to handle error propagation cleanly and makes your command functions testable. Combined with SilenceErrors, you can capture and assert on returned errors in unit tests.