18.cobra框架了解

概述

github cobra

cobra 快速的实现一个命令行客户端,命令行解析工具。

cobra 中的主要概念

  • -Commands 表示执行运作
  • -Args 执行参数
  • -Flags 这些运作的标识符

举例

  • git clone 命令

git clone https://github.com/spf13/cobra.git --bare

  • git 代表执行的二进制
  • clone 代表执行的运作,可以理解为命令
  • https://github.com/spf13/cobra.git 代表参数
  • –bare 代表标识符,意思是创建个祼库

安装

# 前置条件
go env -w GO111MODULE=on
# 检查
go env 

appledeMacBook-Pro:cobra hyl$ go install github.com/spf13/cobra-cli@latest
go: downloading github.com/spf13/cobra-cli v1.3.0
go: downloading github.com/spf13/cobra v1.3.0
go: downloading github.com/spf13/viper v1.10.1
go: downloading github.com/spf13/jwalterweatherman v1.1.0
go: downloading github.com/spf13/afero v1.6.0
go: downloading github.com/spf13/cast v1.4.1
go: downloading github.com/magiconair/properties v1.8.5
go: downloading github.com/mitchellh/mapstructure v1.4.3
go: downloading github.com/fsnotify/fsnotify v1.5.1
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading gopkg.in/ini.v1 v1.66.2
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/pelletier/go-toml v1.9.4
go: downloading golang.org/x/text v0.3.7
go: downloading golang.org/x/sys v0.0.0-20211210111614-af8b64212486

appledeMacBook-Pro:bin hyl$ ./cobra-cli -h
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.
appledeMacBook-Pro:bin hyl$ pwd
/Users/hyl/Desktop/jk/go/bin

/Users/hyl/Desktop/jk/go/bin/cobra-cli

在这里插入图片描述
在这里插入图片描述

实践

bash: no job control in this shell
appledeMacBook-Pro:cobra-demo hyl$ go get -u github.com/spf13/cobra@latest
go: can only use path@version syntax with 'go get' and 'go install' in module-aware mode
appledeMacBook-Pro:cobra-demo hyl$ go mod init
go: creating new go.mod: module cobra-demo
appledeMacBook-Pro:cobra-demo hyl$ go mod tidy
go: warning: "all" matched no packages
appledeMacBook-Pro:cobra-demo hyl$ 
appledeMacBook-Pro:cobra-demo hyl$ go get -u github.com/spf13/cobra@latest
go: added github.com/inconshreveable/mousetrap v1.1.0
go: added github.com/spf13/cobra v1.8.0
go: added github.com/spf13/pflag v1.0.5

官网文档

实践

官网文档

appledeMacBook-Pro:src hyl$ mkdir cobra
appledeMacBook-Pro:src hyl$ ls
cobra		github.com	k8s.io		test
appledeMacBook-Pro:src hyl$ cd cobra/

appledeMacBook-Pro:cobra hyl$ go run "/Users/hyl/Desktop/jk/go/src/cobra/main.go"
测试
appledeMacBook-Pro:cobra hyl$ go mod tidy

# 结合官网,组合成代码,按错误提示,引入类


appledeMacBook-Pro:cobra hyl$ go run main.go times
测试
Error: requires at least 1 arg(s), only received 0
Usage:
  exe times [flags]

Flags:
  -h, --help        help for times
  -t, --times int   times to echo the input (default 1)

requires at least 1 arg(s), only received 0
exit status 1
appledeMacBook-Pro:cobra hyl$ 

appledeMacBook-Pro:cobra hyl$ go run main.go times -t=4 k8s
测试
times call...
echo:k8s
echo:k8s
echo:k8s
echo:k8s
appledeMacBook-Pro:cobra hyl$ go run main.go times  k8s
测试
times call...
echo:k8s
appledeMacBook-Pro:cobra hyl$ 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流月up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值