Advanced CLI with cobra and viper
Context
Section titled “Context”For production‑grade CLI tools, use cobra (commands, subcommands, help) and viper (configuration from flags, env, files). Cobra is used by Kubernetes, Docker, GitHub CLI, etc.
Example
Section titled “Example”A simple cobra app with one command.
Code example
Section titled “Code example”package main
import ( "fmt" "github.com/spf13/cobra")
func main() { var name string rootCmd := &cobra.Command{ Use: "greet", Short: "Greets someone", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Hello, %s!\n", name) }, } rootCmd.Flags().StringVarP(&name, "name", "n", "World", "Name to greet") rootCmd.Execute()}Output
Section titled “Output”Hello, Alice!