简介

cobra是一个命令行程序库,可以用来编写命令行程序。同时,它也提供了一个脚手架,
用于生成基于 cobra 的应用程序框架。非常多知名的开源项目使用了 cobra 库构建命令行,如KubernetesHugoetcd等等等等。
本文介绍 cobra 库的基本使用和一些有趣的特性。

关于作者spf13,这里多说两句。spf13 开源不少项目,而且他的开源项目质量都比较高。
相信使用过 vim 的都知道spf13-vim,号称 vim 终极配置。
可以一键配置,对于我这样的懒人来说绝对是福音。他的viper是一个完整的配置解决方案。
完美支持 JSON/TOML/YAML/HCL/envfile/Java properties 配置文件等格式,还有一些比较实用的特性,如配置热更新、多查找目录、配置保存等。
还有非常火的静态网站生成器hugo也是他的作品。

快速使用

第三方库都需要先安装,后使用。下面命令安装了cobra生成器程序和 cobra 库:

  1. $ go get github.com/spf13/cobra/cobra

如果出现了golang.org/x/text库找不到之类的错误,需要手动从 GitHub 上下载该库,再执行上面的安装命令。我以前写过一篇博客搭建 Go 开发环境提到了这个方法。

我们实现一个简单的命令行程序 git,当然这不是真的 git,只是模拟其命令行。最终还是通过os/exec库调用外部程序执行真实的 git 命令,返回结果。
所以我们的系统上要安装 git,且 git 在可执行路径中。目前我们只添加一个子命令version。目录结构如下:

  1. get-started/
  2. cmd/
  3. helper.go
  4. root.go
  5. version.go
  6. main.go

root.go

  1. package cmd
  2. import (
  3. "errors"
  4. "github.com/spf13/cobra"
  5. )
  6. var rootCmd = &cobra.Command {
  7. Use: "git",
  8. Short: "Git is a distributed version control system.",
  9. Long: `Git is a free and open source distributed version control system
  10. designed to handle everything from small to very large projects
  11. with speed and efficiency.`,
  12. Run: func(cmd *cobra.Command, args []string) {
  13. Error(cmd, args, errors.New("unrecognized command"))
  14. },
  15. }
  16. func Execute() {
  17. rootCmd.Execute()
  18. }

version.go

  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. )
  7. var versionCmd = &cobra.Command {
  8. Use: "version",
  9. Short: "version subcommand show git version info.",
  10. Run: func(cmd *cobra.Command, args []string) {
  11. output, err := ExecuteCommand("git", "version", args...)
  12. if err != nil {
  13. Error(cmd, args, err)
  14. }
  15. fmt.Fprint(os.Stdout, output)
  16. },
  17. }
  18. func init() {
  19. rootCmd.AddCommand(versionCmd)
  20. }

main.go文件中只是调用命令入口:

  1. package main
  2. import (
  3. "github.com/go-quiz/go-daily-lib/cobra/get-started/cmd"
  4. )
  5. func main() {
  6. cmd.Execute()
  7. }

为了编码方便,在helpers.go中封装了调用外部程序和错误处理函数:

  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "github.com/spf13/cobra"
  7. )
  8. func ExecuteCommand(name string, subname string, args ...string) (string, error) {
  9. args = append([]string{subname}, args...)
  10. cmd := exec.Command(name, args...)
  11. bytes, err := cmd.CombinedOutput()
  12. return string(bytes), err
  13. }
  14. func Error(cmd *cobra.Command, args []string, err error) {
  15. fmt.Fprintf(os.Stderr, "execute %s args:%v error:%v\n", cmd.Name(), args, err)
  16. os.Exit(1)
  17. }

每个 cobra 程序都有一个根命令,可以给它添加任意多个子命令。我们在version.goinit函数中将子命令添加到根命令中。

编译程序。注意,不能直接go run main.go,这已经不是单文件程序了。如果强行要用,请使用go run .

  1. $ go build -o main.exe

cobra 自动生成的帮助信息,very cool:

  1. $ ./main.exe -h
  2. Git is a free and open source distributed version control system
  3. designed to handle everything from small to very large projects
  4. with speed and efficiency.
  5. Usage:
  6. git [flags]
  7. git [command]
  8. Available Commands:
  9. help Help about any command
  10. version version subcommand show git version info.
  11. Flags:
  12. -h, --help help for git
  13. Use "git [command] --help" for more information about a command.

单个子命令的帮助信息:

  1. $ ./main.exe version -h
  2. version subcommand show git version info.
  3. Usage:
  4. git version [flags]
  5. Flags:
  6. -h, --help help for version

调用子命令:

  1. $ ./main.exe version
  2. git version 2.19.1.windows.1

未识别的子命令:

  1. $ ./main.exe clone
  2. Error: unknown command "clone" for "git"
  3. Run 'git --help' for usage.

编译时可以将main.exe改成git,用起来会更有感觉🤩。

  1. $ go build -o git
  2. $ ./git version
  3. git version 2.19.1.windows.1

使用 cobra 构建命令行时,程序的目录结构一般比较简单,推荐使用下面这种结构:

  1. appName/
  2. cmd/
  3. cmd1.go
  4. cmd2.go
  5. cmd3.go
  6. root.go
  7. main.go

每个命令实现一个文件,所有命令文件存放在cmd目录下。外层的main.go仅初始化 cobra。

特性

cobra 提供非常丰富的功能:

  • 轻松支持子命令,如app serverapp fetch等;
  • 完全兼容 POSIX 选项(包括短、长选项);
  • 嵌套子命令;
  • 全局、本地层级选项。可以在多处设置选项,按照一定的顺序取用;
  • 使用脚手架轻松生成程序框架和命令。

首先需要明确 3 个基本概念:

  • 命令(Command):就是需要执行的操作;
  • 参数(Arg):命令的参数,即要操作的对象;
  • 选项(Flag):命令选项可以调整命令的行为。

下面示例中,server是一个(子)命令,--port是选项:

hugo server --port=1313

下面示例中,clone是一个(子)命令,URL是参数,--bare是选项:

git clone URL --bare

命令

在 cobra 中,命令和子命令都是用Command结构表示的。Command有非常多的字段,用来定制命令的行为。
在实际中,最常用的就那么几个。我们在前面示例中已经看到了Use/Short/Long/Run

Use指定使用信息,即命令怎么被调用,格式为name arg1 [arg2]name为命令名,后面的arg1为必填参数,arg3为可选参数,参数可以多个。

Short/Long都是指定命令的帮助信息,只是前者简短,后者详尽而已。

Run是实际执行操作的函数。

定义新的子命令很简单,就是创建一个cobra.Command变量,设置一些字段,然后添加到根命令中。例如我们要添加一个clone子命令:

  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. )
  7. var cloneCmd = &cobra.Command {
  8. Use: "clone url [destination]",
  9. Short: "Clone a repository into a new directory",
  10. Run: func(cmd *cobra.Command, args []string) {
  11. output, err := ExecuteCommand("git", "clone", args...)
  12. if err != nil {
  13. Error(cmd, args, err)
  14. }
  15. fmt.Fprintf(os.Stdout, output)
  16. },
  17. }
  18. func init() {
  19. rootCmd.AddCommand(cloneCmd)
  20. }

其中Use字段clone url [destination]表示子命令名为clone,参数url是必须的,目标路径destination可选。

我们将程序编译为mygit可执行文件,然后将它放到$GOPATH/bin中。我喜欢将$GOPATH/bin放到$PATH中,所以可以直接调用mygit命令了:

  1. $ go build -o mygit
  2. $ mv mygit $GOPATH/bin
  3. $ mygit clone https://github.com/go-quiz/leetcode
  4. Cloning into 'leetcode'...

大家可以继续添加命令。但是我这边只是偷了个懒,将操作都转发到实际的 git 去执行了。这确实没什么实际的用处。
有这个思路,试想一下,我们可以结合多个命令实现很多有用的工具,例如打包工具😉。

选项

cobra 中选项分为两种,一种是永久选项,定义它的命令和其子命令都可以使用。通过给根命令添加一个选项定义全局选项。
另一种是本地选项,只能在定义它的命令中使用。

cobra 使用pflag解析命令行选项。pflag使用上基本与flag相同,该系列文章有一篇介绍flag库的,Go 每日一库之 flag

flag一样,存储选项的变量也需要提前定义好:

  1. var Verbose bool
  2. var Source string

设置永久选项:

  1. rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")

设置本地选项:

  1. localCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")

两种参数都是相同的,长选项/短选项名、默认值和帮助信息。

下面,我们通过一个案例来演示选项的使用。

假设我们要做一个简单的计算器,支持加、减、乘、除操作。并且可以通过选项设置是否忽略非数字参数,设置除 0 是否报错。
显然,前一个选项应该放在全局选项中,后一个应该放在除法命令中。程序结构如下:

  1. math/
  2. cmd/
  3. add.go
  4. divide.go
  5. minus.go
  6. multiply.go
  7. root.go
  8. main.go

这里展示divide.goroot.go,其它命令文件都类似。完整代码我放在GitHub上了。

divide.go

  1. var (
  2. dividedByZeroHanding int // 除 0 如何处理
  3. )
  4. var divideCmd = &cobra.Command {
  5. Use: "divide",
  6. Short: "Divide subcommand divide all passed args.",
  7. Run: func(cmd *cobra.Command, args []string) {
  8. values := ConvertArgsToFloat64Slice(args, ErrorHandling(parseHandling))
  9. result := calc(values, DIVIDE)
  10. fmt.Printf("%s = %.2f\n", strings.Join(args, "/"), result)
  11. },
  12. }
  13. func init() {
  14. divideCmd.Flags().IntVarP(&dividedByZeroHanding, "divide_by_zero", "d", int(PanicOnDividedByZero), "do what when divided by zero")
  15. rootCmd.AddCommand(divideCmd)
  16. }

root.go

  1. var (
  2. parseHandling int
  3. )
  4. var rootCmd = &cobra.Command {
  5. Use: "math",
  6. Short: "Math calc the accumulative result.",
  7. Run: func(cmd *cobra.Command, args []string) {
  8. Error(cmd, args, errors.New("unrecognized subcommand"))
  9. },
  10. }
  11. func init() {
  12. rootCmd.PersistentFlags().IntVarP(&parseHandling, "parse_error", "p", int(ContinueOnParseError), "do what when parse arg error")
  13. }
  14. func Execute() {
  15. rootCmd.Execute()
  16. }

divide.go中定义了如何处理除 0 错误的选项,在root.go中定义了如何处理解析错误的选项。选项枚举如下:

  1. const (
  2. ContinueOnParseError ErrorHandling = 1 // 解析错误尝试继续处理
  3. ExitOnParseError ErrorHandling = 2 // 解析错误程序停止
  4. PanicOnParseError ErrorHandling = 3 // 解析错误 panic
  5. ReturnOnDividedByZero ErrorHandling = 4 // 除0返回
  6. PanicOnDividedByZero ErrorHandling = 5 // 除0 painc
  7. )

其实命令的执行逻辑并不复杂,就是将参数转为float64。然后执行相应的运算,输出结果。

测试程序:

  1. $ go build -o math
  2. $ ./math add 1 2 3 4
  3. 1+2+3+4 = 10.00
  4. $ ./math minus 1 2 3 4
  5. 1-2-3-4 = -8.00
  6. $ ./math multiply 1 2 3 4
  7. 1*2*3*4 = 24.00
  8. $ ./math divide 1 2 3 4
  9. 1/2/3/4 = 0.04

默认情况,解析错误被忽略,只计算格式正确的参数的结果:

  1. $ ./math add 1 2a 3b 4
  2. 1+2a+3b+4 = 5.00
  3. $ ./math divide 1 2a 3b 4
  4. 1/2a/3b/4 = 0.25

设置解析失败的处理,2 表示退出程序,3 表示 panic(看上面的枚举):

  1. $ ./math add 1 2a 3b 4 -p 2
  2. invalid number: 2a
  3. $ ./math add 1 2a 3b 4 -p 3
  4. panic: strconv.ParseFloat: parsing "2a": invalid syntax
  5. goroutine 1 [running]:
  6. github.com/go-quiz/go-daily-lib/cobra/math/cmd.ConvertArgsToFloat64Slice(0xc00004e300, 0x4, 0x6, 0x3, 0xc00008bd70, 0x504f6b, 0xc000098600)
  7. D:/code/golang/src/github.com/go-quiz/go-daily-lib/cobra/math/cmd/helper.go:58 +0x2c3
  8. github.com/go-quiz/go-daily-lib/cobra/math/cmd.glob..func1(0x74c620, 0xc00004e300, 0x4, 0x6)
  9. D:/code/golang/src/github.com/go-quiz/go-daily-lib/cobra/math/cmd/add.go:14 +0x6d
  10. github.com/spf13/cobra.(*Command).execute(0x74c620, 0xc00004e1e0, 0x6, 0x6, 0x74c620, 0xc00004e1e0)
  11. D:/code/golang/src/github.com/spf13/cobra/command.go:835 +0x2b1
  12. github.com/spf13/cobra.(*Command).ExecuteC(0x74d020, 0x0, 0x599ee0, 0xc000056058)
  13. D:/code/golang/src/github.com/spf13/cobra/command.go:919 +0x302
  14. github.com/spf13/cobra.(*Command).Execute(...)
  15. D:/code/golang/src/github.com/spf13/cobra/command.go:869
  16. github.com/go-quiz/go-daily-lib/cobra/math/cmd.Execute(...)
  17. D:/code/golang/src/github.com/go-quiz/go-daily-lib/cobra/math/cmd/root.go:45
  18. main.main()
  19. D:/code/golang/src/github.com/go-quiz/go-daily-lib/cobra/math/main.go:8 +0x35

至于除 0 选项大家自己试试。

细心的朋友应该都注意到了,该程序还有一些不完善的地方。例如这里如果输入非数字参数,该参数也会显示在结果中:

  1. $ ./math add 1 2 3d cc
  2. 1+2+3d+cc = 3.00

感兴趣可以自己完善一下~

脚手架

通过前面的介绍,我们也看到了其实 cobra 命令的框架还是比较固定的。这就有了工具的用武之地了,可极大地提高我们的开发效率。

前面安装 cobra 库的时候也将脚手架程序安装好了。下面我们介绍如何使用这个生成器。

使用cobra init命令创建一个 cobra 应用程序:

  1. $ cobra init scaffold --pkg-name github.com/go-quiz/go-daily-lib/cobra/scaffold

其中scaffold为应用程序名,后面通过pkg-name选项指定包路径。生成的程序目录结构如下:

  1. scaffold/
  2. cmd/
  3. root.go
  4. LICENSE
  5. main.go

这个项目结构与之前介绍的完全相同,也是 cobra 推荐使用的结构。同样地,main.go也仅仅是入口。

root.go中,工具额外帮我们生成了一些代码。

在根命令中添加了配置文件选项,大部分应用程序都需要配置文件:

  1. func init() {
  2. cobra.OnInitialize(initConfig)
  3. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.scaffold.yaml)")
  4. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  5. }

在初始化完成的回调中,如果发现该选项为空,则默认使用主目录下的.scaffold.yaml文件:

  1. func initConfig() {
  2. if cfgFile != "" {
  3. viper.SetConfigFile(cfgFile)
  4. } else {
  5. home, err := homedir.Dir()
  6. if err != nil {
  7. fmt.Println(err)
  8. os.Exit(1)
  9. }
  10. viper.AddConfigPath(home)
  11. viper.SetConfigName(".scaffold")
  12. }
  13. viper.AutomaticEnv()
  14. if err := viper.ReadInConfig(); err == nil {
  15. fmt.Println("Using config file:", viper.ConfigFileUsed())
  16. }
  17. }

这里用到了我前几天介绍的go-homedir库。配置文件的读取使用了 spf13 自己的开源项目viper(毒龙?真是起名天才)。

除了代码文件,cobra 还生成了一个 LICENSE 文件。

现在这个程序还不能做任何事情,我们需要给它添加子命令,使用cobra add命令:

  1. $ cobra add date

该命令在cmd目录下新增了date.go文件。基本结构已经搭好了,剩下的就是修改一些描述,添加一些选项了。

我们现在实现这样一个功能,根据传入的年、月,打印这个月的日历。如果没有传入选项,使用当前的年、月。

选项定义:

  1. func init() {
  2. rootCmd.AddCommand(dateCmd)
  3. dateCmd.PersistentFlags().IntVarP(&year, "year", "y", 0, "year to show (should in [1000, 9999]")
  4. dateCmd.PersistentFlags().IntVarP(&month, "month", "m", 0, "month to show (should in [1, 12]")
  5. }

修改dateCmdRun函数:

  1. Run: func(cmd *cobra.Command, args []string) {
  2. if year < 1000 && year > 9999 {
  3. fmt.Fprintln(os.Stderr, "invalid year should in [1000, 9999], actual:%d", year)
  4. os.Exit(1)
  5. }
  6. if month < 1 && year > 12 {
  7. fmt.Fprintln(os.Stderr, "invalid month should in [1, 12], actual:%d", month)
  8. os.Exit(1)
  9. }
  10. showCalendar()
  11. }

showCalendar函数就是利用time提供的方法实现的,这里就不赘述了。感兴趣可以去我的 GitHub 上查看实现。

看看程序运行效果:

  1. $ go build -o main.exe
  2. $ ./main.exe date
  3. Sun Mon Tue Wed Thu Fri Sat
  4. 1 2 3 4
  5. 5 6 7 8 9 10 11
  6. 12 13 14 15 16 17 18
  7. 19 20 21 22 23 24 25
  8. 26 27 28 29 30 31
  9. $ ./main.exe date --year 2019 --month 12
  10. Sun Mon Tue Wed Thu Fri Sat
  11. 1 2 3 4 5 6 7
  12. 8 9 10 11 12 13 14
  13. 15 16 17 18 19 20 21
  14. 22 23 24 25 26 27 28
  15. 29 30 31

可以再为这个程序添加其他功能,试一试吧~

其他

cobra 提供了非常丰富的特性和定制化接口,例如:

  • 设置钩子函数,在命令执行前、后执行某些操作;
  • 生成 Markdown/ReStructed Text/Man Page 格式的文档;
  • 等等等等。

由于篇幅限制,就不一一介绍了。有兴趣可自行研究。cobra 库的使用非常广泛,很多知名项目都有用到,前面也提到过这些项目。
学习这些项目是如何使用 cobra 的,可以从中学习 cobra 的特性和最佳实践。这也是学习开源项目的一个很好的途径。

文中所有示例代码都已上传至我的 GitHub,Go 每日一库https://github.com/go-quiz/go-daily-lib/tree/master/cobra。

参考

  1. cobra GitHub 仓库