Cobra
是一个用于创建强大的、基于Golang
client 的lib库,常见的hugo
、kubernetes
均基于其创建。
安装
go install github.com/spf13/cobra-cli@latest
重要概念
Command
:是Cobra的核心,通过交互命令体现,命令可以包含子命令
Flags
:命令的扩展属性
Help
# cobra-cli
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-cli [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-cli
-l, --license string name of license for the project
--viper use Viper for configuration
Use "cobra-cli [command] --help" for more information about a command.
init
$ Initialize (cobra init) will create a new application, with a license
and the appropriate structure for a Cobra-based CLI application.
Cobra init must be run inside of a go module (please run "go mod init <MODNAME>" first)
Usage:
cobra-cli init [path] [flags]
Aliases:
init, initialize, initialise, create
Flags:
-h, --help help for init
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
add
$ cobra-cli 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-cli 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
demo
mkdir goto
cd goto
go mod init github.com/xiexianbin/goto
# 初始化
cobra-cli init . --author xiexianbin.cn -l Apache
# 添加子命令
cobra-cli add <abc> --author xiexianbin.cn -l Apache