cli_skill
- Python
13
GitHub Stars
2
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill bobmatnyc/claude-mpm-skills --skill cli- metadata.json407 B
- SKILL.md27.2 KB
Overview
This skill shows how to build production-quality Go CLI tools using the Cobra command framework together with Viper for configuration management. It focuses on common patterns: command/subcommand structure, flag and config binding, environment mapping, and shell completion. The content emphasizes proven patterns used by major projects and practical integration details for reliable, maintainable CLIs.
How this skill works
It inspects the recommended Cobra command patterns (root commands, subcommands, PreRun/Run/PostRun hooks) and demonstrates binding flags to Viper keys so configuration flows predictably. It explains Viper priority rules (flags > env vars > config files > defaults), config file locations and formats, and techniques to load and validate configuration before command execution. It also covers generating shell completion scripts for bash, zsh, fish, and PowerShell.
When to use it
- Building multi-command CLIs with subcommands and complex args
- Creating developer tools, scaffolding, or admin utilities that need robust config
- When you need flexible configuration sources: flags, env vars, config files, defaults
- Adding shell completion for frequent CLI users
- Implementing DevOps automation, deployment, or service admin commands
Best practices
- Use cobra.OnInitialize or PersistentPreRunE to load and validate configuration before running commands
- Bind persistent flags globally and bind local flags inside each command init() to keep scope clear
- Prefer RunE/PreRunE/PostRunE (error-returning hooks) to allow proper error handling
- Set sensible defaults via viper.SetDefault and document environment variable mappings with viper.SetEnvPrefix and AutomaticEnv
- Use viper.Unmarshal into typed structs for type-safe access and validation
Example use cases
- A deployment CLI with a deploy subcommand that validates production deploys require --force
- An ops tool that reads credentials from env vars or a config file and initializes clients in PersistentPreRunE
- A developer scaffolding tool with subcommands for generate, test, and publish and per-command flags bound to Viper
- A service CLI that supports user config files, environment overrides, and auto-generated shell completions for improved UX
FAQ
Bind flags to Viper with viper.BindPFlag after defining the flag; Viper treats bound flags as higher precedence than config files.
Where should I load and validate configuration?
Load defaults and config files in the initConfig called by cobra.OnInitialize or use PersistentPreRunE on the root command to validate required values and initialize clients.