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

相关文章

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal