当前位置:首页 > 尘凡 > 正文内容

go原生flag库非常好的例子

满纸空言3年前 (2021-08-13)尘凡25710

参考:https://github.com/davecheney/httpstat/blob/master/main.go

 

解决了几个问题:

1、无输入时,显示帮助

2、增加补充注释、例子等

 

仍需改进问题:

1、不支持长短参数,如-c或--config

 

例子:

package main

import (
	"flag"
	"fmt"
	"io"
	"lictool/license"
	"os"
)

func main() {

	v := flag.Bool("v", false, "show version")
	g := flag.Bool("g", false, "Generate a hardware license request")
	c := flag.Bool("c", false, "Check whether the exist license file is valid")
	m := flag.Bool("m", false, "get mac")
	sn := flag.String("s", "00000000-0000-0000-0000-000000000002", "license request SN\r\n")
	licPath := flag.String("p", "license", "Check license file path\r\n")
	outPath := flag.String("o", "license.req", "Output license request file path\r\n")

	flag.Usage = usage
	flag.Parse()

	if len(os.Args)<2 {
		flag.Usage()
		os.Exit(0)
	}

	if *v {
		fmt.Println("Version: " + license.Version)
		fmt.Println("BuildTime: " + license.Compile)
		os.Exit(0)
	}

	if *g {
		if license.GenLicenseReq(*sn, *outPath) != nil {
			fmt.Println("error")
		}
		os.Exit(0)
	}

	if *c {
		lic, err := license.CheckLicense(*licPath)
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(0)
		}
		fmt.Println(lic.Tbs)
		os.Exit(0)
	}

	if *m {
		fmt.Println(license.GetMac())
		os.Exit(0)
	}

	//flag.PrintDefaults()
}

func doPrintf(w io.Writer, format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format+"\n", a...)
}
func usage() {
	doPrintf(os.Stderr, "Version: %s", license.Version)
	doPrintf(os.Stderr, "BuildTime: %s", license.Compile)
	doPrintf(os.Stderr, "Usage: %s [OPTIONS] args\n", os.Args[0])
	doPrintf(os.Stderr, "OPTIONS:")
	flag.PrintDefaults()
	doPrintf(os.Stderr, "")
	doPrintf(os.Stderr, "USAGE:")
	doPrintf(os.Stderr, "  Get Local MAC")
	doPrintf(os.Stderr, "        %s -m", os.Args[0])
	doPrintf(os.Stderr, "  Check License")
	doPrintf(os.Stderr, "        %s -c", os.Args[0])
	doPrintf(os.Stderr, "        %s -c -p license", os.Args[0])
	doPrintf(os.Stderr, "  Generate License Request")
	doPrintf(os.Stderr, "        %s -g", os.Args[0])
	doPrintf(os.Stderr, "        %s -g -o output.lic", os.Args[0])
	doPrintf(os.Stderr, "        %s -g -s 00000000-0000-0000-0000-000000000002 -o output.lic", os.Args[0])
	doPrintf(os.Stderr, "")
}

效果:

[root@mzky licTool]# ./licTool 
Version: 3b78d9c0ae0e1649debbc6681f56dfe41148158a @2021-08-12 18:22:21 +0800
BuildTime: 2021-08-16 09:54:20 by go version go1.16.2 linux/amd64
Usage: ./licTool [OPTIONS] args

OPTIONS:
  -c    Check whether the exist license file is valid
  -g    Generate a hardware license request
  -m    get mac
  -o string
        Output license request file path
         (default "license.req")
  -p string
        Check license file path
         (default "license")
  -s string
        license request SN
         (default "00000000-0000-0000-0000-000000000002")
  -v    show version

USAGE:
  Get Local MAC
        ./licTool -m
  Check License
        ./licTool -c
        ./licTool -c -p license
  Generate License Request
        ./licTool -g
        ./licTool -g -o output.lic
        ./licTool -g -s 00000000-0000-0000-0000-000000000002 -o output.lic

 

 

 

 

扫描二维码推送至手机访问。

版权声明:本文由满纸空言发布,如需转载请注明出处。

本文链接:https://mzky.cc/post/46.html

分享给朋友:

“go原生flag库非常好的例子” 的相关文章

红旗11系统下载4年前 (2021-07-09)
uos编译njmon4年前 (2021-07-21)
Golong发送HTTP、HTTPS请求3年前 (2021-08-19)

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。