您的位置:首页 > 游戏 > 手游 > 18.cobra框架了解

18.cobra框架了解

2024/12/27 21:59:37 来源:https://blog.csdn.net/2301_79691134/article/details/135940764  浏览:    关键词: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-af8b64212486appledeMacBook-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 Applicationcompletion  Generate the autocompletion script for the specified shellhelp        Help about any commandinit        Initialize a Cobra ApplicationFlags:-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 configurationUse "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$ 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com