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)")
}

参考:

golang之命令行工具Cobra的更多相关文章

  1. go Cobra命令行工具入门

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

  2. 通过阿里云命令行工具 aliyuncli 购买服务器

    开始想通过 aliyuncli 的 golang 源码进行编译安装(注:python 版的 aliyuncli 已不再维护),但没成功,详见 通过 golang 源码编译阿里云命令行工具 aliyun ...

  3. GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载

    GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...

  4. 一个小时学会用 Go 编写命令行工具

    前言 最近因为项目需要写了一段时间的 Go ,相对于 Java 来说语法简单同时又有着一些 Python 之类的语法糖,让人大呼"真香". 但现阶段相对来说还是 Python 写的 ...

  5. windows下的命令行工具babun

    什么是babun babun是windows上的一个第三方shell,在这个shell上面你可以使用几乎所有linux,unix上面的命令,他几乎可以取代windows的shell.用官方的题目说就是 ...

  6. 『.NET Core CLI工具文档』(一).NET Core 命令行工具(CLI)

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:.NET Core Command Line Tools 翻译:.NET Core命令行工具 什么是 .NET Core ...

  7. 命令行工具aspnet_regiis.exe实现加密和解密web.config

    命令行工具aspnet_regiis.exe,是一个类似于DOS的命令工具,称之为命令解释器.使用命令行工具加密和解密web.config文件中的数据库连接字符串时,只需要简单的语法命令即可. 加密语 ...

  8. nodejs 编写(添加时间戳)命令行工具 timestamp

    Nodejs除了编写服务器端程序还可以编写命令行工具,如gulp.js就是Nodejs编写的. 接下来我们来实现一个添加时间戳的命令: $ timestamp action https://www.n ...

  9. 如何用Node编写命令行工具

    0. 命令行工具 当全局安装模块之后,我们可以在控制台下执行指定的命令来运行操作,如果npm一样.我把这样的模块称之为命令行工具模块(如理解有偏颇,欢迎指正) 1.用Node编写命令行工具 在Node ...

  10. Orchard中的命令行工具

    在Orchard中提供了一个命令行工具,我们可以使用这个命令行工具创建用户.创建博客.生成代码.配置网站.打包模块等.并且这个命令行工具是可以扩充的,只要我们在自己开发的模块中创建一个Command类 ...

随机推荐

  1. C++ weak_ptr除了解决循环引用还能做什么?

    C++: weak_ptr到底有什么用? 很多人对std::weak_ptr的认识只是不增加std::shared_ptr的引用计数,可以用来解决std::shared_ptr可能造成的循环引用问题. ...

  2. Tenzing and Random Operations CF1842G 题解

    [1] 分析 设 \(m\) 次选的位置分别为 \(b_{1\sim m}\). 于是答案为 \(\mathbb E(\prod\limits_{i = 1}^{n}(a_i + \sum\limit ...

  3. 【渗透测试】ATT&CK靶场一,phpmyadmin,域渗透,内网横向移动攻略

    前言 VulnStack,作为红日安全团队匠心打造的知识平台,其独特优势在于全面模拟了国内企业的实际业务场景,涵盖了CMS.漏洞管理及域管理等核心要素.这一设计理念源于红日安全团队对ATT&C ...

  4. 记一次Razor Pages无法编译问题及解决

    解决方案写在前面:更新Visual Studio及相关组件,本人版本自17.8.0更新至17.11.4 缘起于公司的一个业务接口,在有一些信息需要在应用内嵌的webview中展示,信息不少,涉及的前端 ...

  5. Java日期时间API系列35-----Jdk8中java.time包中的新的日期时间API类应用,微秒和纳秒等更精确的时间格式化和解析。

    通过Java日期时间API系列1-----Jdk7及以前的日期时间类中得知,Java8以前除了java.sql.Timestamp扩充纳秒,其他类最大只精确到毫秒:Java8 time包所有相关类都支 ...

  6. C#的Skip 和 Take 方法

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  7. Android复习(六)核心组件—>Activity 任何和返回栈、进程和应用生命周期、Parcelable和Bundle

    了解任务和返回堆栈 任务是用户在执行某项工作时与之互动的一系列 Activity 的集合.这些 Activity 按照每个 Activity 打开的顺序排列在一个返回堆栈中.例如,电子邮件应用可能有一 ...

  8. Codeforces 做题记录 2023-10-22

    远古做题记录.大概是 22 年写的. CF1858E1 Rollbacks (Easy Version) Description 给定一个初始为空的数列 \(a\),你需要处理以下操作: + x 将数 ...

  9. Linux中ln 链接命令的用法

    ln的语法 Usage: ln [OPTION]... [-T] TARGET LINK_NAME (1st form) or: ln [OPTION]... TARGET (2nd form) or ...

  10. 你真的理解 Kubernetes 中的 requests 和 limits 吗?

    在 Kubernetes 集群中部署资源的时候,你是否经常遇到以下情形: 经常在 Kubernetes 集群种部署负载的时候不设置 CPU requests 或将 CPU requests 设置得过低 ...