• 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$ 
    
    
  • 相关阅读:
    JS 数组的各个方法汇总
    flutter入门-MaterialApp详解
    编程语言的选择
    创建线程池
    python模块报错:‘No module named “Crypto“ ‘
    基于大规模测量和多任务深度学习的电子鼻系统目标识别、浓度预测和状态判断
    K8S一个Yaml执行多个任务
    java计算机毕业设计婚纱摄影网站(附源码、数据库)
    Java的基础知识
    uartus使用vwf仿真simulation里没有opting选项选择不了使用自带仿真
  • 原文地址:https://blog.csdn.net/2301_79691134/article/details/135940764