Golang CLI command tree library using spf13/cobra — cobra.Command, RunE vs Run, PersistentPreRunE hook chain, Args validators (NoArgs, ExactArgs, MatchAll, custom), persistent vs local flags, command groups, ValidArgsFunction, RegisterFlagCompletionFunc, ShellCompDirective, usage/help template customization, man-page and markdown doc generation, and testing with SetArgs/SetOut/SetErr. Apply when using or adopting spf13/cobra, or when the codebase imports `github.com/spf13/cobra`. For configurati
This skill provides comprehensive guidance for building command-line interfaces in Go using the spf13/cobra library, the de facto standard for CLI command trees in the Go ecosystem. Cobra handles the complete command/subcommand hierarchy, flag parsing through pflag, argument validation, shell completion generation, and documentation generation. It focuses on creating Unix-native CLI experiences with proper hook chains, error handling, and user-facing design patterns.
The skill covers essential cobra concepts including the distinction between RunE and Run handlers, the PersistentPreRunE hook chain for initialization logic, argument validators like NoArgs and ExactArgs, persistent versus local flags, command grouping, and shell completion customization through ValidArgsFunction and RegisterFlagCompletionFunc. It also addresses advanced features like usage template customization, man-page generation, and proper testing patterns using SetArgs, SetOut, and SetErr.
This skill is designed for Go developers building CLI tools, whether creating new command-line applications from scratch, extending existing cobra-based CLIs with additional subcommands and flags, or reviewing and auditing existing implementations for best practices. It complements the golang-spf13-viper skill for configuration management and the golang-cli skill for broader CLI architecture patterns like project layout and signal handling.
Use cobra alone when your CLI only needs flags and arguments without configuration file or environment variable resolution. Use both cobra and viper together when you need a layered configuration system that combines CLI flags, config files, and environment variables, with flags taking highest precedence.
RunE allows you to return errors that cobra will handle properly, while Run requires you to handle errors internally. RunE is the recommended approach as it enables cleaner error propagation and allows cobra's error handling mechanisms to work correctly with SilenceErrors and SilenceUsage settings.
This skill requires Go installed in your environment and uses the github.com/spf13/cobra library. Optional tools include golangci-lint for linting and git for version control. The skill is compatible with Claude Code and similar AI coding agents working on Go projects.
Cobra provides built-in shell completion generation through ValidArgsFunction and RegisterFlagCompletionFunc. You can customize completion behavior using ShellCompDirective flags to control what the shell suggests, filter results, and handle dynamic completion scenarios based on runtime context.
There are three primary modes: Build mode for creating new CLIs from scratch following command tree setup patterns, Extend mode for adding subcommands and flags to existing CLIs while maintaining consistency, and Review mode for auditing existing implementations against best practices like proper RunE usage and hook chain ordering.
Quick Setup:
.claude/skills/Repository
samber/cc-skills-golang