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类 ...
随机推荐
- 【YashanDB数据库】YAS-02024 lock wait timeout, wait time 0 milliseconds
[标题]错误码处理 [问题分类]锁等待超时 [关键字]YAS-02024 [问题描述]执行语句时候,因锁等待超时执行语句失败 [问题原因分析]数据库默认锁等待时间为0秒,如果执行语句存在锁等待过长会执 ...
- sicp每日一题[1.44]
Exercise 1.44 The idea of smoothing a function is an important concept in signal processing. If f is ...
- 8.18域横向smb&wmi明文或hash传递
知识点 windows 2012以上版本默认关闭wdigust,攻击者无法从内存中获取明文密码: Windows2012以下版本如安装KB287199补丁,同样也无法从内存中获取明文密码: 解决方法: ...
- C++: 虚函数,一些可能被忽视的细节
C++: 虚函数,一些可能被忽视的细节 引言:关于C++虚函数,对某些细节的理解不深入,可能导致我们的程序无法按预期结果运行,或是表明我们对其基本原理理解不够透彻.本文详细解答以下几个问题:实现多态, ...
- 如何发布一个Vue组件到Npm上?
前端时间做了一个基于Vue的拼图验证组件,因为公司需要,就想着做完之后放到Npm上,方便使用 发布流程如下: 1. 创建一个Npm账号并进行邮箱确认(很重要) 2. 创建一个文件夹,然后 npm in ...
- 中国信通院高质量数字化转型产品及服务全景图发布,合合信息多项AI产品入选
随着5G.人工智能.大数据等新一代技术的发展,企业在商业竞争中正面临更多不确定性.中国信通院高度关注企业数字化转型中遇到的痛点,发起"铸基计划-高质量数字化转型行动",链接企业数字 ...
- Hydra(海德拉)工具使用从0到1,爆破服务器密码,2024最新版
Hydra(海德拉)工具使用从0到1,爆破服务器密码,2024最新版 Hydra简介 Hydra又叫九头蛇,是一款由著名的黑客组织THC开发的开源暴力破解工具,支持大部分协议的在线密码破解,是网络安全 ...
- 系统编程-进程-当fork遇到管道,可能碰撞出什么?
第一部分 1. 直接上代码 #include <stdio.h> #include <unistd.h> #include <stdlib.h> int globv ...
- C#/.NET/.NET Core技术前沿周刊 | 第 6 期(2024年9.16-9.22)
前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...
- Atcoder Beginner Contest 367
A.Shout Everyday \(\text{Diff }43\) 给你 \(24\) 小时制下的 \(A,B,C\) 三个时刻,问 \(A\) 是否在 \([B,C]\) 范围内 考虑到先将 \ ...