使用 cobra-cli 搭建手脚架

# 安装 cobra-cli
go install github.com/spf13/cobra-cli@latest
# 创建一个应用
mkdir myapp && cd myapp
# 初始化 go mod
go mod init myapp
# 使用 cobra-cli 搭建手脚架
cobra-cli init

注意: cobra-cli 在 go1.18+ 的 go work 模式下有 bug, 不要使用 go work 模式

2022年11月18日的最新版本1.3.0 查看 issue https://github.com/spf13/cobra-cli/issues/26

查看生成的代码

$ tree -L 2
.
├── LICENSE
├── cmd
│ └── root.go
├── go.mod
├── go.sum
└── main.go

测试运行

$ go run main.go
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.

新增命令

cobra-cli add serve
cobra-cli add config

查看新增的命令

$ go run main.go
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. Usage:
myapp [command] Available Commands:
completion Generate the autocompletion script for the specified shell
config A brief description of your command
help Help about any command
serve A brief description of your command Flags:
-h, --help help for myapp
-t, --toggle Help message for toggle Use "myapp [command] --help" for more information about a command.
$

新增 config 命令的子命令

cobra-cli add create -p 'configCmd'

查看新增的子命令

$ go run main.go config --help
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. Usage:
myapp config [flags]
myapp config [command] Available Commands:
create A brief description of your command Flags:
-h, --help help for config Use "myapp config [command] --help" for more information about a command.
$

填充业务逻辑

定义 Flags

Flag 有 2 种, Persistent Flag 和 Local Flag, Persistent Flag 可以在此命令和所有的子命令都生效, Local Flag 只是在此命令生效

比如绑定到 rootCmd 的 Persistent Flag 相当于全局 Flag, 所有的命令都能调用

rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")

但是 Local Flag 只是在当前有效

localCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")

reference

更多参考

Go 使用 Cobra 构建 CLI 程序的更多相关文章

  1. 快速构建CLI程序并发布到PyPi

    构造一个简单的CLI程序 typer 这个从去年就被各种营销号吹成Web框架的 第三方库, 与 FastAPI 同出一人之手,它不是Web框架,它是一个用来构建CLI程序的库,我们就简单搞个例子 # ...

  2. iOS-马上着手开发iOS应用应用程序-第二部分构建应用程序

    第二部分构建应用程序 1,应用程序开发过程 2,设计用户界面 3,定义交互 4,教程:串联图 1,应用程序开发过程 定义概念 设计用户界面 定义交互 实现行为整合数据 对象是应用程序的基石 类是对象的 ...

  3. 实战案例--Grunt构建Web程序

    GruntJS构建Web程序.使用Gruntjs来搭建一个前端项目,然后使用grunt合并,压缩JS文件,熟练了node.js安装和grunt.js安装后,接下来来实战一个案例,案例是根据snandy ...

  4. Go基础系列:构建go程序

    hello world 从一个简单的程序开始解释,将下面的内容放进test.go文件中,路径随意: package main import ( "fmt" ) func main( ...

  5. Maven构建应用程序常用配置(转)

    来自:http://shiyanjun.cn/archives/180.html 使用Maven来构建应用程序,可以非常方便地管理应用相关的资源.众所周知,应用程序中涉及到的一些依赖关系,如Java应 ...

  6. wepy框架构建小程序(1)

    wepy框架构建小程序(1) 基本操作: # 安装脚手架工具 npm install wepy-cli -g # 创建一个新的项目 npm init standard myproject # 进入新项 ...

  7. BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发、集成和构建应用程序 总结

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发.集成和构建应用程序  总结         SharePoint开发已经 ...

  8. 【译】写个好的 CLI 程序

    写个好的 CLI 程序 Write a Good CLI Program 译文 原文链接:https://qiita.com/tigercosmos/items/678f39b1209e60843cc ...

  9. 多阶段构建Golang程序Docker镜像

    Docker简介 Docker是基于Linux容器技术(LXC),使用Go语言实现的开源项目,诞生于2013年,遵循Apache2.0协议.Docker自开源后,受到广泛的关注和讨论. Docker在 ...

  10. go如何编写命令行(cli)程序

    创建一个命令行程序 问题 如何使用golang创建可以在命令行当中传递参数的程序?go如何带参数执行程序? 比如我们期望使用hello -version来查看hello程序的版本号码.或者输入hell ...

随机推荐

  1. 深度学习论文翻译解析(二十二):Uniformed Students Student-Teacher Anomaly Detection With Discriminative Latent Embbeddings

    论文标题:Uniformed Students Student-Teacher Anomaly Detection With Discriminative Latent Embbeddings 论文作 ...

  2. k8s中查看pod的yaml文件的案例

    在Kubernetes (K8s) 中,Pod 的 YAML 文件定义了 Pod 的配置和规格.当你想要查看 Pod 的 YAML 文件参数参考时,通常是为了了解可以配置哪些字段以及这些字段的含义. ...

  3. 使用Express写接口

    接口规范 随着前后端分离越来越普遍, 后端接口规范也就越来越重要了,一套良好的接口规范可以提升工作效率, 减少沟通障碍.通常我们都会采用RestfulApi方式来提供接口, 使用 JSON 来传输数据 ...

  4. Vulkan Support Check and Dynamic Loader C++ code sample

    很多时候不想静态依赖VulkanSDK所提供的静态库,因为会遇到一些过早的电脑不支持vulkan, 那么就需要使用动态加载vulkan-1.dll(for Windows)或libMoltenVK.d ...

  5. minos 1.1 内存虚拟化——hyp

    首发公号:Rand_cs minos 1.1 内存虚拟化--hyp 内存虚拟化,目前理解主要两方面: 内存管理,没有虚拟化的情况时,对于 Linux 内核运行在物理硬件之上,内核需要管理物理内存,需要 ...

  6. LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)

    题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...

  7. LeetCode 221. Maximal Square 最大正方形(C++/Java)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's an ...

  8. java springboot 读取自定义配置文件

    java springboot 读取自定义配置文件 application.properties.test.properties maven中引用 : <dependency> <g ...

  9. 设置profile启动配置 -Dspring.profiles.active=dev

  10. mysql子查询不支持limit问题解决

    如果sql语句中的子查询包含limit 例如: select * from table where id in (select id from table limit 3) 会报错: This ver ...