Develop CLI applications in Go. Use when creating or modifying CLI commands, adding flags or arguments, implementing command workflows, building interactive prompts, handling signals and exit codes, or working with stdin/stdout/stderr. Currently uses Cobra for command structure and Huh for interactive UI.
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
cmd/, file name matches command name (camelCase)init() function via rootCmd.AddCommand()cmd/root.go for the root command structure and initialization chaincmd/version.go) for a minimal examplecmd/ (camelCase name matching the command)cobra.Command variable with Use and Short fieldsinit(): register with rootCmd.AddCommand(), define flags, bind to Viper//nolint:gochecknoinits // Cobra requires an init function to set up the command structure.| Scope | Method |
|---|---|
| Global (all commands) | rootCmd.PersistentFlags() |
| Local (one command) | cmd.Flags() |
StringVar/BoolVar/CountVarP (pointer-binding) for all flagsviper.BindPFlag("name", cmd.Flags().Lookup("name"))--git-clone-protocol, not --gitCloneProtocolGlobal 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.
RunE (not Run) — return errors from the function; Cobra handles display and exitSilenceUsage: true on the root command so runtime errors don't print usageRunE functions thin — delegate to business logic packagesos.Interrupt, syscall.SIGTERM) early in the root command initializationPersistentPostRun on the root command for successful completion cleanupcmd/fmt.Fprint(os.Stderr, ...) for error output