Cobra是前go语言负责人spf13开发的一款go-cli框架,可以轻松规范的处理cli应用场景。
项目地址:https://github.com/spf13/cobra
作者博客: https://spf13.com/
所谓官网:https://cobra.dev/ https://unsplash.com/t/people 图片网站
我们以go的命令运行run做例子
Command 命令
SubCommand 子命令
Argument 参数
Flag 标记参数
go run main.go --config=app.conf
Command:
go
SubCommand # run是go的子命令 也就是run 是go(Command)的子命令run(SubCommand)
run
Argument #参数一般值一个预设目标
main.go
Flag: #标记参数一般以kv形式
--config=app.conf
$GOPATH/bin
目下生成一个可执行文件,将其配置在系统的环境变量$PATH
中保证在shell下可以执行即可安装完成。go install github.com/spf13/cobra-cli@latest
go get -u github.com/spf13/cobra@latest
随着go模块的引入,Cobra生成器已被简化以利用模块。Cobra生成器在Go模块中工作。
#进入有go.mod的项目根目录下执行
cobra-cli init --help
cobra-cli init --author "pro911 <pro911@qq.com>" --license MIT
#1.创建一个应用目录
mkdir -p d:/wwwroot/issue-cli
#2.进入应用目录
cd d:/wwwroot/issue-cli
#3.新建一个go.mod文件
go init mod issue-cli
#4.1初始化cobra项目
cobra-cli init --author "pro911 <pro911@qq.com>" --license MIT --viper //指定作者信息 指定开源 指定使用viper自动加载viper包
#4.2初始化cobra项目
cobra-cli init --config ./etc/cobra.yaml //内容如下
#4.2.1cobra.yaml内容 key不区分大小写。
author: pro911x <pro911@qq.com>
LICENSE: apache
useViper: true
cobra-cli add version
执行后会在项目的cmd目录下生成一个名称为version.go的文件,该命令的执行文件。
/*
Copyright © 2022 pro911 <pro911@qq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// versionCmd represents the demo command
var versionCmd = &cobra.Command{
Use: "version", //这个是命令的参数 go run main.go version 而不是文件名本身 demo.go
Short: "版本信息",
Long: `关于版本的长信息:
他是可以执行一些参数描述和换行进行描述。
描述结束。`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("version called")
},
}
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.:
// demoCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// demoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}