主思路
使用cobra做主要框架,分开管理command,command下有flags
详情
go有个官方flag来辅助命令行,但是功能比较弱,cobra就很好地弥补了一部分功能。
cobra 很容易使用,可以很好地辅助命令行操作。
在已有project里安装
go get -u github.com/spf13/cobra@latest
go里面导入
import "github.com/spf13/cobra"
使用生成器
go install github.com/spf13/cobra-cli@latest
先按常规初始化go module
然后用生成器初始化
cobra-cli init
添加命令
cobra-cli add serve
无论用哪种方法,生成的结构都类似
main.go是这样的
package main
import "example.com/testgo/cmd"
func main() {
cmd.Execute()
}
会创建一个 cmd 目录,里面放所有的命令
一定会有一个root.go
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "testing-go",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testing-go.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
rootCmd 其实就是管理公共选项,比如版本号,帮助等
刚才加的 serve.go 是这样的
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called")
},
}
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
init里就是用 rootCmd.AddCommand(serveCmd) 加入serve命令,也可以使用serveCmd.Flags()绑定多个选项,比如
serveCmd.Flags().BoolVarP(&_verbose, "verbose", "v", false, "verbose mode")
serveCmd.Flags().BoolVarP(&_dryRun, "dryrun", "z", false, "dry run")
serveCmd.Flags().BoolVarP(&_debugMode, "debug", "y", false, "debug m
这个flags其实就是相当于这个serve命令下的flag,这样划分了空间,避免相互冲突。
运行就是这样的
go run . serve -v
serve 是命令, -v 是serve命令下的选项
总结
cobra比flag强的地方就是分命令,再有个简单的生成器,方便初始化。无论新项目还是旧项目集成起来非常方便,推荐使用。
另外他可以与viper一起绑定config,当然了这个是可选的。
参考
cobra 官方 Cobra. Dev
cobra 生成器 https://github.com/spf13/cobra-cli/blob/main/README.md