第一个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

相关文章

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.