基于cobra的go语言命令行解析器
ubuntu安装cobra
$ sudo apt install cobra
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:cobra
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,873 kB of archives.
After this operation, 9,495 kB of additional disk space will be used.
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/universe amd64 cobra amd64 0.0.5-1 [2,873 kB]
Fetched 2,873 kB in 2s (1,272 kB/s)
Selecting previously unselected package cobra.
(Reading database ... 188150 files and directories currently installed.)
Preparing to unpack .../cobra_0.0.5-1_amd64.deb ...
Unpacking cobra (0.0.5-1) ...
Setting up cobra (0.0.5-1) ...
Processing triggers for man-db (2.9.1-1) ...
Go在项目中使用cobra
import "github.com/spf13/cobra/cobra"
使用cobra创建项目
创建项目建议使用Go module
模式,首先,创建一个go module
项目,项目名称为arsenal
。
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# go mod init arsenal
go: creating new go.mod: module arsenal
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ll
total 12
drwxr-xr-x 2 root root 4096 11月 18 21:25 ./
drwxr-xr-x 6 644 root 4096 11月 18 21:25 ../
-rw-r--r-- 1 root root 24 11月 18 21:25 go.mod
在arsenal
目录中添加cobra
项目。
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# cobra init --pkg-name arsenal
Your Cobra applicaton is ready at
/home/curtis/go_env/arsenalroot@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# tree .
.
├── cmd
│ └── root.go # 最顶层的命令rootCmd
├── go.mod
├── LICENSE # license信息
└── main.go
编译之前下载几个依赖包
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# go build
cmd/root.go:24:3: no required module provides package github.com/mitchellh/go-homedir; to add it:go get github.com/mitchellh/go-homedir
cmd/root.go:22:3: no required module provides package github.com/spf13/cobra; to add it:go get github.com/spf13/cobra
cmd/root.go:25:3: no required module provides package github.com/spf13/viper; to add it:go get github.com/spf13/viper# 执行生成的可执行文件
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example: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.
编译出来的二进制文件arsenal
默认打印的信息是怎么来的?来自rootCmd
命令的长描述信息。
// cmd/root.go
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{Use: "arsenal",Short: "A brief description of your application",Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example: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.`,// Uncomment the following line if your bare application// has an action associated with it:// Run: func(cmd *cobra.Command, args []string) { },
}
尝试修改rootCmd
命令的描述信息。
var rootCmd = &cobra.Command{Use: "arsenal",Short: "A brief description of your application",Long: "This is long description of your application", // 出现在-h的帮助信息中// Uncomment the following line if your bare application// has an action associated with it:// Run: func(cmd *cobra.Command, args []string) { },
}
执行命令
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal
This is long description of your application
添加子命令,比如说添加一个版本信息
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# cobra add version
version created at /home/curtis/go_env/arsenal
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# tree .
.
├── arsenal
├── cmd
│ ├── root.go
│ └── version.go # 新添加了一个versoin.go
├── go.mod
├── go.sum
├── LICENSE
└── main.go# go build编译之后执行命令
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal
This is long description of your applicationUsage:arsenal [command]Available Commands:completion Generate the autocompletion script for the specified shellhelp Help about any command # 添加的子命令version This short information about version # 可以看到version子命令的短描述信息Flags:--config string config file (default is $HOME/.arsenal.yaml)-h, --help help for arsenal-t, --toggle Help message for toggleUse "arsenal [command] --help" for more information about a command.# 执行了注册的Run 函数,该函数打印了信息version called
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal version
version called# -h 信息可以看到version 命令的长信息
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal version -h
This long information about versionUsage:arsenal version [flags]Flags:-h, --help help for versionGlobal Flags:--config string config file (default is $HOME/.arsenal.yaml)
如何把命令绑定到父命令中,可以无限制的往下添加,但是需要注意的是每个cobra.command
结构体只能指向一个parent cobra.command
。
# 不做任何处理
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# cobra add cpu
cpu created at /home/curtis/go_env/arsenalroot@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal#
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# tree .
.
├── arsenal
├── cmd
│ ├── cpu.go
│ ├── root.go
│ └── version.go
├── go.mod
├── go.sum
├── LICENSE
└── main.go1 directory, 8 files
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# go build
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal
This is long description of your applicationUsage:arsenal [command]Available Commands:completion Generate the autocompletion script for the specified shellcpu A brief description of your commandhelp Help about any commandversion This short information about versionFlags:--config string config file (default is $HOME/.arsenal.yaml)-h, --help help for arsenal-t, --toggle Help message for toggleUse "arsenal [command] --help" for more information about a command.
添加子命令
将上述cpu
子命令添加到version
下修改之后。
// 只需要将cpuCmd添加到versionCmd之下就可以了。
func init() {// 使用cobra add默认将命令添加到rootCmd// 可以尝试修改添加到version cmd之下versionCmd.AddCommand(cpuCmd)// Here you will define your flags and configuration settings.// Cobra supports Persistent Flags which will work for this command// and all subcommands, e.g.:// cpuCmd.PersistentFlags().String("foo", "", "A help for foo")// Cobra supports local flags which will only run when this command// is called directly, e.g.:// cpuCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal version -h
This long information about versionUsage:arsenal version [flags]arsenal version [command]Available Commands:cpu A brief description of your command # 子命令添加成功Flags:-h, --help help for versionGlobal Flags:--config string config file (default is $HOME/.arsenal.yaml)Use "arsenal version [command] --help" for more information about a command.
选项的接收与处理
var versionCmd = &cobra.Command{Use: "version",Short: "This short information about version",Long: "This long information about version",Run: func(cmd *cobra.Command, args []string) {fmt.Println("version called")// 各个选项参数获取auther, err := cmd.Flags().GetString("auther")if err != nil {fmt.Println("请输入正确的作者信息")return}fmt.Println("作者是: ", auther)},
}func init() {rootCmd.AddCommand(versionCmd)// Here you will define your flags and configuration settings.// Cobra supports Persistent Flags which will work for this command// and all subcommands, e.g.:// versionCmd.PersistentFlags().String("foo", "", "A help for foo")// Cobra supports local flags which will only run when this command// is called directly, e.g.:// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")// 添加version参数auther,a为别名,默认值为cutisversionCmd.Flags().StringP("auther", "a", "curtis", "The auther name")// 添加version参数percent,p为别名,默认值为60%versionCmd.Flags().StringP("percent", "p", "60%", "The percent of cpu")
}
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal version -h
This long information about versionUsage:arsenal version [flags]arsenal version [command]Available Commands:cpu A brief description of your commandFlags:-a, --auther string The auther name (default "curtis")-h, --help help for version-p, --percent string The percent of cpu (default "60%")Global Flags:--config string config file (default is $HOME/.arsenal.yaml)Use "arsenal version [command] --help" for more information about a command.
选项常见的类型
- 布尔值
- 数字(各种整形,浮点数等)
- 字符串
- 其他高级类型
选项的帮助信息
命令自动补全补全功能
从上面构建的工程来看,cobra
已经把自动补全的功能默认添加到项目中,使用方法如下所示。
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal completion bash -h
Generate the autocompletion script for the bash shell.This script depends on the 'bash-completion' package.
If it is not installed already, you can install it via your OS's package manager.To load completions in your current shell session:source <(arsenal completion bash) # 只有在当前shell终端生效To load completions for every new session, execute once:#### Linux:arsenal completion bash > /etc/bash_completion.d/arsenal # 永久生效#### macOS:arsenal completion bash > $(brew --prefix)/etc/bash_completion.d/arsenal# 需要重新开启新的终端才会生效
You will need to start a new shell for this setup to take effect.Usage:arsenal completion bashFlags:-h, --help help for bash--no-descriptions disable completion descriptionsGlobal Flags:--config string config file (default is $HOME/.arsenal.yaml)
启用自动补全功能之后,使用tab命令没有自动补全,报错如下。
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# source <(./arsenal completion bash)
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal ver_get_comp_words_by_ref: command not found
_get_comp_words_by_ref: command not found
_get_comp_words_by_ref: command not found
可能原因是有可能没有安装bash-completion
包,安装方法即开启命令自动补全如下所示。
# 安装bash-completion包
$ sudo apt install bash-completion# 启用completion功能
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# source /usr/share/bash-completion/bash_completion
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# source <(./arsenal completion bash)# 可以使用tab键自动补全命令
root@curtis-Aspire-E5-471G:/home/curtis/go_env/arsenal# ./arsenal version cpu
自定义配置文件中读取cli命令行相关参数
常见的配置文件类型有json,yaml
从上述注册的命令来看,还有一个可选项,那就是从配置文件中读取cli命令行相关参数。用cobra命令初始化的模块默认的文件格式为yaml。
Global Flags:--config string config file (default is $HOME/.arsenal.yaml)
获取命令行中的所有flags
// 返回指向Flag结构体的指针f
func checkFlags(f *pflag.Flag) {fmt.Println(f.Name)
}// 访问命令行中已经输入的flags
func main(){flagset.Visit(checkFlags);
}// 访问所有定义的flags
func main(){flagset.VisitAll(checkFlags);
}
示例代码:
func checkFlags(f *pflag.Flag) {fmt.Println(f.Name)
}// cpuCmd represents the cpu command
var cpuCmd = &cobra.Command{Use: "cpu",Short: "A brief description of your command",Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example: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.`,Run: func(cmd *cobra.Command, args []string) {fmt.Println("cpu called")// flags := cmd.Flags()// fmt.Println(flags)cmd.Flags().Visit(checkFlags)},
}
成功解析当前命令行输入的所有flags,想要获取flags所对应的值,cmd.Flags()
下有对应的方法。
curtis@curtis-Aspire-E5-471G:~/go_env/arsenal$ ./arsenal version cpu -p ./ll -a curtis
cpu called
author
file-name
获取某个command的帮助信息
func getUsageInfo(cmd *cobra.command, args []string) {usageInfo := cmd.Flags().FlagUsages()fmt.Println(usageInfo)
}
获取Flagset的所有信息
flagInfo := cmd.Flags()
fmt.Println(flagInfo )
代码实例
// cpuCmd represents the cpu command
var cpuCmd = &cobra.Command{Use: "cpu",Short: "A brief description of your command",Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example: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.`,Run: func(cmd *cobra.Command, args []string) {fmt.Println("cpu called")flags := cmd.Flags()fmt.Println(flags)},
}
打印信息如下,为结构体FlagSet
所对应的详细信息。
&{<nil> true {false} cpu true map[author:0xc0000bbae0 file-name:0xc0000bba40] [0xc0000bba40 0xc0000bbae0] [] map[author:0xc0000bbae0 config:0xc0000bbb80 file-name:0xc0000bba40 help:0xc0001380a0] [0xc0000bba40 0xc0000bbae0 0xc0000bbb80 0xc0001380a0] [0xc0000bbae0 0xc0000bbb80 0xc0000bba40 0xc0001380a0] map[97:0xc0000bbae0 104:0xc0001380a0 112:0xc0000bba40] [] -1 0 0xc00009b800 true <nil> []}
基于cobra的go语言命令行解析器相关推荐
- Go语言 命令行解析(二)
今天我们继续讲解Go语言中命令行,当我们在解析命令行传递的参数时通常会想用最简单的方法来解析自己行用到的命令行参数,那么urfave/cli可以帮助我们快速的解析命令行参数,它是一个简单快速的命令行包 ...
- Go语言 命令行解析(一)
命令行启动服务的方式,在后端使用非常广泛,如果有写过C语言的同学相信不难理解这一点!在C语言中,我们可以根据argc和argv来获取和解析命令行的参数,从而通过不同的参数调取不同的方法,同时也可以用U ...
- .NET:命令行解析器介绍
背景 经常需要开发一下小工具,之前都是自己解析命令行参数,接触过动态语言社区以后,发现命令行解析有特定的模式和框架可以利用,本文介绍一个 .NET 平台的类库. 示例 需求 拷贝文件,如:CopyFi ...
- [Python] argparse命令行解析器的使用
参考了python文档argparse- 用于命令行选项,参数和子命令的解析器和一篇非常非常优秀的博客python命令行解析模块argpars. 该argparse模块可以轻松编写用户友好的命令行界面 ...
- Python 命令行解析器argparse及传参数详解
源码实例一 from argparse import ArgumentParserparser = ArgumentParser(description='Beeswarm')group = pars ...
- IConfiguration的命令行解析
目录 介绍 背景 使用代码 支持哪些操作系统? 您可以绑定哪些类型的属性? 绑定到集合 嵌套属性和公共无参数构造函数 提供帮助 日志记录 兴趣点 介绍 有许多可用于.NET的命令行解析器,其中一些可与 ...
- 一种命令行解析的新思路(Go 语言描述)
简介: 本文通过打破大家对命令行的固有印象,对命令行的概念解构后重新梳理,开发出一种功能强大但使用极为简单的命令行解析方法.这种方法支持任意多的子命令,支持可选和必选参数,对可选参数可提供默认值,支持 ...
- Python命令行解析:IDE内点击Run运行代码直接得出结果、基于TF flags(或argparse、sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果
Python命令行解析:IDE内点击Run运行代码直接得出结果.基于TF flags(或argparse.sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果 目录 命令行解析 T1.采 ...
- 【Rust】argh:基于 derive 宏且对二进制体积进行优化的命令行解析工具
Derive-based argument parsing optimized for code size and conformance to the Fuchsia commandline too ...
最新文章
- soalris小記...
- jtable如何从表格中定位_Java Swing组件编程之JTable表格用法实例详解
- unity3d显示c4d材质_C4D小白最常踩的9个坑,看看你中招了没?
- docker报错:Unable to get pid of LinuxThreads manager thread及openjdk-alpine镜像无法打印线程堆栈和内存堆栈问题
- python中字符串之间的比较_如何利用python来对比两个字符串的差异
- 我们是否应该保留同时多线程?
- Linux_快速查找文件
- stm32GPIO8种模式
- android开机动画多长时间_ANDROID开机动画分析
- 「面试必背」Linux面试题(2022最新版)
- C语言控制台美化教程
- 黄杏元《地理信息系统概论》考研复习考点精讲(二)
- 深度linux已连网但无法访问互联网,wifi已连接但无法访问互联网怎么办?
- java5兼容性,兼容性问题 (适用于 UNIX 的 Sun Java Enterprise System 5 发行说明)
- vue vue-print-nb 打印图片
- Mind思维导图-在线
- 福建省2021高考成绩如何查询,2021福建省地区高考成绩排名查询,福建省高考各高中成绩喜报榜单...
- IT十年人生过客-二十一-忙碌和非凡的一年(上)
- 李俊计算机哈佛大学,李俊教授个人主页
- 新冠疫情可视化(7月9日,7月10日)