Go 每日一库之 cobra
简介
cobra是一个命令行程序库,可以用来编写命令行程序。同时,它也提供了一个脚手架,
用于生成基于 cobra 的应用程序框架。非常多知名的开源项目使用了 cobra 库构建命令行,如Kubernetes、Hugo、etcd等等等等。
本文介绍 cobra 库的基本使用和一些有趣的特性。
关于作者spf13,这里多说两句。spf13 开源不少项目,而且他的开源项目质量都比较高。
相信使用过 vim 的都知道spf13-vim,号称 vim 终极配置。
可以一键配置,对于我这样的懒人来说绝对是福音。他的viper是一个完整的配置解决方案。
完美支持 JSON/TOML/YAML/HCL/envfile/Java properties 配置文件等格式,还有一些比较实用的特性,如配置热更新、多查找目录、配置保存等。
还有非常火的静态网站生成器hugo也是他的作品。
快速使用
第三方库都需要先安装,后使用。下面命令安装了cobra
生成器程序和 cobra 库:
$ go get github.com/spf13/cobra/cobra
如果出现了golang.org/x/text
库找不到之类的错误,需要手动从 GitHub 上下载该库,再执行上面的安装命令。我以前写过一篇博客搭建 Go 开发环境提到了这个方法。
我们实现一个简单的命令行程序 git,当然这不是真的 git,只是模拟其命令行。最终还是通过os/exec
库调用外部程序执行真实的 git 命令,返回结果。
所以我们的系统上要安装 git,且 git 在可执行路径中。目前我们只添加一个子命令version
。目录结构如下:
▾ get-started/
▾ cmd/
helper.go
root.go
version.go
main.go
root.go
:
package cmd
import (
"errors"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command {
Use: "git",
Short: "Git is a distributed version control system.",
Long: `Git is a free and open source distributed version control system
designed to handle everything from small to very large projects
with speed and efficiency.`,
Run: func(cmd *cobra.Command, args []string) {
Error(cmd, args, errors.New("unrecognized command"))
},
}
func Execute() {
rootCmd.Execute()
}
version.go
:
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var versionCmd = &cobra.Command {
Use: "version",
Short: "version subcommand show git version info.",
Run: func(cmd *cobra.Command, args []string) {
output, err := ExecuteCommand("git", "version", args...)
if err != nil {
Error(cmd, args, err)
}
fmt.Fprint(os.Stdout, output)
},
}
func init() {
rootCmd.AddCommand(versionCmd)
}
main.go
文件中只是调用命令入口:
package main
import (
"github.com/darjun/go-daily-lib/cobra/get-started/cmd"
)
func main() {
cmd.Execute()
}
为了编码方便,在helpers.go
中封装了调用外部程序和错误处理函数:
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
)
func ExecuteCommand(name string, subname string, args ...string) (string, error) {
args = append([]string{subname}, args...)
cmd := exec.Command(name, args...)
bytes, err := cmd.CombinedOutput()
return string(bytes), err
}
func Error(cmd *cobra.Command, args []string, err error) {
fmt.Fprintf(os.Stderr, "execute %s args:%v error:%v\n", cmd.Name(), args, err)
os.Exit(1)
}
每个 cobra 程序都有一个根命令,可以给它添加任意多个子命令。我们在version.go
的init
函数中将子命令添加到根命令中。
编译程序。注意,不能直接go run main.go
,这已经不是单文件程序了。如果强行要用,请使用go run .
:
$ go build -o main.exe
cobra 自动生成的帮助信息,very cool:
$ ./main.exe -h
Git is a free and open source distributed version control system
designed to handle everything from small to very large projects
with speed and efficiency.
Usage:
git [flags]
git [command]
Available Commands:
help Help about any command
version version subcommand show git version info.
Flags:
-h, --help help for git
Use "git [command] --help" for more information about a command.
单个子命令的帮助信息:
$ ./main.exe version -h
version subcommand show git version info.
Usage:
git version [flags]
Flags:
-h, --help help for version
调用子命令:
$ ./main.exe version
git version 2.19.1.windows.1
未识别的子命令:
$ ./main.exe clone
Error: unknown command "clone" for "git"
Run 'git --help' for usage.
编译时可以将main.exe
改成git
,用起来会更有感觉
Go 每日一库之 cobra的更多相关文章
- Go 每日一库之 viper
简介 上一篇文章介绍 cobra 的时候提到了 viper,今天我们就来介绍一下这个库. viper 是一个配置解决方案,拥有丰富的特性: 支持 JSON/TOML/YAML/HCL/envfile/ ...
- Go 每日一库之 flag
缘起 我一直在想,有什么方式可以让人比较轻易地保持每日学习,持续输出的状态.写博客是一种方式,但不是每天都有想写的,值得写的东西. 有时候一个技术比较复杂,写博客的时候经常会写着写着发现自己的理解有偏 ...
- Go 每日一库之 fsnotify
简介 上一篇文章Go 每日一库之 viper中,我们介绍了 viper 可以监听文件修改进而自动重新加载. 其内部使用的就是fsnotify这个库,它是跨平台的.今天我们就来介绍一下它. 快速使用 先 ...
- Go 每日一库之 go-flags
简介 在上一篇文章中,我们介绍了flag库.flag库是用于解析命令行选项的.但是flag有几个缺点: 不显示支持短选项.当然上一篇文章中也提到过可以通过将两个选项共享同一个变量迂回实现,但写起来比较 ...
- Go 每日一库之 go-homedir
简介 今天我们来看一个很小,很实用的库go-homedir.顾名思义,go-homedir用来获取用户的主目录. 实际上,使用标准库os/user我们也可以得到这个信息: package main i ...
- Go 每日一库之 go-ini
简介 ini 是 Windows 上常用的配置文件格式.MySQL 的 Windows 版就是使用 ini 格式存储配置的. go-ini是 Go 语言中用于操作 ini 文件的第三方库. 本文介绍g ...
- go每日一库 [home-dir] 获取用户主目录
关于我 我的博客|文章首发 顾名思义,go-homedir用来获取用户的主目录.实际上,通过使用标准库os/user我们也可以得到内容,使用以下方式 标准库使用 package main import ...
- [python每日一库]——hotshot
High performance logging profiler 官方文档:http://docs.python.org/2/library/hotshot.html#module-hotshot ...
- go每日新闻--2020-02-27
go 语言中文网(每日资讯)_2020-02-27 一.Go 语言中文网 如何正确看待 Google 宣布 Fuchsia 操作系统没有选 Go 作为终端开发语言 Actor 还是 CSP?Go 中的 ...
随机推荐
- 【u221】分数
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 高考分数刚刚公布.共有n人参加考试,为了便于填报志愿,教育部把所有考生的成绩平均分为m档.保证n是m的 ...
- 【js】 vue 2.5.1 源码学习(十二)模板编译
大体思路(十) 本节内容: 1. baseoptions 参数分析 2. options 参数分析 3. parse 编译器 4. parseHTNL 函数解析 // parse 解析 parser- ...
- vmware虚拟机卸载干净在注册表的也需要删除
- springboot+jpa分页(Pageable+Page)
Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖) Controller控制层 package com.gxuwz.late.controller; import co ...
- vue2.x+elelmentUI@3.5 表格
<template> <section> <el-row> <el-col :span="16"> <!--表单--> ...
- asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported
在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...
- GapMinder气泡图:在线互动图表数据平台
GapMinder:在线互动图表数据平台是一个将国际统计数据转换成活动的.交互的和有趣的图表,以在线统计数据为基础的互动图表集的完美世界.目的是通过增进对可以自由访问的公共统计数据的使用和理解,以促进 ...
- Trie 树的一些题
Trie 树的一些题 牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不 ...
- Cisco DNA网络POC
角色名词解释 拓扑图 集成ISE
- PostgreSQL 遇到 column "value" does not exist
初次使用PostgreSQL,在执行插入语句的时候死活插入不进去数据 INSERT INTO pre_trait ( archive_id, apply_from, owner_area_code ) ...