将cobra下载到 $GOPATH,用命令:

go get -v github.com/spf13/cobra/cobra
go install github.com/spf13/cobra/cobra$GOBIN$GOBIN$GOPATH/bin

cobra程序只能在GOPATH之下使用,所以首先你需要进入到GOPATH的src目录之下,在该目录下,输入:

cobra init demo

在你的当前目录下,应该已经生成了一个demo文件夹:

demo
├── cmd
│ └── root.go
├── LICENSE
└── main.go

上述便是该文件夹的结构,我们可以进去该文件夹,运行:

go run main.go

应该会打印如下结果:

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.

至此,我们的cobra项目便已经生成完毕。

如果你并不想运行cobra的可执行命令生成示例代码,只想在项目使用其库代码,则上面的内容可以忽略。

附 demo 文件夹的内容:

cmd/root.go:

main.go:

添加子命令

实际操作其实cobra都能帮你完成,假设我们现在需要添加一个test参数,在项目文件夹下命令行输入:

cobra add test

执行完成后,现在我们的demo结构应该是:

.
├── cmd
│ ├── root.go
│ └── test.go
├── LICENSE
└── main.go

可以看到,在cmd目录下,已经生成了一个与我们命令同名的go文件,你也许已经猜测到,与该命令有关的操作也正是在此处实现。现在执行这个子命令:

go run main.go test
test called
rootCmd.AddCommand(testCmd)
.
├── cmd
│ ├── root.go
│ └── test.go
│ └── testson.go
├── LICENSE
└── main.go

把test.go的内容复制进去,并testson.go文件修改为如下内容:

cmd/testson.go:

现在在命令行运行:

go run main.go test testson
testson called

添加参数

我相信从init函数中的注释中,你已经得到了足够多的信息来自己操作添加flag,但我还是想要啰嗦两句。首先是persistent参数,当你的参数作为persistent flag存在时,如注释所言,在其所有的子命令之下该参数都是可见的。而local flag则只能在该命令调用时执行。可以做一个简单的测试,在test.go的init函数中,添加如下内容:

testCmd.PersistentFlags().String("foo", "", "A help for foo")
testCmd.Flags().String("foolocal", "", "A help for foo")
go run main.go test -h
$ go run main.go test -h
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. Usage:
demo test [flags]
demo test [command] Available Commands:
testson A brief description of your command Flags:
--foo string A help for foo
--foolocal string A help for foo
-h, --help help for test Global Flags:
--config string config file (default is $HOME/.demo.yaml) Use "demo test [command] --help" for more information about a command.
go run main.go test testson -h
$ go run main.go test testson -h
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. Usage:
demo test testson [flags] Flags:
-h, --help help for testson Global Flags:
--config string config file (default is $HOME/.demo.yaml)
--foo string A help for foo

可以发现在Gloabal Flags的变化。test作为root的子命令,仍然可以使用root的persistent flag-> config(可以查看root.go),而testson作为test的子命令,不仅可以使用test的persistent flag-> fool, 也可以使用test父命令的persistent flag。从而我们可以直观的看出persistent的作用范围是该命令之后的所有子命令。接下来你可能会问,那flag支持什么类型参数?答案是,请查看官方文档
请注意,cmd.Flags().String()与 cmd.Flags().StringP()是不一样的。假如我们在test.go的init下增加如下两行:

testCmd.Flags().String("f", "", "test")
testCmd.Flags().StringP("aaa", "a", "", "test")

前者调用需要如下形式:

go run main.go test --f

后者有如下两种形式调用:

go run main.go test --aaa
go run main.go test -a

另外可以额外告知你如何使用slice作为参数,如[]string:

testCmd.Flags().StringSliceP("arr","r", nil, "test arr")

调用该参数的方法为:

go run main.go test -r "a,b,c"

请不要键入多余空格(除非确实需要键入),也不要使用空格替代逗号作为分割符。

获取参数值

在知道了如何设置参数后,我们的下一步当然便是需要在运行时获取该参数的值。现在让我们把注意力放到test.go的此部分:

var testCmd = &cobra.Command{
Use: "test",
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("test called")
},
}
RuntestCmd.Flags().StringP("aaa", "a", "", "test")
str := testCmd.Flags().GetString("aaa")
testCmd.Flags().GetStringSlice("arr")