Cobra 是一个 Golang 下的简单易用的命令行应用工具库,类似于之前提到的 GCli。
安装 cobra 命令行程序
使用 go install
安装即可:
$ go get github.com/spf13/cobra/cobra && go install github.com/spf13/cobra/cobra
执行完上述命令后会在 $GOPATH/bin
目录下生成一个名为 cobra
的命令行程序,保证 $GOPATH/bin
目录存在于 PATH
环境变量下即可直接使用该命令:
$ cobra
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:
cobra [command]
Available Commands:
add Add a command to a Cobra Application
completion Generate the autocompletion script for the specified shell
help Help about any command
init Initialize a Cobra Application
Flags:
-a, --author string author name for copyright attribution (default "YOUR NAME")
--config string config file (default is $HOME/.cobra.yaml)
-h, --help help for cobra
-l, --license string name of license for the project
--viper use Viper for configuration
Use "cobra [command] --help" for more information about a command.
初始化应用目录结构
创建应用目录:
$ mkdir cobra_t
$ cd cobra_t/
$ go mod init zze326/cobra_t
$ cobra init
执行完上述命令后,应用目录结构如下:
$ tree
.
├── LICENSE
├── cmd
│ └── root.go
├── go.mod
├── go.sum
└── main.go
1 directory, 5 files
运行该程序可以看到对 cobra 的说明介绍:
$ 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 add <cmd_name>
来完成,比如:
cobra add serve
cobra add config
cobra add create -p 'configCmd'
上述三行命令就会创建三个自定义命令,可以注意到第三行的命令用到了 -p
选项,并指定其值为 configCmd
,这表示将 create
命令添加为 config
命令的子命令。
由于
config
命令在代码中的变量字面量形式为configCmd
,所以是-p configCmd
而不是-p config
。
此时 cmd
目录为每个命令添加了一个 go
文件,如下:
$ ls cmd/
config.go create.go root.go serve.go
新添加的几个命令的按添加顺序执行方式分别如下:
$ go run main.go serve
serve called
$ go run main.go config
config called
$ go run main.go config create
create called
示例
下面新建一个名为 person
命令,在执行该命令时可通过选项参数传入姓名、年龄、性别。
新建子命令:
$ cobra add person
修改生成的 person.go
文件:
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// personCmd represents the person command
var personCmd = &cobra.Command{
Use: "person",
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) {
name := cmd.Flag("name").Value
age := cmd.Flag("age").Value
isMale := cmd.Flag("male").Value
fmt.Println(name)
fmt.Println(age)
fmt.Println(isMale)
},
}
func init() {
rootCmd.AddCommand(personCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// personCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
personCmd.Flags().StringP("name", "n", "", "姓名")
personCmd.Flags().Int8P("age", "a", 0, "年龄")
personCmd.Flags().BoolP("male", "m", false, "是否是男性")
}
运行程序:
# 短格式选项
$ go run main.go person -n zze -a 25 -m
zze
25
true
# 长格式选项
$ go run main.go person --name zze --age 35 --male
zze
35
true
参考:
评论区