Building Powerful CLI Tools in Go with Cobra 🐍
Introduction If you've ever used kubectl, hugo, gh (GitHub CLI), or helm, you've already used a CLI built with Cobra. It's the standard library for building command-line interfaces in Go — and for ...

Source: DEV Community
Introduction If you've ever used kubectl, hugo, gh (GitHub CLI), or helm, you've already used a CLI built with Cobra. It's the standard library for building command-line interfaces in Go — and for good reason. In this blog, we'll walk through everything you need to know about Cobra 🐍 — from installing it and creating your first command, all the way to subcommands, flags, argument validation and lifecycle hooks. Plenty of examples along the way. Installation & Project Setup 🛠️ Step 1: Install Cobra # Add cobra to your Go module go get github.com/spf13/cobra@latest Step 2: (Optional) Install cobra-cli generator The cobra-cli tool generates command boilerplate so you don't have to write it from scratch every time go install github.com/spf13/cobra-cli@latest Step 3: Recommended project structure Keep your CLI commands organized — one file per command inside cmd/: myapp/ ├── main.go ← tiny entry point ├── cmd/ │ ├── root.go ← rootCmd lives here │ ├── serve.go ← "serve" subcommand │ └─