• 18.cobra框架了解


    概述

    github cobra

    cobra 快速的实现一个命令行客户端,命令行解析工具。

    cobra 中的主要概念

    • -Commands 表示执行运作
    • -Args 执行参数
    • -Flags 这些运作的标识符

    举例

    • git clone 命令

    git clone https://github.com/spf13/cobra.git --bare

    • git 代表执行的二进制
    • clone 代表执行的运作,可以理解为命令
    • https://github.com/spf13/cobra.git 代表参数
    • –bare 代表标识符,意思是创建个祼库

    安装

    # 前置条件
    go env -w GO111MODULE=on
    # 检查
    go env 
    
    appledeMacBook-Pro:cobra hyl$ go install github.com/spf13/cobra-cli@latest
    go: downloading github.com/spf13/cobra-cli v1.3.0
    go: downloading github.com/spf13/cobra v1.3.0
    go: downloading github.com/spf13/viper v1.10.1
    go: downloading github.com/spf13/jwalterweatherman v1.1.0
    go: downloading github.com/spf13/afero v1.6.0
    go: downloading github.com/spf13/cast v1.4.1
    go: downloading github.com/magiconair/properties v1.8.5
    go: downloading github.com/mitchellh/mapstructure v1.4.3
    go: downloading github.com/fsnotify/fsnotify v1.5.1
    go: downloading github.com/subosito/gotenv v1.2.0
    go: downloading gopkg.in/ini.v1 v1.66.2
    go: downloading github.com/hashicorp/hcl v1.0.0
    go: downloading github.com/pelletier/go-toml v1.9.4
    go: downloading golang.org/x/text v0.3.7
    go: downloading golang.org/x/sys v0.0.0-20211210111614-af8b64212486
    
    appledeMacBook-Pro:bin hyl$ ./cobra-cli -h
    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.
    appledeMacBook-Pro:bin hyl$ pwd
    /Users/hyl/Desktop/jk/go/bin
    

    /Users/hyl/Desktop/jk/go/bin/cobra-cli

    在这里插入图片描述
    在这里插入图片描述

    实践

    bash: no job control in this shell
    appledeMacBook-Pro:cobra-demo hyl$ go get -u github.com/spf13/cobra@latest
    go: can only use path@version syntax with 'go get' and 'go install' in module-aware mode
    appledeMacBook-Pro:cobra-demo hyl$ go mod init
    go: creating new go.mod: module cobra-demo
    appledeMacBook-Pro:cobra-demo hyl$ go mod tidy
    go: warning: "all" matched no packages
    appledeMacBook-Pro:cobra-demo hyl$ 
    appledeMacBook-Pro:cobra-demo hyl$ go get -u github.com/spf13/cobra@latest
    go: added github.com/inconshreveable/mousetrap v1.1.0
    go: added github.com/spf13/cobra v1.8.0
    go: added github.com/spf13/pflag v1.0.5
    

    官网文档

    实践

    官网文档

    appledeMacBook-Pro:src hyl$ mkdir cobra
    appledeMacBook-Pro:src hyl$ ls
    cobra		github.com	k8s.io		test
    appledeMacBook-Pro:src hyl$ cd cobra/
    
    appledeMacBook-Pro:cobra hyl$ go run "/Users/hyl/Desktop/jk/go/src/cobra/main.go"
    测试
    appledeMacBook-Pro:cobra hyl$ go mod tidy
    
    # 结合官网,组合成代码,按错误提示,引入类
    
    
    appledeMacBook-Pro:cobra hyl$ go run main.go times
    测试
    Error: requires at least 1 arg(s), only received 0
    Usage:
      exe times [flags]
    
    Flags:
      -h, --help        help for times
      -t, --times int   times to echo the input (default 1)
    
    requires at least 1 arg(s), only received 0
    exit status 1
    appledeMacBook-Pro:cobra hyl$ 
    
    appledeMacBook-Pro:cobra hyl$ go run main.go times -t=4 k8s
    测试
    times call...
    echo:k8s
    echo:k8s
    echo:k8s
    echo:k8s
    appledeMacBook-Pro:cobra hyl$ go run main.go times  k8s
    测试
    times call...
    echo:k8s
    appledeMacBook-Pro:cobra hyl$ 
    
    
  • 相关阅读:
    word怎么转pdf?word转pdf借助pdf软件即可搞定!
    【MQTT】Javascript通过WebSocket连接MQTT
    在java开发工具IntelliJ IDEA中如何与远程 Git 存储库同步?
    DDD战略设计--如何设计聚合(含示例代码)
    1024程序员节主题征文 | 35种语言输出1024
    Java实战:指定大小分组压缩文件夹里面的文件案例
    Dubbo学习记录(六)--Spring整合Dubbo中的Dubbo配置文件流程解析(二)
    leetcode-每日一题-自定义字符串排序-791(中等,字符串应用)
    华为云云耀云服务器L实例评测|搭建CounterStrike Source Delicated Server(CS起源游戏服务器)
    kubernetes集群yaml文件与kubectl工具
  • 原文地址:https://blog.csdn.net/2301_79691134/article/details/135940764