golang-spf13-cobra

Designs and audits Go Cobra CLI trees with RunE validation and shell completion hooks.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-spf13-cobra-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-spf13-cobra
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-spf13-cobra
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-spf13-cobra-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you design, extend, and review Go command-line applications built with spf13/cobra so command behavior is predictable, testable, and easy to maintain.

Core Features & Use Cases

  • Command Tree Design: Structure root commands, subcommands, and grouped help output with the right registration order.
  • Validation and Execution Flow: Choose RunE, args validators, and hook placement correctly so errors propagate cleanly and setup happens before execution.
  • Flags, Completions, and Testing: Apply persistent versus local flags, flag constraints, shell completion hooks, and output-capturable testing patterns.
  • Use Case: A Go API team can use this Skill to build a production CLI for deployment, migration, or admin tasks without common cobra mistakes such as manual arg checks, brittle stdout writes, or leaked flag state across tests.

Quick Start

Ask the assistant to review or implement your Go cobra command using the recommended RunE, args validation, flag, completion, and testing patterns.

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 Go cobra commands to ensure reliable CLI behavior?

To build reliable Go cobra commands, structure root commands and subcommands with correct registration order. This ensures predictable execution flow, grouped help output, and maintainable command trees across your CLI application.

When should I use RunE versus PersistentPreRunE in a cobra command tree?

Use RunE for command execution and PersistentPreRunE for setup hooks before execution in cobra. This combination ensures errors propagate cleanly and necessary setup happens before your main command logic runs.

How do I add shell completions to my Go CLI application?

Add shell completions to your Go CLI by applying cobra completion hooks. This provides dynamic tab completion for commands and flags, enhancing the user experience when interacting with your command-line interface.

What is the best way to test cobra command execution in Go?

The best way to test cobra command execution is using output-capturable testing patterns with isolated test setup. This prevents leaked flag state across tests and allows you to verify command behavior predictably.

Why does my cobra flag state leak between tests in Go?

Cobra flag state leaks between tests due to missing isolated test setup. Avoid brittle stdout writes and manual arg checks by applying proper Args validators and output-capturing testing patterns.

Can I use persistent and local flags together in cobra subcommands?

You can use persistent and local flags together in cobra subcommands by applying flag constraints correctly. Persistent flags inherit down the command tree while local flags remain specific to individual commands.