https://github.com/spf13/cobra
https://github.com/xiazemin/cobra
Cobra提供的功能
简易的子命令行模式,如 app server, app fetch等等
完全兼容posix命令行模式
嵌套子命令subcommand
支持全局,局部,串联flags
使用Cobra很容易的生成应用程序和命令,使用cobra create appname和cobra add cmdname
如果命令输入错误,将提供智能建议,如 app srver,将提示srver没有,是否是app server
自动生成commands和flags的帮助信息
自动生成详细的help信息,如app help
自动识别-h,–help帮助flag
自动生成应用程序在bash下命令自动完成功能
自动生成应用程序的man手册
命令行别名
自定义help和usage信息
可选的紧密集成的viper apps
cobra init github.com/xiazemin/cobra/myapp
Error: required flag(s) “pkg-name” not set
Usage:
cobra init [name] [flags]
Aliases:
init, initialize, initialise, create
https://github.com/spf13/cobra/issues/901
Hi, I tried using the following: cobra init gitHub.com/pookah-net/test, from go/src, as specified in the README, to create a new Golang project using Cobra, and it kept telling me Error: required flag(s) “pkg-name” not set and giving me the help text.
I finally (!) figured out that cobra init –pkg-name gitHub.com/pookah-net/test was needed, instead. Is it supposed to be that way?
(Oh, it didn’t create the src/github.com/pookah-net/test folder. Instead, it created src/cmd/root.go, and src/main.go and src/LICENSE files and folders!)
$ cobra init –pkg-name github.com/xiazemin/cobra/gen
Your Cobra applicaton is ready at
/Users/didi/goLang/src/github.com/xiazemin/cobra/gen
$ tree
.
├── LICENSE
├── cmd
│ └── root.go
└── main.go
生成了三个文件
$ cobra add serve
serve created at /Users/didi/goLang/src/github.com/xiazemin/cobra/gen
i$ 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:
gen [command]
Available Commands:
help Help about any command
serve A brief description of your command
Flags:
–config string config file (default is $HOME/.gen.yaml)
-h, –help help for gen
-t, –toggle Help message for toggle
Use “gen [command] –help” for more information about a command.
$ go build main.go
$ ./main serve
serve called
$ cobra add create -p ‘configCmd’
create created at /Users/didi/goLang/src/github.com/xiazemin/cobra/gen
i$ go build main.go
cmd/create.go:40:2: undefined: configCmd
$ cobra add -h
Add (cobra add) will create a new command, with a license and
the appropriate structure for a Cobra-based CLI application,
and register it to its parent (default rootCmd).
If you want your command to be public, pass in the command name
with an initial uppercase letter.
Example: cobra add server -> resulting in a new cmd/server.go
Usage:
cobra add [command name] [flags]
Aliases:
add, command
Flags:
-h, –help help for add
-p, –parent string variable name of parent command for this command (default “rootCmd”)
Global Flags:
-a, –author string author name for copyright attribution (default “YOUR NAME”)
–config string config file (default is $HOME/.cobra.yaml)
-l, –license string name of license for the project
–viper use Viper for configuration (default true)
-p 是指定父命令的,父命令不存在,所以报错了
$ cobra add create -p ‘createCmd’
create created at /Users/didi/goLang/src/github.com/xiazemin/cobra/gen
$ go build main.go
成功了
$ ./main create
panic: Command can’t be a child of itself
goroutine 1 [running]:
github.com/spf13/cobra.(*Command).AddCommand(0x16df660, 0xc42012bf48, 0x1, 0x1)
/Users/didi/goLang/src/github.com/spf13/cobra/command.go:1061 +0x2af
github.com/xiazemin/cobra/gen/cmd.init.0()
/Users/didi/goLang/src/github.com/xiazemin/cobra/gen/cmd/create.go:40 +0x5e
github.com/xiazemin/cobra/gen/cmd.init()
main.init()
因为指定了命令的父命令是它本身,运行时报错了
$ cobra add create -p ‘serveCmd’
create created at /Users/didi/goLang/src/github.com/xiazemin/cobra/gen
$ ./main create
Error: unknown command “create” for “gen”
Run ‘gen –help’ for usage.
unknown command “create” for “gen”
$ ./main serve create
create called
给命令加参数
分两种:
1,全局参数,当前命令和子命令可用
2,局部参数,当前命令可用
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
serveCmd.PersistentFlags().String(“foo”, “”, “A help for foo”)
serveCmd.Flags().BoolP(“toggle”, “t”, false, “Help message for toggle”)
$ ./main serve –foo=”abc”
serve called
$ ./main serve create –foo=”abc”
create called
$ ./main serve –foo=”abc” create
create called
$ ./main serve -t=true
serve called
$ ./main serve create -t=true
Error: unknown shorthand flag: ‘t’ in -t=true
cobra 的子孙链路可以非常长
$ cobra add grandchild -p ‘createCmd’
grandchild created at /Users/didi/goLang/src/github.com/xiazemin/cobra/gen
$ ./main serve create grandchild –foo=”abc”
grandchild called
在Run这里来获取参数并执行我们的命令功能。
foo,err:=cmd.Flags().GetString(“foo”)
fmt.Println(foo,err)
$ ./main serve create grandchild –foo=”abc”
grandchild called
abc
生成文挡
https://github.com/spf13/cobra/blob/master/doc/md_docs.md
生成文档
在root.go中增加
func Get()*cobra.Command{
return rootCmd
}
main.go 中import
“github.com/spf13/cobra/doc”
在main中增加
err := doc.GenMarkdownTree(cmd.Get(), “./”)
if err != nil {
log.Fatal(err)
}
$ go build main.go
../../../spf13/cobra/doc/man_docs.go:27:2: cannot find package “github.com/cpuguy83/go-md2man/md2man” in any of:
/usr/local/go/src/github.com/cpuguy83/go-md2man/md2man (from $GOROOT)
/Users/didi/goLang/src/github.com/cpuguy83/go-md2man/md2man (from $GOPATH)
/Users/didi/PhpstormProjects/go/src/github.com/cpuguy83/go-md2man/md2man
$ go get github.com/cpuguy83/go-md2man/md2man
$ ./main
生成3个文件
├── gen.md
├── gen_serve.md
├── gen_serve_create.md
├── gen_serve_create_grandchild.md