第一个golang项目增加help指令并调整指令模式

2024-09-02 03:52

本文主要是介绍第一个golang项目增加help指令并调整指令模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一个golang项目增加help指令并调整指令模式

  • 调整指令模式
  • 增加help指令
  • 减少了配置文件的解析读取次数
  • 新指令模式
  • 打包并运行

上一篇文章

调整指令模式

  • version指令修改为-v-version
  • replace指令修改为-r-replace
  • dir参数修改为-d-directory
package commandsimport ("flag""fmt""log""os""strings""github.com/spf13/viper"
)var (help    boolversion boolreplace booldirectory string
)func Init() {flag.BoolVar(&help, "h", false, "this `help` and exit")flag.BoolVar(&help, "help", false, "this `help` and exit")flag.BoolVar(&version, "v", false, "show `version` and exit")flag.BoolVar(&version, "version", false, "show `version` and exit")flag.BoolVar(&replace, "r", false, "send `replace` to process and replace content for scan files under directory.")flag.BoolVar(&replace, "replace", false, "send `replace` to process and replace content for scan files under directory.")// 注意 `directory`。默认是 -d string,有了 `directory` 之后,变为 -d directoryflag.StringVar(&directory, "d", "", "send `directory` to process: standard, standard-fix, saas, saas-fix")flag.StringVar(&directory, "directory", "", "send `directory` to process: standard, standard-fix, saas, saas-fix")// 改变默认的 Usageflag.Usage = usageviper.SetConfigName("config") // 配置文件名(不包含扩展名)viper.SetConfigType("toml")   // 配置文件格式viper.AddConfigPath(".")      // 查找配置文件的路径if err := viper.ReadInConfig(); err != nil {log.Fatalf("Error reading config file, %s", err)}flag.Parse()
}func usage() {version := viper.Get("version").(string)cmd := os.Args[0]fmt.Fprintf(os.Stderr, `%s version: %s
Usage: %s [-hvq] [-d,directory directory]Options:
`, cmd, version, cmd)flag.PrintDefaults()
}func Run() {Init()if help {flag.Usage()} else if version {Version()} else if replace {dir := "standard"if strings.TrimSpace(directory) != "" {dir = directory}Replace(dir)} else {flag.Usage()}
}

增加help指令

  • -h-help指令,打印程序已知参数列表及参数说明
func usage() {version := viper.Get("version").(string)cmd := os.Args[0]fmt.Fprintf(os.Stderr, `%s version: %s
Usage: %s [-hvq] [-d,directory directory]Options:
`, cmd, version, cmd)flag.PrintDefaults()
}

减少了配置文件的解析读取次数

  • 在程序执行初始化时解析一次,后续皆可使用
func Init() {flag.BoolVar(&help, "h", false, "this `help` and exit")flag.BoolVar(&help, "help", false, "this `help` and exit")flag.BoolVar(&version, "v", false, "show `version` and exit")flag.BoolVar(&version, "version", false, "show `version` and exit")flag.BoolVar(&replace, "r", false, "send `replace` to process and replace content for scan files under directory.")flag.BoolVar(&replace, "replace", false, "send `replace` to process and replace content for scan files under directory.")// 注意 `directory`。默认是 -d string,有了 `directory` 之后,变为 -d directoryflag.StringVar(&directory, "d", "", "send `directory` to process: standard, standard-fix, saas, saas-fix")flag.StringVar(&directory, "directory", "", "send `directory` to process: standard, standard-fix, saas, saas-fix")// 改变默认的 Usageflag.Usage = usageviper.SetConfigName("config") // 配置文件名(不包含扩展名)viper.SetConfigType("toml")   // 配置文件格式viper.AddConfigPath(".")      // 查找配置文件的路径if err := viper.ReadInConfig(); err != nil {log.Fatalf("Error reading config file, %s", err)}flag.Parse()
}

新指令模式

func Run() {Init()if help {flag.Usage()} else if version {Version()} else if replace {dir := "standard"if strings.TrimSpace(directory) != "" {dir = directory}Replace(dir)} else {flag.Usage()}
}

打包并运行

go build -v -ldflags "-linkmode external -extldflags '-static' -w" -o devtools.exe main.go
devtools.exe -h
devtools.exe -help
devtools.exe -v
devtools.exe -version
devtools.exe -r -d saas
devtools.exe -replace -directory saas

在这里插入图片描述

https://gitee.com/xqxyxchy/dev-tools
尽情享用吧

这篇关于第一个golang项目增加help指令并调整指令模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128919

相关文章

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

golang 日志log与logrus示例详解

《golang日志log与logrus示例详解》log是Go语言标准库中一个简单的日志库,本文给大家介绍golang日志log与logrus示例详解,感兴趣的朋友一起看看吧... 目录一、Go 标准库 log 详解1. 功能特点2. 常用函数3. 示例代码4. 优势和局限二、第三方库 logrus 详解1.

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4