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

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

上一篇文章

调整指令模式

  • version指令修改为-v-version

  • replace指令修改为-r-replace

  • dir参数修改为-d-directory

    package commands

    import (
    "flag"
    "fmt"
    "log"
    "os"
    "strings"

    复制代码
      "github.com/spf13/viper"

    )

    var (
    help bool
    version bool
    replace bool

    复制代码
      directory 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 directory
      flag.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")
    
      // 改变默认的 Usage
      flag.Usage = usage
    
      viper.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 directory
      flag.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")
    
      // 改变默认的 Usage
      flag.Usage = usage
    
      viper.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

尽情享用吧

相关推荐
语落心生几秒前
Apache Geaflow推理框架Geaflow-infer 解析系列(五)环境上下文管理
后端
程序员爱钓鱼2 分钟前
用 Python 批量生成炫酷扫光 GIF 动效
后端·python·trae
封奚泽优5 分钟前
下降算法(Python实现)
开发语言·python·算法
aiopencode15 分钟前
iOS 应用上架的工程实践复盘,从构建交付到审核通过的全流程拆解
后端
笃行客从不躺平27 分钟前
遇到大SQL怎么处理
java·开发语言·数据库·sql
郝学胜-神的一滴27 分钟前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
q***876034 分钟前
Spring Boot 整合 Keycloak
java·spring boot·后端
Billow_lamb35 分钟前
Spring Boot2.x.x全局拦截器
java·spring boot·后端
g***B7381 小时前
Kotlin协程在Android中的使用
android·开发语言·kotlin
火白学安全1 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全