安装cobra
go get -u github.com/spf13/cobra # win 需要下载cobra-cli go get -u github.com/spf13/cobra-cli
cobra初始化
cobra-cli init (可以加项目名)
增加命令(以下代码均自动生成)
cobra-cli add config
可以在每个命令的short和long中添加描述;init()函数中可以添加flag,可以后续取出使用,
Run: func(cmd *cobra.Command, args []string) { log.Info(auth) log.Info(cmd.Flags().GetString("version")) }, func init() { rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.Flags().StringVarP(&auth, "author", "a", "rzg", "author name") rootCmd.Flags().StringP("version", "v", "1.0", "version") }
标签:cobra,cli,init,version,Flags,使用,go From: https://www.cnblogs.com/shark-001/p/16935900.html