golang之命令行工具Cobra
github地址: https://github.com/spf13/cobra
[安装]
go get -u github.com/spf13/cobra@latest
使用cobra_cli工具
go install github.com/spf13/cobra-cli@latest
[使用]
# 初始化工程
cobra-cli init # 添加应用
cobra-cli add timezone
示例:
// timezone.go
/*
Copyright 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd import (
"fmt"
"time" "github.com/spf13/cobra"
) // timezoneCmd represents the timezone command
var timezoneCmd = &cobra.Command{
Use: "timezone",
Short: "Get the current time in a given timezone",
Long: `Get the current time in a given timezone.
This command takes one argument, the timezone you want to get the current time,
It returns the current time in datetime format.
`,
Args: cobra.ExactArgs(1), // 需要一个额外参数
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("timezone called")
timezone := args[0]
//currentTime, err := getTimeInTimezone(timezone)
//if err != nil {
// log.Fatalln("The timezone string is invalid")
//}
//fmt.Println(currentTime)
//
location, _ := time.LoadLocation(timezone)
currentTime := time.Now().In(location)
dateFlag, _ := cmd.Flags().GetString("date")
var date string
if dateFlag != "" {
date = currentTime.Format(dateFlag)
} else {
date = currentTime.Format(time.RFC1123)
}
fmt.Printf("current timezone:%v , time:%v\n", timezone, date)
},
} func init() {
rootCmd.AddCommand(timezoneCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
timezoneCmd.PersistentFlags().String("date", "", "returns the date in a time zone in a specialfied format") // Cobra supports local flags which will only run when this command
// is called directly, e.g.:
//timezoneCmd.Flags().String("date", "", "Date for which to get the time (format: yyyy-mm-dd)")
}
参考:
- 示例Demo:https://www.digitalocean.com/community/tutorials/how-to-use-the-cobra-package-in-go
- 更多使用详情: https://xie.infoq.cn/article/915006cf3760c99ad0028d895
golang之命令行工具Cobra的更多相关文章
- go Cobra命令行工具入门
简介 Github:https://github.com/spf13/cobra Star:26.5K Cobra是一个用Go语言实现的命令行工具.并且现在正在被很多项目使用,例如:Kuberne ...
- 通过阿里云命令行工具 aliyuncli 购买服务器
开始想通过 aliyuncli 的 golang 源码进行编译安装(注:python 版的 aliyuncli 已不再维护),但没成功,详见 通过 golang 源码编译阿里云命令行工具 aliyun ...
- GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载
GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...
- 一个小时学会用 Go 编写命令行工具
前言 最近因为项目需要写了一段时间的 Go ,相对于 Java 来说语法简单同时又有着一些 Python 之类的语法糖,让人大呼"真香". 但现阶段相对来说还是 Python 写的 ...
- windows下的命令行工具babun
什么是babun babun是windows上的一个第三方shell,在这个shell上面你可以使用几乎所有linux,unix上面的命令,他几乎可以取代windows的shell.用官方的题目说就是 ...
- 『.NET Core CLI工具文档』(一).NET Core 命令行工具(CLI)
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:.NET Core Command Line Tools 翻译:.NET Core命令行工具 什么是 .NET Core ...
- 命令行工具aspnet_regiis.exe实现加密和解密web.config
命令行工具aspnet_regiis.exe,是一个类似于DOS的命令工具,称之为命令解释器.使用命令行工具加密和解密web.config文件中的数据库连接字符串时,只需要简单的语法命令即可. 加密语 ...
- nodejs 编写(添加时间戳)命令行工具 timestamp
Nodejs除了编写服务器端程序还可以编写命令行工具,如gulp.js就是Nodejs编写的. 接下来我们来实现一个添加时间戳的命令: $ timestamp action https://www.n ...
- 如何用Node编写命令行工具
0. 命令行工具 当全局安装模块之后,我们可以在控制台下执行指定的命令来运行操作,如果npm一样.我把这样的模块称之为命令行工具模块(如理解有偏颇,欢迎指正) 1.用Node编写命令行工具 在Node ...
- Orchard中的命令行工具
在Orchard中提供了一个命令行工具,我们可以使用这个命令行工具创建用户.创建博客.生成代码.配置网站.打包模块等.并且这个命令行工具是可以扩充的,只要我们在自己开发的模块中创建一个Command类 ...
随机推荐
- [big data] main entry for Spark, Zeppelin, Delta Lake ...
1. 环境搭建 big data env setup 2. Spark 学习 spark 怎么读写 elasticsearch spark 怎么 连接 读写 ElasticSearch Spark 上 ...
- P7706 「Wdsr-2.7」文文的摄影布置
题意 给定长度为 \(n\) 的数组 \(a\) 和 \(b\),支持单点修改,\(q\) 次区间查询 \(\max_{l\le i<k\le r} \{a_i + a_k - \min_{i& ...
- containerd 拉取k8s.gcr.io/pause镜像i/o timeout
由于k8s.gcr.io 需要连外网才可以拉取到,导致 k8s 的基础容器 pause 经常无法获取.k8s docker 可使用代理服拉取,再利用 docker tag 解决问题 docker pu ...
- C++: 如何高效地往unordered_map中插入key-value
C++: unordered_map 花式插入key-value的5种方式 前言 无意中发现std::unordered_map.std::map等插入key-value对在C++17后竟有了 ins ...
- python 浅拷贝与深拷贝
赋值引用 >>> a= {1:[1,2]}>>> b = a>>> b[2]=3>>> b {1: [1, 2], 2: 3} ...
- sql 查询死锁
1 -- 查询死锁 2 --第一种 3 sp_lock 4 5 --第二种 6 select object_name(resource_associated_entity_id) as tableNa ...
- EF Core – 冷知识
Add vs AddAsync 参考: .NET 5 REST API Tutorial AddAsync() vs Add() in EF Core EF Core's AddAsync v. Ad ...
- Maven 使用方法
Maven Maven是一个项目管理工具,它包含了一个项目对象模型(POM:Project Object Model),其表现于一个XML文件(pom.xml),其中包含了项目的基本学习,依赖关系,插 ...
- 基于图扑 HT for Web 实现的昼夜切换场景应用
图扑软件 HT 的案例中有许多白天黑夜效果.这种效果在各类不同的项目中得到了广泛的应用和认可. 白天黑夜效果是视觉设计和交互设计中常见的一种手法.通过细致巧妙地调整色彩.亮度.对比度等视觉参数,即可成 ...
- JavaScript中if嵌套assert的方法
在JavaScript中,通常我们不会直接使用assert这个词,因为JavaScript标准库中并没有直接提供assert函数(尽管在一些测试框架如Jest.Mocha中经常看到).但是,我们可以模 ...