Golang — 命令行工具cobra
文章目录
- Cobra
- 特性
- 概念
- Command
- Flag
- 全局标识(Persistent Flags)
- 局部标识(Local Flags)
- Run Hooks
Cobra
Cobra是一个用于Go语言的CLI框架。它包含一个用于创建强大的现代CLI应用程序的库,以及一个用于快速生成基于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
- 自动识别帮助 flag -h,–help
- 自动生成应用程序在 bash 下命令自动完成功能
- 自动生成应用程序的 man 手册
- 命令行别名
- 自定义 help 和 usage 信息
- 可选的与 viper apps 的紧密集成
概念
Cobra是建立在命令、参数和标志的结构上的。
Commands代表动作,Args是东西,Flags是这些动作的修饰符。
最好的应用读起来像句子。用户会知道如何使用应用程序,因为他们天生就知道如何使用它。
例如:
hugo server --port=1313
Command
Command是应用程序的中心点。应用程序支持的每个交互都包含在Command中。命令可以有子命令,也可以有选择地运行操作。
Flag
flag 是修改命令行为的一种方式。Cobra完全支持兼容posix标志以及Go标志包。Cobra命令可以定义贯穿子命令的标志,以及只对该命令可用的标志。
上面例子中--port
即是一个flag
全局标识(Persistent Flags)
全局标识(Persistent Flags)会作用于其指定的命令与指定命令所有的子命令。
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
局部标识(Local Flags)
局部标示(Local Flags)仅作用于其指定命令。
rootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
Run Hooks
可以在命令的主run
函数之前或之后运行函数。PersistentPreRun
和PreRun
函数将在Run
之前执行。persistentpoststrun
和poststrun
会在Run
之后执行。如果子类不声明自己的Persistent*Run
函数,它们将被子类继承。这些函数按以下顺序运行:
- PersistentPreRun
- PreRun
- Run
- PostRun
- PersistentPostRun
示例:
package mainimport ("fmt""github.com/spf13/cobra"
)func main() {var rootCmd = &cobra.Command{Use: "root [sub]",Short: "My root command",PersistentPreRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)},PreRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)},Run: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside rootCmd Run with args: %v\n", args)},PostRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)},PersistentPostRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)},}var subCmd = &cobra.Command{Use: "sub [no options!]",Short: "My subcommand",PreRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside subCmd PreRun with args: %v\n", args)},Run: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside subCmd Run with args: %v\n", args)},PostRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside subCmd PostRun with args: %v\n", args)},PersistentPostRun: func(cmd *cobra.Command, args []string) {fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args)},}rootCmd.AddCommand(subCmd)rootCmd.SetArgs([]string{""})rootCmd.Execute()fmt.Println()rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})rootCmd.Execute()
}
output:
Inside rootCmd PersistentPreRun with args: []
Inside rootCmd PreRun with args: []
Inside rootCmd Run with args: []
Inside rootCmd PostRun with args: []
Inside rootCmd PersistentPostRun with args: []Inside rootCmd PersistentPreRun with args: [arg1 arg2]
Inside subCmd PreRun with args: [arg1 arg2]
Inside subCmd Run with args: [arg1 arg2]
Inside subCmd PostRun with args: [arg1 arg2]
Inside subCmd PersistentPostRun with args: [arg1 arg2]
Golang — 命令行工具cobra相关推荐
- 酷玩Go命令行工具—Cobra
不知大家有没有在使用Git命令.Linux的yum命令.Go命令.Maven命令的时候感觉到非常的酷,比如你刚刚拿到一个Go的开源项目,初始化时只需要输入go mod tidy进行对依赖的下载,或者是 ...
- golang开发命令行工具-cobra
包地址:go get -u github.com/spf13/cobra/cobra go mod init blog go get -u github.com/spf13/cobra/cobra 具 ...
- 命令行工具cobra的使用
安装cobra go get -v github.com/spf13/cobra/cobra 切换到GOPATH的src目录下并创建一个新文件夹:demo cd $GOPATH/src mkdir d ...
- 使用 Cobra 构建命令行工具
文章首发于个人公众号:阿拉平平 最近折腾了下命令行库 Cobra,和大家分享下.本文演示环境为 CentOS 7.5,Golang 1.11. 文章目录 1. Cobra 介绍 1.1 概念 1.2 ...
- Go命令行库Cobra的使用
点击上方"朱小厮的博客",选择"设为星标" 回复"1024"获取独家整理的学习资料 欢迎跳转到本文的原文链接:https://honeypp ...
- 非零基础自学Golang 第15章 Go命令行工具 15.4 注释文档(doc)
非零基础自学Golang 文章目录 非零基础自学Golang 第15章 Go命令行工具 15.4 注释文档(doc) 第15章 Go命令行工具 15.4 注释文档(doc) Go语言文档工具go do ...
- 使用Cobra开发自己的命令行工具
Cobra 项目地址:https://github.com/spf13/cobra 1 新建cobra项目 # 安装cobra-cli工具 go install github.com/spf13/co ...
- go Cobra命令行工具入门
简介 Github:https://github.com/spf13/cobra Star:26.5K Cobra是一个用Go语言实现的命令行工具.并且现在正在被很多项目使用,例如:Kubernete ...
- golang实现一个linux命令ls命令(命令行工具构建)
希望2023可以听到这些话: 恭喜你得到了这份工作 恭喜你的建议被采用了 恭喜你被录取了 恭喜你的考试顺利通过了 恭喜你上岸了 恭喜你升职了 恭喜你加薪了 恭喜你体检结果一切正常 在这篇文章下面许个愿 ...
最新文章
- muduo之Logger
- 【Egret】WebSocket 的使用说明
- [转] sql server 跨数据库调用存储过程
- 计算机专业英语第二版张强华翻译_计算机语言发展的三个阶段,机器语言、汇编语言与高级语言...
- jar包部署到window系统服务器的办法
- halcon获取图像中心点_关于Halcon的复杂图形中心点查找
- c#基础这些你都看过吗?(一)-----仅供初学者使用
- 帮助新手理解equals和hashCode
- android 基础布局
- 第九届蓝桥杯C++A组
- Mac 系统安装Chromedriver
- 全国独一份!200万杭州人公积金可用支付宝刷脸提取,秒到账!
- 基于TCP/UDP的NIO服务端/客户端代码实现damo(java)
- 5.3 Hessenberg法求特征值
- MySQL day()函数
- 电单车中的N车模长得啥样呢?
- 如何判断Hadoop集群是否搭建成功
- Microsoft project 2016 使用出现的bug
- pip安装tensorflow报错:could not find a version that satisfies the requirement grpcio=1.8.6
- 闭关修炼(二十五)基础web安全