即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Go语言学习与进阶之flag包(The way to go)

编程语言 wangshubo1989 74℃ 0评论

生命不止,继续 go go go !!!

今天跟大家分享的是golang中的flag package,如果你在golang中用到了命令行参数,那么你就必须要了解一些flag的知识。

Package flag


作用:


Package flag implements command-line flag parsing.


Go语言通过使用标准库里的flag包来处理命令行参数。

定义flags


定义String flag:

func (f *FlagSet) String(name string, value string, usage string) *string

String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.

定义int flag:

func Int64(name string, value int64, usage string) *int64

Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.

定义布尔flag:

func Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

这里唯一指的注意的就是返回值:是指针

应用示例:

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

将flag绑定到一个变量

func StringVar(p *string, name string, value string, usage string)

StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

func IntVar(p *int, name string, value int, usage string)

IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.

func BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.

应用示例:

var flagvar int
func init() {
    flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}

绑定自定义的类型


这里就简单提一嘴,我们自定义的类型需要实现value接口,那么就可以通过flag.Var方法了:

flag.Var(&flagVal, "name", "help message for flagname")

For such flags, the default value is just the initial value of the variable.

将命令行参数解析到flag中

func Parse()

Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.


解析函数将会在碰到第一个非flag命令行参数时停止。


非flag命令行参数是指不满足命令行语法的参数,如命令行参数为cmd --flag=true abc 则第一个非flag命令行参数为“abc”


调用Parse解析后,就可以直接使用flag本身(指针类型)或者绑定的变量了(值类型)

NArg和NFlag


NArg is the number of arguments remaining after flags have been processed.


获得non-flag个数

NFlag returns the number of command-line flags that have been set.


获得flag个数

获取非flag参数

func Arg(i int) string

Arg returns the i’th command-line argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist.

func Args() []string

Args returns the non-flag command-line arguments.

如何区分non-flag和flag


命令行参数的格式可以是:


-flag xxx (使用空格,一个 – 符号)


–flag xxx (使用空格,两个 – 符号)


-flag=xxx (使用等号,一个 – 符号)


–flag=xxx (使用等号,两个 – 符号)


其中,布尔类型的参数防止解析时的二义性,应该使用等号的方式指定

应用


新建一个test_flag.go,键入代码:

package main

import (
    "flag"
    "fmt"
)

var Input_Name = flag.String("name", "dabaojian", "input your name")
var Input_Age = flag.Int("age", 20, "input your age")
var Input_Gender = flag.String("gender", "male", "input your age")
var Input_flagvar int

func Init() {
    flag.IntVar(&Input_flagvar, "flagname", 1234, "help message for flagname")
}

func main() {
    Init()
    flag.Parse()

    fmt.Printf("args=%s, num=%d\n", flag.Args(), flag.NArg())
    fmt.Printf("flag num=%d\n", flag.NFlag())

    for i := 0; i != flag.NArg(); i++ {
        fmt.Printf("arg[%d]=%s\n", i, flag.Arg(i))
    }

    fmt.Println("name=", *Input_Name)
    fmt.Println("age=", *Input_Age)
    fmt.Println("gender=", *Input_Gender)
    fmt.Println("flagname=", Input_flagvar)
}

build:

go build test_flag.go

执行:


D:\go_workspace\src\go_flag\test_flag.exe –name “11” -age=22 -flagname=0x22 fuck hit ds

输出:


args=[fuck hit ds], num=3


flag num=3


arg[0]=fuck


arg[1]=hit


arg[2]=ds


name= 11


age= 22


gender= male


flagname= 34

这里写图片描述

转载请注明:CodingBlog » Go语言学习与进阶之flag包(The way to go)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情