Github每日精选(第74期):使用 Cobra 创建 golang 命令行 应用

Cobra

在命令行的程序中,是服务端最经常使用的,因为他方便,而且效率高。为了提高命令行程序的开发速度,golang中有一个框架是Cobra

我们一起来看看Cobra 是怎么简化我们开发控制行命令的。

github 的地址在这里

在这里插入图片描述

Cobra 的安装

Cobra 使用的人特别的多,github 的很多工具都是用Cobra 开发的,看了下文档,用起来确实非常的简单。

安装非常的简单:

go install github.com/spf13/cobra-cli@latest

这时候我们就可以使用cobra-cli 命令了。

D:\work\GolangProjects>cobra-cli
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.
实例

还是从helloworld上开始,用框架创建helloworld 应用:

#创建目录
mkdir helloapp
cd helloapp

#创建mod文件
go mod init helloapp

#初始化
cobra-cli init

初始化完了以后,他就帮我们生成了如下的文件:

│  go.mod
│  go.sum
│  LICENSE
│  main.go
│
└─cmd
        root.go

运行命令,看看输出:

D:\work\GolangProjects\helloapp>go run main.go
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.

打开 root.go 就可以看到这一句话。

添加我们的命令:

D:\work\GolangProjects\helloapp>cobra-cli add hello
hello created at D:\work\GolangProjects\helloapp

运行下:

D:\work\GolangProjects\helloapp>go run main.go

出现

hello call

这就是我们使用框架的好处,很容易就构建一个命令参数。

添加如下的代码:

func inHello(args []string) {
	
	helloto := "" 
	
	for _, ival := range args {
		helloto = helloto + ival
		
	}
	fmt.Printf("Welcome to app, %s \n", helloto )
}

helloCmd 变为如下:

// helloCmd represents the hello command
var helloCmd = &cobra.Command{
	Use:   "hello",
	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) {
		inHello(args)
	},
}

运行:

D:\work\GolangProjects\helloapp>go run main.go hello pp io
Welcome to app, ppio

我们获取了各个参数,组合起来做事情。

这个就是Cobra的使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

go2coding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值