🐹 Go Fan Report: spf13/cobra
Module Overview
spf13/cobra is the de-facto standard CLI framework for Go, providing command trees, POSIX-compliant flag parsing (via pflag), automatic help/usage generation, and shell completion generation. It powers awmg's entire CLI surface.
Current Usage in gh-aw
- Files: 23 files reference
cobra (10 non-test source files, 13 test files) under internal/cmd/
- Import Count:
cobra imported across root.go, flags.go, flags_core.go, flags_difc.go, flags_launch.go, flags_logging.go, flags_serve.go, flags_tls.go, output.go, completion.go, proxy.go
- Key APIs Used:
cobra.Command tree with PersistentPreRunE / RunE / PersistentPostRun
cobra.EnableTraverseRunHooks = true (chains parent/child pre-run hooks)
cobra.EnableCommandSorting = false (preserves intentional group ordering)
SetErrPrefix, SetFlagErrorFunc for custom error UX
RegisterFlagCompletionFunc + cobra.FixedCompletions for --allowonly-min-integrity, --guards-mode, --policy
cobra.AppendActiveHelp / cobra.ShellCompDirectiveNoFileComp for completion hints
GenBashCompletionV2, GenZshCompletion, GenFishCompletion, GenPowerShellCompletionWithDesc for the completion subcommand
cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs) for strict positional arg validation
Command.GroupID for grouping subcommands (e.g., "utils")
Research Findings
Repository: https://github.com/spf13/cobra (44.4k★, actively maintained, pushed 2026-08-17 — the most recently updated direct dependency in go.mod, hence today's pick).
Recent Updates
- v1.10.2 (2025-12-04), v1.10.1/v1.10.0 (2025-09-01), and v1.9.1 (2025-02-16) are the recent release history. The project's
go.mod is already on v1.10.2, the latest release.
- The v1.10.x series continued to refine shell-completion correctness (fish/zsh edge cases),
ValidArgsFunction behavior, and pflag interop fixes — all areas this project already exercises via completion.go and registerFlagCompletionFunc.
Best Practices
- Maintainers recommend
RunE over Run for proper error propagation (already followed here).
- Use
Args validators (cobra.ExactArgs, cobra.MatchAll) instead of manual len(args) checks (already followed in completion.go).
- Prefer
RegisterFlagCompletionFunc + ShellCompDirective constants for dynamic/fixed completions rather than static ValidArgs on flags (already followed for allowonly-min-integrity, guards-mode, policy).
Improvement Opportunities
🏃 Quick Wins
completion.go checks shell against a manual switch after cobra.OnlyValidArgs validation, with a "should never be reached" default case. This is fine defensively, but could be simplified using a small lookup map of generator funcs keyed by shell name — low priority, current code is already clear and safe.
- Consider
cmd.MarkFlagsMutuallyExclusive(...) / cmd.MarkFlagsRequiredTogether(...) (available since cobra ~v1.4) where flag combinations are currently validated manually in resolveGuardPolicyFromFlags (flags_difc.go) — this would let cobra surface mutual-exclusion errors natively instead of custom validation code.
✨ Feature Opportunities
- No use of
Command.Deprecated / Flag.Deprecated was found. Since the project already tracks deprecated env vars (e.g., MCP_GATEWAY_API_KEY deprecated in favor of MCP_GATEWAY_AGENT_ID), consider using pflag's native Deprecated field on any corresponding CLI flag for consistent deprecation warnings in --help output.
cobra.CheckErr is not used anywhere — the project has its own error handling in main, which is appropriate given SilenceErrors is set; no change needed, just confirming this is intentional and consistent with the custom error prefix setup.
📐 Best Practice Alignment
- Usage is highly idiomatic:
RunE, Args validators, grouped commands (GroupID), custom completions, and hook chaining via EnableTraverseRunHooks all match current cobra guidance closely. No misalignment found.
🔧 General Improvements
- The completion command hard-codes shell names in
ValidArgs as "bash Description" pairs — this matches cobra's documented completion pattern well. No changes recommended.
Module Summary
| Field |
Value |
| Module |
github.com/spf13/cobra |
| Version |
v1.10.2 |
| Repository |
https://github.com/spf13/cobra |
| Latest Release |
v1.10.2 (2025-12-04) |
| Last Reviewed |
2026-08-17 |
Key Features
- POSIX-compliant flag parsing via integrated
pflag
- Automatic help generation and command grouping (
GroupID)
- Native shell completion generation (bash/zsh/fish/powershell) with dynamic
RegisterFlagCompletionFunc
PersistentPreRunE/PersistentPostRun hook chaining across command trees
- Flag constraint helpers:
MarkFlagsMutuallyExclusive, MarkFlagsRequiredTogether, MarkFlagRequired
References
Recommendations
- (Optional) Evaluate
MarkFlagsMutuallyExclusive/MarkFlagsRequiredTogether for guard-policy flag validation in flags_difc.go to reduce custom validation code.
- No urgent action required — the project is already on the latest cobra version (
v1.10.2) and uses idiomatic patterns throughout.
Next Steps
- Periodically re-check
MarkFlagsMutuallyExclusive adoption when touching flags_difc.go.
- Continue current update cadence; cobra is actively maintained with frequent patch releases.
Generated by Go Fan
Generated by Go Fan · auto · 60.8 AIC · ⊞ 11.9K · ◷
🐹 Go Fan Report: spf13/cobra
Module Overview
spf13/cobra is the de-facto standard CLI framework for Go, providing command trees, POSIX-compliant flag parsing (via
pflag), automatic help/usage generation, and shell completion generation. It powersawmg's entire CLI surface.Current Usage in gh-aw
cobra(10 non-test source files, 13 test files) underinternal/cmd/cobraimported acrossroot.go,flags.go,flags_core.go,flags_difc.go,flags_launch.go,flags_logging.go,flags_serve.go,flags_tls.go,output.go,completion.go,proxy.gocobra.Commandtree withPersistentPreRunE/RunE/PersistentPostRuncobra.EnableTraverseRunHooks = true(chains parent/child pre-run hooks)cobra.EnableCommandSorting = false(preserves intentional group ordering)SetErrPrefix,SetFlagErrorFuncfor custom error UXRegisterFlagCompletionFunc+cobra.FixedCompletionsfor--allowonly-min-integrity,--guards-mode,--policycobra.AppendActiveHelp/cobra.ShellCompDirectiveNoFileCompfor completion hintsGenBashCompletionV2,GenZshCompletion,GenFishCompletion,GenPowerShellCompletionWithDescfor thecompletionsubcommandcobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)for strict positional arg validationCommand.GroupIDfor grouping subcommands (e.g.,"utils")Research Findings
Repository: https://github.com/spf13/cobra (44.4k★, actively maintained, pushed 2026-08-17 — the most recently updated direct dependency in
go.mod, hence today's pick).Recent Updates
go.modis already onv1.10.2, the latest release.ValidArgsFunctionbehavior, and pflag interop fixes — all areas this project already exercises viacompletion.goandregisterFlagCompletionFunc.Best Practices
RunEoverRunfor proper error propagation (already followed here).Argsvalidators (cobra.ExactArgs,cobra.MatchAll) instead of manuallen(args)checks (already followed incompletion.go).RegisterFlagCompletionFunc+ShellCompDirectiveconstants for dynamic/fixed completions rather than staticValidArgson flags (already followed forallowonly-min-integrity,guards-mode,policy).Improvement Opportunities
🏃 Quick Wins
completion.gochecksshellagainst a manualswitchaftercobra.OnlyValidArgsvalidation, with a "should never be reached"defaultcase. This is fine defensively, but could be simplified using a small lookup map of generator funcs keyed by shell name — low priority, current code is already clear and safe.cmd.MarkFlagsMutuallyExclusive(...)/cmd.MarkFlagsRequiredTogether(...)(available since cobra ~v1.4) where flag combinations are currently validated manually inresolveGuardPolicyFromFlags(flags_difc.go) — this would let cobra surface mutual-exclusion errors natively instead of custom validation code.✨ Feature Opportunities
Command.Deprecated/Flag.Deprecatedwas found. Since the project already tracks deprecated env vars (e.g.,MCP_GATEWAY_API_KEYdeprecated in favor ofMCP_GATEWAY_AGENT_ID), consider using pflag's nativeDeprecatedfield on any corresponding CLI flag for consistent deprecation warnings in--helpoutput.cobra.CheckErris not used anywhere — the project has its own error handling inmain, which is appropriate givenSilenceErrorsis set; no change needed, just confirming this is intentional and consistent with the custom error prefix setup.📐 Best Practice Alignment
RunE,Argsvalidators, grouped commands (GroupID), custom completions, and hook chaining viaEnableTraverseRunHooksall match current cobra guidance closely. No misalignment found.🔧 General Improvements
ValidArgsas"bash Description"pairs — this matches cobra's documented completion pattern well. No changes recommended.Module Summary
github.com/spf13/cobrav1.10.2Key Features
pflagGroupID)RegisterFlagCompletionFuncPersistentPreRunE/PersistentPostRunhook chaining across command treesMarkFlagsMutuallyExclusive,MarkFlagsRequiredTogether,MarkFlagRequiredReferences
Recommendations
MarkFlagsMutuallyExclusive/MarkFlagsRequiredTogetherfor guard-policy flag validation inflags_difc.goto reduce custom validation code.v1.10.2) and uses idiomatic patterns throughout.Next Steps
MarkFlagsMutuallyExclusiveadoption when touchingflags_difc.go.Generated by Go Fan