# cobra **Repository Path**: bmchris0200/cobra ## Basic Information - **Project Name**: cobra - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Readme ## 概述 命令行实用程序并不是都象 cat、more、grep 是简单命令。[go](https://go-zh.org/cmd/go/) 项目管理程序,类似 java 项目管理 [maven](http://maven.apache.org/)、Nodejs 项目管理程序 [npm](https://www.npmjs.com/)、git 命令行客户端、 docker 与 kubernetes 容器管理工具等等都是采用了较复杂的命令行。即一个实用程序同时支持多个子命令,每个子命令有各自独立的参数,命令之间可能存在共享的代码或逻辑,同时随着产品的发展,这些命令可能发生功能变化、添加新命令等。因此,符合 [OCP 原则](https://en.wikipedia.org/wiki/Open/closed_principle) 的设计是至关重要的编程需求。 ## 使用 首先下载cobra包 `go get gitee.com/bmchris0200/cobra` 然后创建一个文件夹test,文件结构如下: ``` ▾ test/ ▾ cmd/ root.go version.go subcmd1.go subcmd2.go main.go ``` main.go用于调用执行根命令root的函数,其代码如下 ```go package main import (cmd "mycobra/cmd") func main(){ cmd.Execute(); } ``` root.go用于声明根命令,代码如下: ```go package cmd import( cobra "cobra" ) var rootCmd = &cobra.Command{ Use: "Cobra", Short: "Cobra is a very fast static site generator", Long: `A Fast and Flexible Static Site Generator built with love by spf13 and friends in Go. Complete documentation is available at http://hugo.spf13.com`, Run: func(cmd *cobra.Command, args []string) { // Do Stuff Here }, } func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } ``` version.go可以输入版本 ```go package cmd import ( "fmt" cobra "cobra" ) func init() { rootCmd.AddCommand(versionCmd) } var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of Cobra", Long: `All software has versions. This is Cobra's`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Cobra version is v1.0.0") }, } ``` subcmd1.go和subcmd2.go代码如下: ```go package cmd import ( "fmt" cobra "gitee.com/bmchris0200/cobra" ) var subcmd1 = &cobra.Command{ Use: "subcmd1", Short: "subcmd1 is a subcommand of cobra", Long: "subcmd1 is a subcommand of cobra", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("subcmd1 is called, arguments are %v\n", args) }, } func init() { rootCmd.AddCommand(subcmd1) } ``` ```go package cmd import ( "fmt" cobra "gitee.com/bmchris0200/cobra" ) var sub2Cmd = &cobra.Command{ Use: "subcmd2", Short: "subcmd2 is the subcommand of subcmd1", Long: "subcmd2 is the subcommand of subcmd1", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("subcmd2 is called, arguments are %v\n", args) }, } func init() { subCmd.AddCommand(sub2cmd) } ``` 接下来要安装cobra `go install cobra` 接着在终端输入命令检测 1. 测试version命令 `mycobra version` ![2](./img/2.png) 2. 测试subcmd1 `mycobra subcmd1` ![2](./img/3.png) 3. 测试subcmd2 `mycobra subcmd1 subcmd2` ![2](./img/4.png) 4. 测试subcmd2带参数 `mycobra subcmd2 -a -b` ![5](./img/5.png) 5. 测试help ![5](./img/6.png) 6. 测试help version ![5](./img/7.png) -b`