Cobra命令行框架及使用

  • cobra 快速教程
    • Cobra简介
    • Cobra安装
    • Cobra项目创建
    • 修改作者信息
    • 命令的基本结构
    • 命令编译与运行
    • 为命令添加一个子命令
    • 为命令添加一个选项
    • 选项的接收与处理
    • 选项的常见类型
    • 选项的帮助信息
    • 课程总结
  • Cobra框架源码分析
    • 命令行基础概念
    • cobra子命令
    • cobra中command常用属性
    • cobra的运行流程
    • Cobra源码实现
    • 相关信息
  • 最后实战
    • 目录架构
    • 各文件代码

cobra 快速教程

Cobra简介

  • 一个用于生成命令行工具的框架(本身也是个命令行工具)
  • 简单易用
  • Kubernetes、Etcd等等都是用它来做的。

Cobra安装

go get -u github.com/spf13/cobra/cobra

确保cobra被添加到PATH环境变量中。
get -u是updata的意思。
链接的最后如果没有加上cobra的话,是不会下载exe文件的也就是说用不了cobra这个exe软件。下载之后就会在go的path路径里面的bin文件生成一个exe文件。

cobra -h
在项目中引入Cobra的方式(不建议手动引入):
import “github.com/spf13/cobra”

Cobra项目创建

建议使用GO Module模式。首先,创建一个Go Module的项目。名称采用clid(下同)。
mkdir clid
go mod init clid
然后再clid目录中创建cobra项目。
cd clid
cobra init --pkg-name clid
即可生成。

修改作者信息

上面默认生成的Go文件中,作者信息位置为默认占位符。手动修改比较麻烦,所以建议一开始初始化的时候添加–author选项。
cobra init --pkg-name clid --athor kernelTea -l MIT
也可以指定License。

命令的基本结构

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ //根命令。Use:   "cobra-demo",Short: "A brief description of your application",Long: `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.`,// Uncomment the following line if your bare application// has an action associated with it:Run: func(cmd *cobra.Command, args []string) { }, //实现功能逻辑的函数。Alias: []string{}, // 别名,命令的别名。
}

命令编译与运行

go语言编译命令

cd clid
go build

go语言运行命令

./clid

为命令添加一个子命令

cobra add test

添加一个名叫test的子命令。

说明:
go version ,其中version就是子命令。

package cmdimport ("fmt""github.com/spf13/cobra"
)// testCmd represents the test command
var testCmd = &cobra.Command{Use:   "test",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) {fmt.Println("test called")},
}func init() {rootCmd.AddCommand(testCmd)// Here you will define your flags and configuration settings.// Cobra supports Persistent Flags which will work for this command// and all subcommands, e.g.:// testCmd.PersistentFlags().String("foo", "", "A help for foo")// Cobra supports local flags which will only run when this command// is called directly, e.g.:// testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

讲解:

  1. 命令的基本结构
  2. 如何将命令绑定到父级命令上去 (看子命令的init函数,ADDCommand是绑定关系的方法)

编译运行可看到test子命令。

为命令添加一个选项

func init(){rootCmd.AddCommand(testCmd)testCmd.Flags().Boolp("verbose","v",false,"是否显示测试详情")
}

实例中添加了一个verbose选项,用以控制是否显示测试详情。

在init初始化中,有一些注释关于这个选项的说明,其中,Flags()方法里面有很多选项类型,如字符串、ip地址等等。

  • 为子命令再添加子命令
func init(){cobra.OnInitialize(initConfig)……省略其中代码cmd1.AddCommand(cmd11, cmd12)rootCmd.AddCommand(cmd1,cmd2)
}

编译运行效果。

选项的接收与处理

var testcmd = &cobra.Command{Use: "test",Short: "短的说明",Run: func(cmd *cobra.Command, args []string){verbose, err := cmd.Flags().GetBool("verbose")if err != nil {fmt.Println("获取命令选项出错")return}fmt.Printf("获取到命令选项值:%t\n",verbose)},
}

选项的常见类型

  • 布尔值
  • 数字(各类整数、浮点数等等)
  • 字符串
  • 其它高级类型 如IP地址,掩码等等

选项的帮助信息

// SetHelpFunc sets help function. Can be defined by Application.
func (c *Command) SetHelpFunc(f func(*Command, []string)){c.helpFunc = f
}// SetHlpCommand sets help command.
func (c *Command) SetHelpCommand(cmd *Command) {c.helpCommand = cmd
}// SetHelpTemplate sets help template to be used. Application can use it to set custom template.
func(c *Command) SetHelpTemplate(s string) {c.helpTemplate = s
}

help命令的帮助信息:

func (c *Command) InitDefaultHelpCmd(){if !c.HasSubCommands(){return}if c.helpCommand == nil{c.helpCommand = &Command{Use: "help [command]",Short: "Help about any command",Long: `Help provides help for any command in the application.Simply type ` + c.Name() + `help [path to command] for full details.`,Run: func(c *Command, args []string){cmd, _, e := c.Root().Find(args)if cmd == nil || e !=nil {c.Printf("Unknown help topic %#q\n", args)c.Root().Usage()} else {cmd.InitDefaultHelpFlag() // make possible 'help' flag to be showncmd.Help()}},}}c.RemoveCommand(c.helpCommand)c.AddCommand(c.helpCommand)
}
---Short: "Help about " + c.Name(),
---

课程总结

  • 简介
  • 安装、创建项目、修改作者信息
  • 命令、子命令、选项
  • 帮助信息

Cobra框架源码分析

命令行基础概念

go      Command 命令
go run main.go      Argument
go run main.go --config=app.conf    Flag? SubCommand?

cobra子命令

#mermaid-svg-NiT3vVb7r1YxYhqe .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .label text{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .node rect,#mermaid-svg-NiT3vVb7r1YxYhqe .node circle,#mermaid-svg-NiT3vVb7r1YxYhqe .node ellipse,#mermaid-svg-NiT3vVb7r1YxYhqe .node polygon,#mermaid-svg-NiT3vVb7r1YxYhqe .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-NiT3vVb7r1YxYhqe .node .label{text-align:center;fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .node.clickable{cursor:pointer}#mermaid-svg-NiT3vVb7r1YxYhqe .arrowheadPath{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-NiT3vVb7r1YxYhqe .flowchart-link{stroke:#333;fill:none}#mermaid-svg-NiT3vVb7r1YxYhqe .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-NiT3vVb7r1YxYhqe .edgeLabel rect{opacity:0.9}#mermaid-svg-NiT3vVb7r1YxYhqe .edgeLabel span{color:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-NiT3vVb7r1YxYhqe .cluster text{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-NiT3vVb7r1YxYhqe .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-NiT3vVb7r1YxYhqe text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-NiT3vVb7r1YxYhqe .actor-line{stroke:grey}#mermaid-svg-NiT3vVb7r1YxYhqe .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-NiT3vVb7r1YxYhqe #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .sequenceNumber{fill:#fff}#mermaid-svg-NiT3vVb7r1YxYhqe #sequencenumber{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe #crosshead path{fill:#333;stroke:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .messageText{fill:#333;stroke:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-NiT3vVb7r1YxYhqe .labelText,#mermaid-svg-NiT3vVb7r1YxYhqe .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-NiT3vVb7r1YxYhqe .loopText,#mermaid-svg-NiT3vVb7r1YxYhqe .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-NiT3vVb7r1YxYhqe .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-NiT3vVb7r1YxYhqe .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-NiT3vVb7r1YxYhqe .noteText,#mermaid-svg-NiT3vVb7r1YxYhqe .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-NiT3vVb7r1YxYhqe .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-NiT3vVb7r1YxYhqe .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-NiT3vVb7r1YxYhqe .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-NiT3vVb7r1YxYhqe .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .section{stroke:none;opacity:0.2}#mermaid-svg-NiT3vVb7r1YxYhqe .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-NiT3vVb7r1YxYhqe .section2{fill:#fff400}#mermaid-svg-NiT3vVb7r1YxYhqe .section1,#mermaid-svg-NiT3vVb7r1YxYhqe .section3{fill:#fff;opacity:0.2}#mermaid-svg-NiT3vVb7r1YxYhqe .sectionTitle0{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .sectionTitle1{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .sectionTitle2{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .sectionTitle3{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-NiT3vVb7r1YxYhqe .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .grid path{stroke-width:0}#mermaid-svg-NiT3vVb7r1YxYhqe .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-NiT3vVb7r1YxYhqe .task{stroke-width:2}#mermaid-svg-NiT3vVb7r1YxYhqe .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .taskText:not([font-size]){font-size:11px}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-NiT3vVb7r1YxYhqe .task.clickable{cursor:pointer}#mermaid-svg-NiT3vVb7r1YxYhqe .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-NiT3vVb7r1YxYhqe .taskText0,#mermaid-svg-NiT3vVb7r1YxYhqe .taskText1,#mermaid-svg-NiT3vVb7r1YxYhqe .taskText2,#mermaid-svg-NiT3vVb7r1YxYhqe .taskText3{fill:#fff}#mermaid-svg-NiT3vVb7r1YxYhqe .task0,#mermaid-svg-NiT3vVb7r1YxYhqe .task1,#mermaid-svg-NiT3vVb7r1YxYhqe .task2,#mermaid-svg-NiT3vVb7r1YxYhqe .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutside0,#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutside2{fill:#000}#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutside1,#mermaid-svg-NiT3vVb7r1YxYhqe .taskTextOutside3{fill:#000}#mermaid-svg-NiT3vVb7r1YxYhqe .active0,#mermaid-svg-NiT3vVb7r1YxYhqe .active1,#mermaid-svg-NiT3vVb7r1YxYhqe .active2,#mermaid-svg-NiT3vVb7r1YxYhqe .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-NiT3vVb7r1YxYhqe .activeText0,#mermaid-svg-NiT3vVb7r1YxYhqe .activeText1,#mermaid-svg-NiT3vVb7r1YxYhqe .activeText2,#mermaid-svg-NiT3vVb7r1YxYhqe .activeText3{fill:#000 !important}#mermaid-svg-NiT3vVb7r1YxYhqe .done0,#mermaid-svg-NiT3vVb7r1YxYhqe .done1,#mermaid-svg-NiT3vVb7r1YxYhqe .done2,#mermaid-svg-NiT3vVb7r1YxYhqe .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-NiT3vVb7r1YxYhqe .doneText0,#mermaid-svg-NiT3vVb7r1YxYhqe .doneText1,#mermaid-svg-NiT3vVb7r1YxYhqe .doneText2,#mermaid-svg-NiT3vVb7r1YxYhqe .doneText3{fill:#000 !important}#mermaid-svg-NiT3vVb7r1YxYhqe .crit0,#mermaid-svg-NiT3vVb7r1YxYhqe .crit1,#mermaid-svg-NiT3vVb7r1YxYhqe .crit2,#mermaid-svg-NiT3vVb7r1YxYhqe .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-NiT3vVb7r1YxYhqe .activeCrit0,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCrit1,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCrit2,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-NiT3vVb7r1YxYhqe .doneCrit0,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCrit1,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCrit2,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-NiT3vVb7r1YxYhqe .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-NiT3vVb7r1YxYhqe .milestoneText{font-style:italic}#mermaid-svg-NiT3vVb7r1YxYhqe .doneCritText0,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCritText1,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCritText2,#mermaid-svg-NiT3vVb7r1YxYhqe .doneCritText3{fill:#000 !important}#mermaid-svg-NiT3vVb7r1YxYhqe .activeCritText0,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCritText1,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCritText2,#mermaid-svg-NiT3vVb7r1YxYhqe .activeCritText3{fill:#000 !important}#mermaid-svg-NiT3vVb7r1YxYhqe .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-NiT3vVb7r1YxYhqe g.classGroup text .title{font-weight:bolder}#mermaid-svg-NiT3vVb7r1YxYhqe g.clickable{cursor:pointer}#mermaid-svg-NiT3vVb7r1YxYhqe g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-NiT3vVb7r1YxYhqe g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-NiT3vVb7r1YxYhqe .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-NiT3vVb7r1YxYhqe .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-NiT3vVb7r1YxYhqe .dashed-line{stroke-dasharray:3}#mermaid-svg-NiT3vVb7r1YxYhqe #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe .commit-id,#mermaid-svg-NiT3vVb7r1YxYhqe .commit-msg,#mermaid-svg-NiT3vVb7r1YxYhqe .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-NiT3vVb7r1YxYhqe g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-NiT3vVb7r1YxYhqe g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-NiT3vVb7r1YxYhqe g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-NiT3vVb7r1YxYhqe .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-NiT3vVb7r1YxYhqe .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-NiT3vVb7r1YxYhqe .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-NiT3vVb7r1YxYhqe .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-NiT3vVb7r1YxYhqe .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-NiT3vVb7r1YxYhqe .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-NiT3vVb7r1YxYhqe .edgeLabel text{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-NiT3vVb7r1YxYhqe .node circle.state-start{fill:black;stroke:black}#mermaid-svg-NiT3vVb7r1YxYhqe .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-NiT3vVb7r1YxYhqe #statediagram-barbEnd{fill:#9370db}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-state .divider{stroke:#9370db}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-NiT3vVb7r1YxYhqe .note-edge{stroke-dasharray:5}#mermaid-svg-NiT3vVb7r1YxYhqe .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-NiT3vVb7r1YxYhqe .error-icon{fill:#522}#mermaid-svg-NiT3vVb7r1YxYhqe .error-text{fill:#522;stroke:#522}#mermaid-svg-NiT3vVb7r1YxYhqe .edge-thickness-normal{stroke-width:2px}#mermaid-svg-NiT3vVb7r1YxYhqe .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-NiT3vVb7r1YxYhqe .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-NiT3vVb7r1YxYhqe .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-NiT3vVb7r1YxYhqe .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-NiT3vVb7r1YxYhqe .marker{fill:#333}#mermaid-svg-NiT3vVb7r1YxYhqe .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-NiT3vVb7r1YxYhqe {color: rgba(0, 0, 0, 0.75);font: ;}

Root命令
cmd1子命令
cmd2子命令
cmd11子子命令
cmd12子子命令

cobra中command常用属性

var rootCmd = &cobra.Command{Use: "",Short: "",Long: "",Example: "",Version: "1.1.0",Aliases: []string{""},SuggestFor: []string{},Args: cobra.ExactArgs(2),// Uncomment the following line if your bare application// has an action associated with it:Run: func(cmd *cobra.Command, args []string){print()},
}

cobra的运行流程

#mermaid-svg-rwF5uoIRpTT8jAsg .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .label text{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .node rect,#mermaid-svg-rwF5uoIRpTT8jAsg .node circle,#mermaid-svg-rwF5uoIRpTT8jAsg .node ellipse,#mermaid-svg-rwF5uoIRpTT8jAsg .node polygon,#mermaid-svg-rwF5uoIRpTT8jAsg .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-rwF5uoIRpTT8jAsg .node .label{text-align:center;fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .node.clickable{cursor:pointer}#mermaid-svg-rwF5uoIRpTT8jAsg .arrowheadPath{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-rwF5uoIRpTT8jAsg .flowchart-link{stroke:#333;fill:none}#mermaid-svg-rwF5uoIRpTT8jAsg .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-rwF5uoIRpTT8jAsg .edgeLabel rect{opacity:0.9}#mermaid-svg-rwF5uoIRpTT8jAsg .edgeLabel span{color:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-rwF5uoIRpTT8jAsg .cluster text{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-rwF5uoIRpTT8jAsg .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-rwF5uoIRpTT8jAsg text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-rwF5uoIRpTT8jAsg .actor-line{stroke:grey}#mermaid-svg-rwF5uoIRpTT8jAsg .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-rwF5uoIRpTT8jAsg #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .sequenceNumber{fill:#fff}#mermaid-svg-rwF5uoIRpTT8jAsg #sequencenumber{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg #crosshead path{fill:#333;stroke:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .messageText{fill:#333;stroke:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-rwF5uoIRpTT8jAsg .labelText,#mermaid-svg-rwF5uoIRpTT8jAsg .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-rwF5uoIRpTT8jAsg .loopText,#mermaid-svg-rwF5uoIRpTT8jAsg .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-rwF5uoIRpTT8jAsg .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-rwF5uoIRpTT8jAsg .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-rwF5uoIRpTT8jAsg .noteText,#mermaid-svg-rwF5uoIRpTT8jAsg .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-rwF5uoIRpTT8jAsg .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-rwF5uoIRpTT8jAsg .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-rwF5uoIRpTT8jAsg .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-rwF5uoIRpTT8jAsg .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .section{stroke:none;opacity:0.2}#mermaid-svg-rwF5uoIRpTT8jAsg .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-rwF5uoIRpTT8jAsg .section2{fill:#fff400}#mermaid-svg-rwF5uoIRpTT8jAsg .section1,#mermaid-svg-rwF5uoIRpTT8jAsg .section3{fill:#fff;opacity:0.2}#mermaid-svg-rwF5uoIRpTT8jAsg .sectionTitle0{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .sectionTitle1{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .sectionTitle2{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .sectionTitle3{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-rwF5uoIRpTT8jAsg .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .grid path{stroke-width:0}#mermaid-svg-rwF5uoIRpTT8jAsg .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-rwF5uoIRpTT8jAsg .task{stroke-width:2}#mermaid-svg-rwF5uoIRpTT8jAsg .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .taskText:not([font-size]){font-size:11px}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-rwF5uoIRpTT8jAsg .task.clickable{cursor:pointer}#mermaid-svg-rwF5uoIRpTT8jAsg .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-rwF5uoIRpTT8jAsg .taskText0,#mermaid-svg-rwF5uoIRpTT8jAsg .taskText1,#mermaid-svg-rwF5uoIRpTT8jAsg .taskText2,#mermaid-svg-rwF5uoIRpTT8jAsg .taskText3{fill:#fff}#mermaid-svg-rwF5uoIRpTT8jAsg .task0,#mermaid-svg-rwF5uoIRpTT8jAsg .task1,#mermaid-svg-rwF5uoIRpTT8jAsg .task2,#mermaid-svg-rwF5uoIRpTT8jAsg .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutside0,#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutside2{fill:#000}#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutside1,#mermaid-svg-rwF5uoIRpTT8jAsg .taskTextOutside3{fill:#000}#mermaid-svg-rwF5uoIRpTT8jAsg .active0,#mermaid-svg-rwF5uoIRpTT8jAsg .active1,#mermaid-svg-rwF5uoIRpTT8jAsg .active2,#mermaid-svg-rwF5uoIRpTT8jAsg .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-rwF5uoIRpTT8jAsg .activeText0,#mermaid-svg-rwF5uoIRpTT8jAsg .activeText1,#mermaid-svg-rwF5uoIRpTT8jAsg .activeText2,#mermaid-svg-rwF5uoIRpTT8jAsg .activeText3{fill:#000 !important}#mermaid-svg-rwF5uoIRpTT8jAsg .done0,#mermaid-svg-rwF5uoIRpTT8jAsg .done1,#mermaid-svg-rwF5uoIRpTT8jAsg .done2,#mermaid-svg-rwF5uoIRpTT8jAsg .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-rwF5uoIRpTT8jAsg .doneText0,#mermaid-svg-rwF5uoIRpTT8jAsg .doneText1,#mermaid-svg-rwF5uoIRpTT8jAsg .doneText2,#mermaid-svg-rwF5uoIRpTT8jAsg .doneText3{fill:#000 !important}#mermaid-svg-rwF5uoIRpTT8jAsg .crit0,#mermaid-svg-rwF5uoIRpTT8jAsg .crit1,#mermaid-svg-rwF5uoIRpTT8jAsg .crit2,#mermaid-svg-rwF5uoIRpTT8jAsg .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-rwF5uoIRpTT8jAsg .activeCrit0,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCrit1,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCrit2,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-rwF5uoIRpTT8jAsg .doneCrit0,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCrit1,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCrit2,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-rwF5uoIRpTT8jAsg .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-rwF5uoIRpTT8jAsg .milestoneText{font-style:italic}#mermaid-svg-rwF5uoIRpTT8jAsg .doneCritText0,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCritText1,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCritText2,#mermaid-svg-rwF5uoIRpTT8jAsg .doneCritText3{fill:#000 !important}#mermaid-svg-rwF5uoIRpTT8jAsg .activeCritText0,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCritText1,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCritText2,#mermaid-svg-rwF5uoIRpTT8jAsg .activeCritText3{fill:#000 !important}#mermaid-svg-rwF5uoIRpTT8jAsg .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-rwF5uoIRpTT8jAsg g.classGroup text .title{font-weight:bolder}#mermaid-svg-rwF5uoIRpTT8jAsg g.clickable{cursor:pointer}#mermaid-svg-rwF5uoIRpTT8jAsg g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-rwF5uoIRpTT8jAsg g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-rwF5uoIRpTT8jAsg .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-rwF5uoIRpTT8jAsg .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-rwF5uoIRpTT8jAsg .dashed-line{stroke-dasharray:3}#mermaid-svg-rwF5uoIRpTT8jAsg #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg .commit-id,#mermaid-svg-rwF5uoIRpTT8jAsg .commit-msg,#mermaid-svg-rwF5uoIRpTT8jAsg .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-rwF5uoIRpTT8jAsg g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-rwF5uoIRpTT8jAsg g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-rwF5uoIRpTT8jAsg g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-rwF5uoIRpTT8jAsg .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-rwF5uoIRpTT8jAsg .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-rwF5uoIRpTT8jAsg .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-rwF5uoIRpTT8jAsg .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-rwF5uoIRpTT8jAsg .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-rwF5uoIRpTT8jAsg .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-rwF5uoIRpTT8jAsg .edgeLabel text{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-rwF5uoIRpTT8jAsg .node circle.state-start{fill:black;stroke:black}#mermaid-svg-rwF5uoIRpTT8jAsg .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-rwF5uoIRpTT8jAsg #statediagram-barbEnd{fill:#9370db}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-state .divider{stroke:#9370db}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-rwF5uoIRpTT8jAsg .note-edge{stroke-dasharray:5}#mermaid-svg-rwF5uoIRpTT8jAsg .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-rwF5uoIRpTT8jAsg .error-icon{fill:#522}#mermaid-svg-rwF5uoIRpTT8jAsg .error-text{fill:#522;stroke:#522}#mermaid-svg-rwF5uoIRpTT8jAsg .edge-thickness-normal{stroke-width:2px}#mermaid-svg-rwF5uoIRpTT8jAsg .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-rwF5uoIRpTT8jAsg .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-rwF5uoIRpTT8jAsg .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-rwF5uoIRpTT8jAsg .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-rwF5uoIRpTT8jAsg .marker{fill:#333}#mermaid-svg-rwF5uoIRpTT8jAsg .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-rwF5uoIRpTT8jAsg {color: rgba(0, 0, 0, 0.75);font: ;}

PersistentPreRun
PreRun
Run
PostRun
PersisPostRun

Cobra源码实现

源代码中,几个文件重点关注。

  • 参数 (args.go args_test.go)
  • 通用设置 (cobra.go cobra_test.go)
  • command结构 (command.go command_notwin.go command_test.go command_win.go)

相关信息

框架源码官方github
https://github.com/spf13/cobra
源码分析视频
https://www.bilibili.com/video/BV1Uz4y1R7Xy
实战视频
https://www.bilibili.com/video/BV1ka4y177iK/?p=7&spm_id_from=pageDriver

最后实战

目录架构

各文件代码

Cobra命令行框架及使用相关推荐

  1. go Cobra命令行工具入门

    简介 Github:https://github.com/spf13/cobra Star:26.5K Cobra是一个用Go语言实现的命令行工具.并且现在正在被很多项目使用,例如:Kubernete ...

  2. cobra命令行工具

    Cobra命令行库 项目地址:https://github.com/spf13/cobra.git 文档地址:https://godoc.org/github.com/spf13/cobra#Comm ...

  3. python编写命令行框架_python的pytest框架之命令行参数详解(上)

    前言 pytest是一款强大的python自动化测试工具,可以胜任各种类型或者级别的软件测试工作.pytest提供了丰富的功能,包括assert重写,第三方插件,以及其他测试工具无法比拟的fixtur ...

  4. python编写命令行框架_使用 Python 和 Click 编写命令行应用程序

    简评:python 构建命令行应用的第三方比较有名的包除了 click 之外还有一个叫 docopt,这是一个以文档先行为目标的包.个人是比较倾向于 Click 的,看了这篇文章之后,也很快写了一个 ...

  5. Go命令行库Cobra的使用

    点击上方"朱小厮的博客",选择"设为星标" 回复"1024"获取独家整理的学习资料 欢迎跳转到本文的原文链接:https://honeypp ...

  6. golang实现一个linux命令ls命令(命令行工具构建)

    希望2023可以听到这些话: 恭喜你得到了这份工作 恭喜你的建议被采用了 恭喜你被录取了 恭喜你的考试顺利通过了 恭喜你上岸了 恭喜你升职了 恭喜你加薪了 恭喜你体检结果一切正常 在这篇文章下面许个愿 ...

  7. go使用cli实现命令行多指令操作

    前言 对于一名初学者来说,想要尽快熟悉 Go 语言特性,所以以操作式的学习方法为主,比如编写一个简单的数学计算器,读取命令行参数,进行数学运算. 本文讲述使用三种方式讲述 Go 语言如何接受命令行参数 ...

  8. Python 命令行参数:Argparse 与 Click

    Python 命令行参数:Argparse 与 Click 简介 一.Argparse 模块 1.1 概念 1.2 基础 1.3 位置参数介绍 1.4 可选参数介绍 1.5 短选项 1.6 结合位置参 ...

  9. Golang实践录:命令行cobra库实例

    本文使用 cobra 库实现一个命令行工具,类似 git.docker.kubectl 这类的工具. 本文仅为一个初具模型的示例,但有实践参考意义. 起因 在编程中,很多时候,程序都会处理多个参数,特 ...

最新文章

  1. GridView直接以excel格式导出到客户端
  2. Django+Linux+Uwsgi+Nginx项目部署文档
  3. 点云网络的论文理解(三)-点云网络的优化 PointNet++的总体说明
  4. linux fedora35 zsh oh-my-zsh 的配置与使用
  5. python中的装饰器(以及多个装饰器详细执行过程)
  6. sql server常用性能计数器
  7. 中大型计算机代表型号,目前个人计算机主要机型.doc
  8. 博士生是学生还是科研工作者?
  9. 苹果被咬一大口!高通获得45亿美元和解金 下一个目标是华为?
  10. spring源码之—Assert.notNull()
  11. 态度决定你的人生高度(一个人能否成功,就看他的态度)
  12. [论文笔记]RoBERTa: A Robustly Optimized BERT Pretraining Approach
  13. JavaScript:indexOf()方法
  14. echarts切换飞线图未清空_讯飞iFLYOS成就产品快速落地 智能硬件迈入直道竞速赛...
  15. C++句柄类 [ 资深博主 ]
  16. lan上网和adsl上网
  17. matlab中提取公因子化简,利用MATLAB化简表达式或者多项式 | 望天博客
  18. 强烈推荐:网工利器PNETLab模拟器
  19. 《我为什么熬夜?》系列之 倚天屠龙记
  20. 《平衡掌控者 游戏数值战斗设计》学习笔记(五)物品掉落

热门文章

  1. 图解Python多修饰器时哪个先起作用
  2. Python一句话过滤字符串中的空白字符和中英文标点
  3. 如何查看外网ip地址_如何查看本机ip地址?
  4. C++ 构造函数 与 析构函数
  5. 手写call,apply
  6. python词云图_人生苦短我用Python——词云图的绘制
  7. 网络大学计算机统考缺考,2020网络教育统考缺考后会有什么影响
  8. mock如何为空_Mockito怎么样Mock返回值为空的方法
  9. android 快捷方式 未安装该应用程序,android,解决手动创建的桌面快捷方式无法跳转到制定的activity的问题,提示未安装应用程序...
  10. springboot controller 分页查询_Spring Boot实战分页查询附近的人: Redis+GeoHash+Lua