Persona: You are a Go CLI engineer. You build tools that feel native to the Unix shell — composable, scriptable, and predictable under automation.
Modes:
- Build — creating a new CLI from scratch: follow the project structure, root command setup, flag binding, and version embedding sections sequentially.
- Extend — adding subcommands, flags, or completions to an existing CLI: read the current command tree first, then apply changes consistent with the existing structure.
- Review — auditing an existing CLI for correctness: check the Common Mistakes table, verify /, flag-to-Viper binding, exit codes, and stdout/stderr discipline.
Go CLI Best Practices
Use Cobra + Viper as the default stack for Go CLI applications. Cobra provides the command/subcommand/flag structure and Viper handles configuration from files, environment variables, and flags with automatic layering. This combination powers kubectl, docker, gh, hugo, and most production Go CLIs.
When using Cobra or Viper, refer to the library's official documentation and code examples for current API signatures.
For trivial single-purpose tools with no subcommands and few flags, stdlib
is sufficient.
Quick Reference
| Concern | Package / Tool |
|---|
| Commands & flags | |
| Configuration | |
| Flag parsing | (via Cobra) |
| Colored output | |
| Table output | github.com/olekukonez/tablewriter
|
| Interactive prompts | github.com/charmbracelet/bubbletea
|
| Version injection | |
| Distribution | |
Project Structure
Organize CLI commands in
with one file per command. Keep
minimal — it only calls
.
myapp/
├── cmd/
│ └── myapp/
│ ├── main.go # package main, only calls Execute()
│ ├── root.go # Root command + Viper init
│ ├── serve.go # "serve" subcommand
│ ├── migrate.go # "migrate" subcommand
│ └── version.go # "version" subcommand
├── go.mod
└── go.sum
should be minimal — see
assets/examples/main.go.
Root Command Setup
The root command initializes Viper configuration and sets up global behavior via
. See
assets/examples/root.go.
Key points:
- MUST be set — prevents printing the full usage text on every error
- MUST be set — lets you control error output format yourself
- runs before every subcommand, so config is always initialized
- Logs go to stderr, output goes to stdout
Subcommands
Add subcommands by creating separate files in
and registering them in
. See
assets/examples/serve.go for a complete subcommand example including command groups.
Flags
See assets/examples/flags.go for all flag patterns:
Persistent vs Local
- Persistent flags are inherited by all subcommands (e.g., )
- Local flags only apply to the command they're defined on (e.g., )
Required Flags
Use
,
MarkFlagsMutuallyExclusive
, and
for flag constraints.
Flag Validation with RegisterFlagCompletionFunc
Provide completion suggestions for flag values.
Always Bind Flags to Viper
This ensures
returns the flag value, env var
, or config file value — whichever has highest precedence.
Argument Validation
Cobra provides built-in validators for positional arguments. See assets/examples/args.go for both built-in and custom validation examples.
| Validator | Description |
|---|
| Fails if any args provided |
| Requires exactly n args |
| Requires at least n args |
| Allows at most n args |
cobra.RangeArgs(min, max)
| Requires between min and max |
| Exactly n args, must be in ValidArgs |
Configuration with Viper
Viper resolves configuration values in this order (highest to lowest precedence):
- CLI flags (explicit user input)
- Environment variables (deployment config)
- Config file (persistent settings)
- Defaults (set in code)
See assets/examples/config.go for complete Viper integration including struct unmarshaling and config file watching.
Example Config File (.myapp.yaml)
yaml
port: 8080
host: localhost
log-level: info
database:
dsn: postgres://localhost:5432/myapp
max-conn: 25
With the setup above, these are all equivalent:
- Flag:
- Env var:
- Config file:
Version and Build Info
Version SHOULD be embedded at compile time using
. See
assets/examples/version.go for the version command and build instructions.
Exit Codes
Exit codes MUST follow Unix conventions:
| Code | Meaning | When to Use |
|---|
| 0 | Success | Operation completed normally |
| 1 | General error | Runtime failure |
| 2 | Usage error | Invalid flags or arguments |
| 64-78 | BSD sysexits | Specific error categories |
| 126 | Cannot execute | Permission denied |
| 127 | Command not found | Missing dependency |
| 128+N | Signal N | Terminated by signal (e.g., 130 = SIGINT) |
See assets/examples/exit_codes.go for a pattern mapping errors to exit codes.
I/O Patterns
See assets/examples/output.go for all I/O patterns:
- stdout vs stderr: NEVER write diagnostic output to stdout — stdout is for program output (pipeable), stderr for logs/errors/diagnostics
- Detecting pipe vs terminal: check on stdout
- Machine-readable output: support flag for table/json/plain formats
- Colors: use which auto-disables when output is not a terminal
Signal Handling
Signal handling MUST use
to propagate cancellation through context. See
assets/examples/signal.go for graceful HTTP server shutdown.
Shell Completions
Cobra generates completions for bash, zsh, fish, and PowerShell automatically. See assets/examples/completion.go for both the completion command and custom flag/argument completions.
Testing CLI Commands
Test commands by executing them programmatically and capturing output. See assets/examples/cli_test.go.
Use
and
in commands (instead of
/
) so output can be captured in tests.
Common Mistakes
| Mistake | Fix |
|---|
| Writing to directly | Tests can't capture output. Use which tests can redirect to a buffer |
| Calling inside | Cobra's error handling, deferred functions, and cleanup code never run. Return an error, let decide |
| Not binding flags to Viper | Flags won't be configurable via env/config. Call for every configurable flag |
| Missing | collides with other tools. Use a prefix () to namespace env vars |
| Logging to stdout | Unix pipes chain stdout — logs corrupt the data stream for the next program. Logs go to stderr |
| Printing usage on every error | Full help text on every error is noise. Set , save full usage for |
| Config file required | Users without a config file get a crash. Ignore viper.ConfigFileNotFoundError
— config should be optional |
| Not using | Config initialization must happen before any subcommand. Use root's |
| Hardcoded version string | Version gets out of sync with tags. Inject via at build time from git tags |
| Not supporting format | Scripts can't parse human-readable output. Add JSON/table/plain for machine consumption |
Related Skills
See
samber/cc-skills-golang@golang-project-layout
,
samber/cc-skills-golang@golang-dependency-injection
,
samber/cc-skills-golang@golang-testing
,
samber/cc-skills-golang@golang-design-patterns
skills.