本文主要是介绍第一个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指令并调整指令模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!