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

尽情享用吧

相关推荐
Victor35610 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor35610 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术12 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
二十雨辰12 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码12 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚12 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂13 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas13613 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
Gogo81613 小时前
BigInt 与 Number 的爱恨情仇,为何大佬都劝你“能用 Number 就别用 BigInt”?
后端
fuquxiaoguang13 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析