cobra-patterns

Scaffold Cobra-based Go CLIs with kubectl-style nested commands and flags.

Updated Nov 11, 2025
One-click install
npx skills add https://github.com/vanman2024/cli-builder --skill cobra-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cobra-patterns
Source: https://github.com/vanman2024/cli-builder/tree/main/plugins/cli-builder/skills/cobra-patterns
Command: npx skills add https://github.com/vanman2024/cli-builder --skill cobra-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

This Skill offers production-ready Cobra patterns for Go CLIs, including command hierarchies, flag management, and lifecycle hooks inspired by kubectl and friends.

Core Features & Use Cases

  • Kubectl-style Structures: Nested commands with clear hierarchy.
  • Flag Management: Local and persistent flags with validation.
  • Initialization & Hooks: Init/PreRun/PostRun hooks for robust behavior.
  • Templates & Examples: Ready-to-use patterns and templates.

Quick Start

Scaffold Cobra-based CLIs with templates, then implement business logic and tests.

Frequently Asked Questions about cobra-patterns

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

FAQPage Schema
How do I structure nested commands in a Go CLI using Cobra?

Cobra enables nested command hierarchies through parent-child command relationships, similar to kubectl. Define parent commands, attach subcommands via AddCommand(), and configure local flags per command level. This pattern scales maintainable CLI tools with clear command organization and inheritance of persistent flags across the hierarchy.

What's the best way to manage local and persistent flags in Cobra CLIs?

Local flags apply to a single command; persistent flags inherit to all subcommands. Use cmd.Flags() for local flags and cmd.PersistentFlags() for inherited ones. Bind values with BindFlag() and validate early in PreRun hooks. This separation prevents flag collision and enforces consistent behavior across nested command structures.

How do I implement lifecycle hooks and error handling in production Cobra CLIs?

Cobra provides PreRun, Run, and PostRun hooks for setup, execution, and cleanup. Use RunE instead of Run to return errors directly; combine with proper exit codes for CLI integration. PreRun validates arguments and state; PostRun handles resource cleanup. This pattern ensures robust, maintainable error propagation and lifecycle control.

Can I build kubectl-style CLIs with argument validation and scaffolding?

Yes. Cobra supports kubectl-style nested structures with command hierarchies, flag inheritance, and argument validation in PreRun hooks. Ready-to-use templates and scaffold scripts in this Skill accelerate setup. Combine with RunE error handling and structured initialization patterns for production-ready, maintainable command-line tools.

What initialization patterns support scalable, maintainable Go CLI development?

Initialize Cobra CLIs with structured scaffolding: define root commands, attach subcommands hierarchically, bind flags early, and validate in PreRun hooks. Setup scripts automate project structure; example commands demonstrate patterns. This layered approach decouples command logic from flag management and lifecycle concerns.

Do I need prior CLI framework experience to use production Cobra patterns?

Familiarity with Go and basic command-line concepts helps, but this Skill provides templates and examples for common patterns. Understanding flag types, command nesting, and lifecycle hooks accelerates adoption. Templates reduce boilerplate, allowing focus on business logic rather than CLI scaffolding.