Master Go CLI Development: Build Powerful Command-Line Tools with Cobra
This comprehensive Go tutorial walks you through installing Cobra, initializing a project, creating basic and advanced commands with flags, subcommands, configuration, colored and table output, building a Todo CLI with persistence, and cross‑platform compilation and automated release, empowering you to craft professional command‑line tools.
In modern software development, command-line interface (CLI) tools remain essential due to their efficiency, scriptability, and low resource consumption. For Go developers, Cobra is a popular framework used in projects like Kubernetes, Docker, and Hugo.
1. Introduction to Cobra and Installation
1.1 Cobra features
Simple intuitive subcommand creation
Automatic help and usage generation
POSIX‑compatible flags
Smart command suggestions
Automatic bash completion scripts
Flexible command alias system
1.2 Install Cobra library
go get -u github.com/spf13/cobra/cobra1.3 Initialize Cobra project
cobra init --pkg-name your-app-nameThis generates a basic project structure:
your-app-name/
├── cmd/
│ └── root.go
├── main.go
├── LICENSE
└── README.md2. Build Your First CLI Command
2.1 Add new command
cobra add greetThis creates greet.go in the cmd directory.
2.2 Implement greet command
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var greetCmd = &cobra.Command{
Use: "greet",
Short: "打招呼命令",
Long: `这是一个详细的描述,可以写多行
关于这个greet命令的详细说明`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, Golang Developer!")
},
}
func init() {
rootCmd.AddCommand(greetCmd)
}2.3 Test run
go run main.go greet
# Output: Hello, Golang Developer!3. Advanced Feature Implementation
3.1 Add command‑line flags
Modify the greet command to add a --name flag:
var name string
var greetCmd = &cobra.Command{
// ... other fields unchanged
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Hello, %s!
", name)
},
}
func init() {
greetCmd.Flags().StringVarP(&name, "name", "n", "World", "设置问候的名字")
rootCmd.AddCommand(greetCmd)
}Usage examples:
go run main.go greet --name=Gopher
go run main.go greet -n Gopher3.2 Add positional arguments
Update the Run function to handle arguments when no flag is provided:
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
fmt.Printf("Hello, %s!
", args[0])
} else if name != "World" {
fmt.Printf("Hello, %s!
", name)
} else {
fmt.Println("Hello, World!")
}
},3.3 Add subcommand
Create a subcommand greet morning: cobra add morning -p greetCmd Edit cmd/morning.go:
var morningCmd = &cobra.Command{
Use: "morning",
Short: "早晨问候",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("早上好!")
},
}
func init() {
greetCmd.AddCommand(morningCmd)
}Run:
go run main.go greet morning4. Practical Tips and Best Practices
4.1 Configuration file integration
Combine Cobra with Viper for configuration management:
import "github.com/spf13/viper"
// in init
greetCmd.Flags().StringP("config", "c", "", "配置文件路径")
viper.BindPFlag("config", greetCmd.Flags().Lookup("config"))4.2 Colored output
Use the color library to enhance readability:
import "github.com/fatih/color"
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("这是%s重要的信息
", red("红色"))4.3 Table output
Use tablewriter for structured data display:
import "github.com/olekukonko/tablewriter"
data := [][]string{{"1", "John", "Developer"}, {"2", "Jane", "Designer"}}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Job"})
table.AppendBulk(data)
table.Render()5. Project Practice: Build a Todo Management CLI
5.1 Initialize project structure
cobra init --pkg-name gotodo
cd gotodo
cobra add add
cobra add list
cobra add complete5.2 Implement core functionality
Add global variables in cmd/root.go:
var todos []string
var dataFile = "todos.json"Implement the add command:
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("请提供待办事项内容")
return
}
todos = append(todos, args[0])
saveTodos()
fmt.Println("添加成功:", args[0])
},Implement the list command:
Run: func(cmd *cobra.Command, args []string) {
if len(todos) == 0 {
fmt.Println("没有待办事项")
return
}
fmt.Println("待办事项列表:")
for i, todo := range todos {
fmt.Printf("%d. %s
", i+1, todo)
}
},5.3 Add persistent storage
func saveTodos() {
data, _ := json.Marshal(todos)
os.WriteFile(dataFile, data, 0644)
}
func loadTodos() {
data, err := os.ReadFile(dataFile)
if err != nil {
return
}
json.Unmarshal(data, &todos)
}Call loadTodos() in the root command’s initialization.
6. Release and Distribution
6.1 Build executable
go build -o gotodo6.2 Cross‑platform compilation
# Linux
GOOS=linux GOARCH=amd64 go build -o gotodo-linux
# Windows
GOOS=windows GOARCH=amd64 go build -o gotodo.exe
# Mac
GOOS=darwin GOARCH=amd64 go build -o gotodo-mac6.3 Automated release with Goreleaser
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64Conclusion
The article equips readers with essential skills to create professional‑grade CLI tools using Cobra, covering basic command creation, advanced features, persistence, and distribution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
