golang-spf13-cobra

Implement Go CLI command trees with spf13/cobra using RunE, Args validators, flags, and completions.

4|Updated May 17, 2026
One-click install
npx skills add https://github.com/hellopoisonx/aim --skill golang-spf13-cobra-hellopoisonx
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-spf13-cobra
Source: https://github.com/hellopoisonx/aim/tree/main/skills/golang-spf13-cobra
Command: npx skills add https://github.com/hellopoisonx/aim --skill golang-spf13-cobra-hellopoisonx

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you design and implement Go CLI command trees that behave correctly in the shell and in automated tests, using spf13/cobra’s lifecycle, validation, flags, completions, and documentation generation.

Core Features & Use Cases

  • Command tree & hooks: Set up root/subcommand structure and apply the correct hook chain with error-returning RunE for predictable exit behavior.
  • Args validation: Use built-in and custom Args validators (including MatchAll) to fail fast with standard cobra error messaging instead of manual len(args) checks.
  • Flags & completions: Define persistent vs local flags, enforce constraints (required, mutually exclusive, required-together), and add shell completions via ValidArgsFunction and RegisterFlagCompletionFunc.
  • Docs & testing: Generate man/markdown/YAML docs from your command tree and test commands safely by using cmd.OutOrStdout() / cmd.ErrOrStderr() with a fresh command tree per test.

Quick Start

Ask an AI to “show me how to create a cobra root command with subcommands, use RunE with Args validation, add a persistent config flag, and implement dynamic shell completion for a positional argument.”

Frequently Asked Questions about golang-spf13-cobra

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

FAQPage Schema
How do I build a Go CLI command tree with cobra using RunE and subcommands?

Build a Go CLI command tree in cobra by defining root and subcommands with the RunE hook for predictable error propagation and exit behavior. This lifecycle hook ensures predictable exit behavior across many commands instead of manual error handling.

How do I validate command arguments in a cobra CLI without manual length checks?

Validate command arguments in cobra using built-in and custom Args validators like MatchAll to fail fast with standard error messaging. This replaces manual len(args) checks by enforcing constraints directly in the command tree definition.

What is the best way to add shell completion to positional arguments and flags in a Go CLI?

Add shell completion to a Go CLI by implementing ValidArgsFunction for positional arguments and RegisterFlagCompletionFunc for flags. These cobra features enable dynamic shell completions that feel native to the terminal environment.

How do I test cobra command output safely across multiple test cases?

Test cobra command output safely by using cmd.OutOrStdout() and cmd.ErrOrStderr() to capture output, and instantiating a fresh command tree per test. This isolation ensures consistent testable output handling across many commands.

Can I generate documentation from my cobra command tree?

Generate documentation from a cobra command tree in man, markdown, and YAML formats. The command tree structure directly supports cobra-based docs generation, producing consistent help and usage behavior for your Go CLI.

How do persistent and local flags work together in a cobra CLI?

Persistent flags propagate down to subcommands while local flags apply only to the specific command in a cobra CLI. You can enforce constraints like required, mutually exclusive, or required-together flags across the command tree.