0
点赞
收藏
分享

微信扫一扫

go pflag命令行参数解析详解

yundejia 2022-05-31 阅读 53


文章目录

  • ​​1. pflag简介​​
  • ​​2. 安装​​
  • ​​3. 用法​​
  • ​​3.1 练习1​​
  • ​​3.2 练习2​​
  • ​​练习三 flag.Lookup​​
  • ​​4. flag与pflag混用​​

1. pflag简介

pflag是Go的flag包的直接替代,实现了POSIX / GNU样式的–flags。pflag是Go的本机标志包的直接替代。如果您在名称“ flag”下导入pflag,则所有代码应继续运行且无需更改。

github:​​https://github.com/spf13/pflag​​​ 源码包:​​https://godoc.org/github.com/spf13/pflag#Args​​

默认标志位:​​--​

--flag    // boolean flags, or flags with no option default values
--flag x // only on flags without a default value
--flag=x

2. 安装

$ go get github.com/spf13/pflag

3. 用法

3.1 练习1

package main

import (
flag "github.com/spf13/pflag" //替换原生的flag,并兼容
"fmt"
)

var flagvar1 int
var flagvar2 bool

func init() {
flag.IntVar(&flagvar1, "varname1", 1, "help message for flagname")
flag.BoolVarP(&flagvar2, "boolname1", "b", true, "help message")
}


func main() {
var ip1 *int = flag.Int("flagname1", 1, "help message for flagname")

var ip2 = flag.IntP("flagname2", "f", 2, "help message")



flag.Parse()

fmt.Println("ip1 has value ", *ip1)
fmt.Println("ip2 has value ", *ip2)
fmt.Println("flagvar1 has value ", flagvar1)
fmt.Println("flagvar2 has value ", flagvar2)
}

$ go build fplag1.go
/pflag1 -h
Usage of ./pflag1:
-b, --boolname1 help message (default true)
--flagname1 int help message for flagname (default 1)
-f, --flagname2 int help message (default 2)
--varname1 int help message for flagname (default 1)
pflag: help requested

3.2 练习2

package main

import (
"github.com/spf13/pflag"
"net"
"fmt"
"time"
)

func pflagDefine() {
//64位整数,不带单标志位的
var pflagint64 *int64 = pflag.Int64("number1", 1234, "this is int 64, without single flag")

//64位整数,带单标志位的
var pflagint64p *int64 = pflag.Int64P("number2", "n", 2345, "this is int 64, without single flag")

//这种可以把变量的定义和变量取值分开,适合于struct,全局变量等地方
var pflagint64var int64
pflag.Int64Var(&pflagint64var, "number3", 1234, "this is int64var")

//上面那一种的增加短标志位版
var pflagint64varp int64
pflag.Int64VarP(&pflagint64varp,"number4", "m", 1234, "this is int64varp")



//slice版本,其实是上面的增强版,但是支持多个参数,也就是导成一个slice
var pflagint64slice *[]int64 = pflag.Int64Slice("number5", []int64{1234, 3456}, "this is int64 slice")

//bool版本
var pflagbool *bool = pflag.Bool("bool", true, "this is bool")

//bytes版本
var pflagbyte *[]byte = pflag.BytesBase64("byte64", []byte("ea"), "this is byte base64")

//count版本
var pflagcount *int= pflag.Count("count", "this is count")

//duration版本
var pflagduration *time.Duration = pflag.Duration("duration", 10* time.Second, "this is duration")

//float版本
var pflagfloat *float64 = pflag.Float64("float64", 123.345, "this is florat64")

//IP版本
var pflagip *net.IP = pflag.IP("ip1", net.IPv4(192, 168, 1, 1), "this is ip, without single flag")

//mask版本
var pflagmask *net.IPMask= pflag.IPMask("mask", net.IPv4Mask(255,255,255,128),"this is net mask")

//string版本
var pflagstring *string= pflag.String("string", "teststring", "this is string")

//uint版本
var pflaguint *uint64 = pflag.Uint64("uint64", 12345, "this is uint64")

pflag.Parse()
fmt.Println("number1 int64 is ", *pflagint64)
fmt.Println("number2 int64 is ", *pflagint64p)
fmt.Println("number3 int64var is ", pflagint64var)
fmt.Println("number4 int64varp is", pflagint64varp)
fmt.Println("number5 int64slice is", *pflagint64slice)
fmt.Println("bool is ", *pflagbool)
fmt.Println("byte64 is ", *pflagbyte)
fmt.Println("count is ", *pflagcount)
fmt.Println("duration is ", *pflagduration)
fmt.Println("float is ", *pflagfloat)
fmt.Println("ip1 net.ip is ", *pflagip)
fmt.Println("mask is %s", *pflagmask)
fmt.Println("string is ", *pflagstring)
fmt.Println("uint64 is ", *pflaguint)

}

func main() {
pflagDefine()

}

$ go build pflag2.go
$ ./pflag2 -h
Usage of ./pflag1:
--bool this is bool (default true)
--byte64 bytesBase64 this is byte base64 (default ZWE=)
--count count this is count
--duration duration this is duration (default 10s)
--float64 float this is florat64 (default 123.345)
--ip1 ip this is ip, without single flag (default
--mask ipMask this is net mask (default ffffff80)
--number1 int this is int 64, without single flag (defa
-n, --number2 int this is int 64, without single flag (defa
--number3 int this is int64var (default 1234)
-m, --number4 int this is int64varp (default 1234)
--number5 int64Slice this is int64 slice (default [1234,3456])
--string string this is string (default "teststring")
--uint64 uint this is uint64 (default 12345)
pflag: help requested

练习三 flag.Lookup

flag包中提供了一种类似上述的”配置中心”的机制,但这种机制不需要我们显示注入“flag vars”了,我们只需按照flag提供的方法在其他package中读取对应flag变量的值即可。

$tree flaglookup
flaglookup
├── etcd
│ └── etcd.go
└── main.go

// flag-demo/flaglookup/main.go
package main

import (
"flag"
"fmt"
"time"

"./etcd"
)

var (
endpoints string
user string
password string
)

func init() {
flag.StringVar(&endpoints, "endpoints", "127.0.0.1:2379", "comma-separated list of etcdv3 endpoints")
flag.StringVar(&user, "user", "", "etcdv3 client user")
flag.StringVar(&password, "password", "", "etcdv3 client password")
}

func usage() {
fmt.Println("flagdemo-app is a daemon application which provides xxx service.\n")
fmt.Println("Usage of flagdemo-app:\n")
fmt.Println("\t flagdemo-app [options]\n")
fmt.Println("The options are:\n")

flag.PrintDefaults()
}


func main() {
flag.Usage = usage
flag.Parse()

go etcd.EtcdProxy()

time.Sleep(5 * time.Second)
}

// flag-demo/flaglookup/etcd/etcd.go
package etcd

import (
"flag"
"fmt"
)

func EtcdProxy() {
endpoints := flag.Lookup("endpoints").Value.(flag.Getter).Get().(string)
user := flag.Lookup("user").Value.(flag.Getter).Get().(string)
password := flag.Lookup("password").Value.(flag.Getter).Get().(string)

fmt.Println(endpoints, user, password)
}

[root@localhost flaglookup]# go run main.go -endpoints 192.168.10.69:2379,10.10.12.36:2378 -user tonybai -password xyz123
192.168.10.69:2379,10.10.12.36:2378 tonybai xyz123

4. flag与pflag混用

混用flag及pflag时,注意使用的方法

import (
goflag "flag"
flag "github.com/spf13/pflag"
)

var ip *int = flag.Int("flagname", 1234, "help message for flagname")

func main() {
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
flag.Parse()
}

举报

相关推荐

0 条评论