Go之Cobra的命令行应用开发使用

 

环境搭建请参照如下博客:

   https://blog.csdn.net/gcglhd/article/details/109572324

具体的开发设计使用:

首先根据上面的步骤已经搭建完成相关的cobra环境,下面就是具体的设计:

1.  函数执行的流程,启动mian.go会自动的触发cmd包下的root.go文件。root.go文件就是主要的相关启动说明,里面涉及到功能点进行说明:

主要的功能点就是3个部分a ,b,  c。

2. 我们添加一个子命令功能(实现参数的值累加求和)

首先,采用cobra add sub添加子命令到项目中。

E:\GITwork\ginWork\src\cobraTest>cobra add sub
sub created at E:\GITwork\ginWork\src\cobraTest

下面就是自动生成的部分:

 

我们会发现sub文件的init()方法中默认已经添加到了rootCmd主命令中。下面我们具体进行编写subCmd的Run函数部分。

/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
	"fmt"
	"strconv"

	"github.com/spf13/cobra"
)

// subCmd represents the sub command
var subCmd = &cobra.Command{
	Use:   "sub",
	Short: "求和操作",
	Long:  `本命令是实现参数的求和计算`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("开启求和模式")
		sub := 0
		for k, e := range args {
			fmt.Println("第", k, "个变量:", e)
			v, err := strconv.Atoi(e)
			if err != nil {
				return
			}
			sub += v
		}
		fmt.Println("参数的求和为:", sub)
	},
}

func init() {
	rootCmd.AddCommand(subCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// subCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// subCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

最后测试命令: 

go build 首先就是编译cobraTest文件生成可执行文件。

go run cobraTest -h : 运行只执行文件查看命令文档说明。
 

E:\GITwork\ginWork\src\cobraTest>go run cobraTest -h
 主程序命令

Usage:
  cobraTest [command]

Available Commands:
  help        Help about any command
  sub         求和操作

Flags:
      --config string   config file (default is $HOME/.cobraTest.yaml)
  -h, --help            help for cobraTest
  -t, --toggle          Help message for toggle

Use "cobraTest [command] --help" for more information about a command.

go run cobraTest sub 1 2 3 4 5 6 :执行求和计算1-6的和

E:\GITwork\ginWork\src\cobraTest>go run cobraTest sub 1 2 3 4 5 6
开启求和模式
第 0 个变量: 1
第 1 个变量: 2
第 2 个变量: 3
第 3 个变量: 4
第 4 个变量: 5
第 5 个变量: 6
参数的求和为: 21

3. 获取命令参数中的配置文件路径:(该方式也是项目中会用到的,使用通过命令行启动的形式,来获取具体的配置文件路径然后结合Viper相关操作获取指定路径下的配置文件信息)

参考:    https://blog.csdn.net/gcglhd/article/details/109622447

具体的代码部分: 

/**
 * @Author gcg
 * @Description: 功能描述(查看配置的信息)
 * @Date : 2020/12/2
 */
var (
	configYml string
	mode      string
	StartCmd  = &cobra.Command{
		Use:     "config",
		Short:   "Get Application config info",
		Example: "可执行文件名 config -c config/settings.yml",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
	}
)

/**
 * @Author gcg
 * @Description: 功能描述()
 * @Date : 2020/12/2
 */
func init() {
	// 该方法就是实现value路径值写进引用类型configYml中。此处的value路径是默认值,只是一个提醒功能,不会真正的写进命令中
	StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
}

func run() {
	fmt.Println("configYml: ", configYml)   //这里获取的就是具体的配置文件的路径。
	fmt.Println("---------configYml--------- ", configYml)
}

具体的命令如下:

可执行文件名  config -c config/settings.yml 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值