是一个golang的库,其提供简单的接口来创建强大现代的CLI接口,类似于git或者go工具。同时,它也是一个应用,用来生成个人应用框架,从而开发以Cobra为基础的应用。热门的docker和k8s源码中都使用了Cobra Cobra 结构由三部分组成:命令( Command )、标志( Flag )、参数( Args )。
1 2 3 4 5 6 7
type Command struct { Usestring// The one-line usage message. Short string// The short description shown in the 'help' output. Long string// The long message shown in the 'help<this-command>' output. Runfunc(cmd *Command, args []string) // Run runs the command. ... }
[root@k8s-m1 guanzhang]# cd $GOPATH [root@k8s-m1 go]# cd src [root@k8s-m1 src]# ll total 12 drwxr-xr-x 4 root root 4096 Jun 3 14:03 guanzhang drwxr-xr-x 3 root root 4096 May 29 13:18 spyder drwxr-xr-x 2 root root 4096 May 22 11:56 test [root@k8s-m1 src]# cobra init test/cli Your Cobra application is ready at /root/go/src/test/cli
Give it a try by going there and running `go run main.go`. Add commands to it by running `cobra add [cmdname]`. [root@k8s-m1 src]# cd test/cli [root@k8s-m1 cli]# ll total 20 drwxr-xr-x 2 root root 4096 Jun 3 16:26 cmd -rw-r--r-- 1 root root 11358 Jun 3 16:26 LICENSE -rw-r--r-- 1 root root 674 Jun 3 16:26 main.go
// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "cli", 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() { iferr := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } }
运行查看
1 2 3 4 5 6 7
[root@k8s-m1 app]# 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 applicationis a tool to generate the needed files to quickly create a Cobra application.
当前命令为cli没有子命令,无法观察出它的强大
添加子命令达到想要的层级关系
1 2
cli |----app
使用cobra生成器添加一个上面二级的app命令
1 2 3 4 5 6
[root@k8s-m1 cli]# cobra add app app created at /root/go/src/test/cli/cmd/app.go [root@k8s-m1 cli]# ll cmd/ total 8 -rw-r--r-- 1 root root 1611 Jun 3 16:29 app.go -rw-r--r-- 1 root root 2776 Jun 3 16:26 root.go
[root@k8s-m1 cli]# 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.
Usage: cli [command]
Available Commands: app A brief description of your command helpHelpabout any command
Flags: --config string config file (default is $HOME/.cli.yaml) -h, --helphelpforcli -t, --toggle Help message for toggle
Use"cli [command] --help"formore information about a command.
发现没有子命令的时候会打印可用的二级命令,里面有我们添加的app命令,来run下app命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@k8s-m1 cli]# go run main.go helpapp 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: cliapp [flags]
Flags: -h, --helphelpforapp
Global Flags: --config string config file (default is $HOME/.cli.yaml)
我们可以看到cmd/app.go里有这段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// appCmd represents the app command var appCmd = &cobra.Command{ Use: "app", 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("app called") }, }
cli |----app |----remove [root@k8s-m1 cli]# cobra addremove remove created at /root/go/src/test/cli/cmd/remove.go [root@k8s-m1 cli]# grep AddCommand cmd/remove.go rootCmd.AddCommand(removeCmd) [root@k8s-m1 cli]# sed -i '/rootCmd/s#rootCmd#appCmd#' cmd/remove.go [root@k8s-m1 cli]# grep AddCommand cmd/remove.go appCmd.AddCommand(removeCmd) [root@k8s-m1 cli]# go run main.go app app called [root@k8s-m1 cli]# go run main.go app remove remove called [root@k8s-m1 cli]# go run main.go app help app called [root@k8s-m1 cli]# go run main.go app --help A longer description that spans multiple lines and likely contains examples and usage of using your command. For example:
Cobra isa CLI library for Go that empowers applications. This application isa tool to generate the needed files to quickly create a Cobra application.
Usage: cli app [flags] cli app [command]
Available Commands: remove A brief description of your command
Flags: -h, --helphelpfor app
Global Flags: --config string config file (default is $HOME/.cli.yaml)
Use "cli app [command] --help"for more information about acommand.
上面并没有达到我们预期的输出,我们期望是go run main.go app的时候输出最后的--help这样的命令帮助提醒用户。这样有两种实现方法,一种是把var appCmd = &cobra.Command的时候Run删掉,或者像下面改成RunE:
1 2 3
RunE: func(cmd *cobra.Command, args []string)error { return errors.New("Provide item to the app command") },
[root@k8s-m1 cli]# go run main.go app remove -n test remove the test [root@k8s-m1 cli]# go run main.go app remove remove the default [root@k8s-m1 cli]# go run main.go app remove --help A longer description that spans multiple lines and likely contains examples and usage of using your command. For example:
Cobra isa CLI library for Go that empowers applications. This application isa tool to generate the needed files to quickly create a Cobra application.
Usage: cli app remove [flags]
Flags: -h, --helphelpforremove -n, --name string The application tobe executed
Global Flags: --config string config file (default is $HOME/.cli.yaml)
带P的相对没带P的多了个短选项,没带P的选项只能用--long-iotion这样。单独的短选项官方提了issue确定了官方从不打算单独的短选项。获取选项的值用cmd.Flags().GetString("name") 不带P下纯type例如.String("name", "","The application to be executed")就是单独的长选项,最后俩参数是默认值和打印输出帮助时候后面的描述字符。 不带Var的获取值使用GetType("optionName"),这样似乎非常麻烦,实际中都是用后面俩种Var直接传入地址自动注入的,例如
1 2
var dates int32 cmd.Flags().Int32VarP(&dates,"date", "d", 1234, "this is var test")
[root@k8s-m1 cli]# go run main.go app remove A longer description that spans multiple lines and likely contains examples and usage of using your command. For example:
Cobra isa CLI library for Go that empowers applications. This application isa tool to generate the needed files to quickly create a Cobra application.
Usage: cli app remove [flags]
Aliases: remove, rm
Flags: -h, --helphelpforremove -n, --name string The application tobe executed
Global Flags: --config string config file (default is $HOME/.cli.yaml)
var removeCmd = &cobra.Command{ Use: "remove", Aliases: []string{"rm"}, Example: ` cli remove -n test cli remove --name test `, go run main.go app remove A longer description that spans multiple lines and likely contains examples and usage of using your command. For example:
Cobra isa CLI library for Go that empowers applications. This application isa tool to generate the needed files to quickly create a Cobra application.
Usage: cli app remove [flags]
Aliases: remove, rm
Examples:
cli remove -n test cli remove --name test
Flags: -h, --helphelpforremove -n, --name string The application tobe executed
Global Flags: --config string config file (default is $HOME/.cli.yaml)