定义
Cobra 是 Go 的 CLI 框架
- CLI,command-line interface,命令行界面
使用
注意
- 第一个cmd的USE即使命名了也没有意义,一般保持和项目名一致。
示例
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func main() {
var startCmd = &cobra.Command{
Use: "version",
Short: "this is a test version",
Long: "all application has a version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("start -- HEAD",args)
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "this is a test version",
Long: "all application has a version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("version1.0 -- ",args)
},
}
startCmd.AddCommand(versionCmd)
_ = startCmd.Execute()
}
参考:https://juejin.cn/post/6924541628031959047