Go Client 框架 Cobra 介绍

发布时间: 更新时间: 总字数:558 阅读时间:2m 作者: IP属地: 分享 复制网址

Cobra 是一个用于创建强大的、基于Golang client 的lib库,常见的hugokubernetes均基于其创建。

安装

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

flag 说明

  • Required flags 默认添加的 flags,示例未指定时会报错
rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
rootCmd.MarkFlagRequired("region")
  • Persistent Flags 用于当前命令子命令
    • rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug log")
  • Local Flags 仅用于当前命令
    • xxxCmd.Flags().StringVarP(&xPath, "path", "p", "", "some path")
  • 使用 TraverseChildren 解析每个命令的 local flags
command := cobra.Command{
    Use: "print [OPTIONS] [COMMANDS]",
    TraverseChildren: true,
}

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
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数