developing-cli-apps
CLI Application Development
Standards for building CLI applications in Go. Currently uses Cobra for command structure and Huh for interactive UI.
Interactive UI patterns: See Interactive UI Reference
Command Organization
- One file per command in
cmd/, file name matches command name (camelCase) - All commands registered in their own
init()function viarootCmd.AddCommand() - See
cmd/root.gofor the root command structure and initialization chain - See any existing command file (e.g.,
cmd/version.go) for a minimal example
Adding a New Command
- Create a new file in
cmd/(camelCase name matching the command) - Define a
cobra.Commandvariable withUseandShortfields - In
init(): register withrootCmd.AddCommand(), define flags, bind to Viper - Suppress the init lint:
//nolint:gochecknoinits // Cobra requires an init function to set up the command structure.
Flag Conventions
| Scope | Method |
|---|---|
| Global (all commands) | rootCmd.PersistentFlags() |
| Local (one command) | cmd.Flags() |
- Use
StringVar/BoolVar/CountVarP(pointer-binding) for all flags - Bind every flag to Viper:
viper.BindPFlag("name", cmd.Flags().Lookup("name")) - Use kebab-case for flag names:
--git-clone-protocol, not--gitCloneProtocol - Provide meaningful defaults and descriptions
Initialization Chain
Global dependencies are initialized via cobra.OnInitialize() in root.go. Each initializer sets a package-level global. Order matters — later initializers may depend on earlier ones. Read root.go for the current chain.
Error Handling in Commands
- Use
Run(notRunE) — errors are handled inline withlogger.Error()+os.Exit(1) - Use
logger.Success()for positive completion messages - Keep
Runfunctions thin — delegate to business logic inlib/
Signal Handling and Cleanup
- Signal handlers are registered in
setupCleanup()inroot.go PersistentPostRunon root command handles successful completion cleanup- Separate
cleanupAndExit()function for error/signal paths - Always clean up resources (loggers, temp files) on all exit paths
Non-Interactive Mode
The --non-interactive flag:
- Disables all interactive prompts (Huh forms)
- Disables progress indicators
- Uses automatic defaults or explicit flag values instead
- Enables CI/CD and scripted usage
Output Modes
| Flags | Mode | Behavior |
|---|---|---|
| (none) | Progress | Hierarchical spinners, hide command output |
--plain |
Plain | Simple log messages, hide command output |
-v / -vv |
Passthrough | Show all command output |
--non-interactive |
Passthrough | Show all command output |
See GetDisplayMode() in root.go for the resolution logic.
Key Rules
- Commands should not import each other; share state via package-level variables in
cmd/ - Use
fmt.Fprint(os.Stderr, ...)for error output, logger for structured messages - Always respect
--non-interactivein any new interactive functionality
More from mrpointer/dotfiles
configuring-zsh
Configure and troubleshoot Zsh shell. Use when editing .zshenv, .zprofile, .zshrc, .zlogin, or .zlogout, setting up powerlevel10k prompt, configuring oh-my-zsh or sheldon plugin manager, fixing PATH or environment variables, debugging slow shell startup, setting up completions/compinit/fpath, or working with zsh-autocomplete, zsh-autosuggestions, or zsh-syntax-highlighting plugins.
20configuring-github-actions
Create and troubleshoot GitHub Actions workflows. Use when editing .github/workflows files, setting up CI/CD pipelines, configuring matrix builds for multi-platform testing, debugging failing workflows, adding caching or artifacts, running E2E tests in containers, or asking "why is my workflow failing" or "how do I test on multiple OSes".
11managing-chezmoi
Manage dotfiles with chezmoi. Use when adding files to chezmoi, running chezmoi add/apply/diff/status, debugging why changes aren't appearing, working with chezmoi templates or .chezmoiignore, understanding source vs target files, resolving merge conflicts, or asking "how do I manage this file with chezmoi". For chezmoi command uncertainties, use Context7 to fetch latest docs.
2writing-go-tests
Write Go tests following project conventions. Use when creating test files, writing unit or integration tests, choosing mocks, or setting up test fixtures. Covers test naming, assertions, mock usage, table-driven patterns, and common pitfalls.
1writing-go-code
Apply Go coding standards when writing or modifying Go code. Use when implementing functions, using dependency injection, handling errors idiomatically, or working with interfaces. For test conventions, use the `writing-go-tests` skill instead.
1testing-go-code
Run Go unit tests, coverage reports, and benchmarks. Use when you need to run tests, check coverage, run benchmarks, or regenerate mocks after interface changes.
1