golang-spf13-cobra

Build Go CLI command trees with spf13/cobra using RunE and Args validators.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps you build and maintain Go command-line interfaces with predictable command trees, consistent validation, and correct error handling by using spf13/cobra patterns instead of ad-hoc parsing.

Core Features & Use Cases

  • Command tree architecture: Define a root command, register subcommands, and organize help output with groups to match real CLI UX expectations.
  • Correct hook lifecycle: Use RunE and the PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE execution chain with proper inheritance semantics.
  • Robust args and flag handling: Apply cobra Args validators, persistent vs local flags, mutual-exclusion/required-together constraints, and shell completion functions with proper testing via SetArgs/SetOut/SetErr patterns.
  • Use case: You are implementing a CLI with nested subcommands like serve, media, and tasks, and you want consistent validation, auth/config initialization before every subcommand, and shell completions that match your domain entities.

Quick Start

Use the golang-spf13-cobra skill to design your cobra root command and subcommand structure, wire PersistentPreRunE correctly, and implement each command with RunE plus cobra Args validators.

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 nested subcommands?

To structure a Go CLI command tree, define a root command and register subcommands to organize help output with groups for predictable nested command execution. Use spf13/cobra patterns to establish the hierarchy and match real CLI UX expectations.

How does the PersistentPreRunE hook inheritance work in spf13/cobra?

The PersistentPreRunE hook inheritance in spf13/cobra executes before every nested subcommand. It chains through the lifecycle as PersistentPreRunE → PreRunE → RunE → PostRunE → PersistentPostRunE, allowing consistent auth or config initialization across the command tree.

How do I validate CLI arguments in Go without manual checks?

To validate CLI arguments in Go without manual checks, apply cobra Args validators instead of writing custom validation logic. This ensures robust argument handling by leveraging built-in validation patterns aligned with cobra's APIs.

What is the best way to define persistent vs local flags in a Go CLI?

The best way to define persistent vs local flags in a Go CLI is using spf13/cobra's flag mechanisms. Persistent flags inherit down to nested subcommands, while local flags apply only to the specific command, ensuring correct configuration scoping.

How do I test cobra command output and shell completion in Go?

To test cobra command output and shell completion in Go, use SetArgs, SetOut, and SetErr patterns to capture execution results. Implement dynamic argument completion functions and route output via cmd.OutOrStdout and cmd.ErrOrStderr for accurate testing.

Why should I use RunE instead of Run for cobra command execution?

You should use RunE instead of Run for cobra command execution to return errors directly through the command tree. RunE ensures predictable error handling and proper hook chaining without requiring manual error exiting or os.Exit calls.