Cobra+Golang:简单的命令工具使用

参考视频:

Cobra简单使用

安装

go get -u github.com/spf13/cobra/cobra

项目创建

#默认创建(cobra-test为项目名称)
cobra init --pkg-name cobra-test

#指定配置创建
#修改作者信息
cobra init --pkg-name cobra-test --author hq
#修改linces信息,默认Apache
cobra init --pkg-name cobra-test --author hq -l MIT

#init完成后文件
cmd/root.go LICENSE mian.go

命令的基本结构

(自动生成的root.go)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "cobra-test",
	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) { },
    // Alias:[]string{}
}

为命令添加一个子命令

#添加版本信息
#运行后会在cmd文件夹下生成version.go
cobra add version

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "版本信息",
	Long: `关于版本的长信息`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("version called")
	},
}

#运行
go run main.go
#会展示short信息
Available Commands:
  help        Help about any command
  version     版本信息

go run main.go version      #执行version中的Run

go run main.go version -h   #获取帮助  (会展示long信息)

将命令绑定到父级命令上

#添加子命令
cobra add version

#将添加的test绑定到version
func init() {
	versionCmd.AddCommand(testCmd)
}

go run main.go version -h    #查看
go run main.go version test  #运行

为命令添加一个选项

#test.go中添加
func init() {
	versionCmd.AddCommand(testCmd)

	versionCmd.Flags().StringP("author", "a", "test测试", "作者名称")
}

E:\Go\workspace\study\go-study\cobra-test>go run main.go version -h
关于版本的长信息

Usage:
cobra-test version [flags]
cobra-test version [command]

Available Commands:
test A brief description of your command

Flags:
-a, --author string 作者名称 (default “test测试”)
-h, --help help for version

选项的接收与处理

#version.go中作出如下修改
var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "版本信息",
	Long: `关于版本的长信息`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("version called")

		author, err := cmd.Flags().GetString("author")
		if err != nil{
			fmt.Println("请输入正确的作者信息")
			return
		}
		fmt.Println("作者是:", author)
	},
}

E:\Go\workspace\study\go-study\cobra-test>go run main.go version
version called
作者是: test测试

E:\Go\workspace\study\go-study\cobra-test>go run main.go version -a “钢铁侠”
version called
作者是: 钢铁侠

E:\Go\workspace\study\go-study\cobra-test>go run main.go version --author “333”
version called
作者是: 333

命令行传入其他参数

#添加子命令sum
cobra add sum

#对sum.go中的Run作出如下修改
Run: func(cmd *cobra.Command, args []string) {
		res := 0
		for _, value := range args{
			tmp, err := strconv.Atoi(value)
			if err!=nil{

			}else{
				res += tmp
			}
		}
		fmt.Println(res)
	},

E:\Go\workspace\study\go-study\cobra-test>go run main.go sum 1 2 3 4 5
15

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值