参考标准库
import "flag"
flag包实现了命令行参数的解析。
要求:
使用flag.String(), Bool(), Int()等函数注册flag,下例声明了一个整数flag,解析结果保存在*int指针ip里:
import "flag"
var ip = flag.Int("flagname", 1234, "help message for flagname")
如果你喜欢,也可以将flag绑定到一个变量,使用Var系列函数:
var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}
或者你可以自定义一个用于flag的类型(满足Value接口)并将该类型用于flag解析,如下:
flag.Var(&flagVal, "name", "help message for flagname")
对这种flag,默认值就是该变量的初始值。
在所有flag都注册之后,调用:
flag.Parse()
来解析命令行参数写入注册的flag里。
解析之后,flag的值可以直接使用。如果你使用的是flag自身,它们是指针;如果你绑定到了某个变量,它们是值。
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
解析后,flag后面的参数可以从flag.Args()里获取或用flag.Arg(i)单独获取。这些参数的索引为从0到flag.NArg()-1。
命令行flag语法:
-flag
-flag=x
-flag x // 只有非bool类型的flag可以
可以使用1个或2个'-'号,效果是一样的。最后一种格式不能用于bool类型的flag,因为如果有文件名为0、false等时,如下命令:
cmd -x *
其含义会改变。你必须使用-flag=false格式来关闭一个bool类型flag。
Flag解析在第一个非flag参数(单个"-"不是flag参数)之前停止,或者在终止符"--"之后停止。
整数flag接受1234、0664、0x1234等类型,也可以是负数。bool类型flag可以是:
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
时间段flag接受任何合法的可提供给time.ParseDuration的输入。
默认的命令行flag集被包水平的函数控制。FlagSet类型允许程序员定义独立的flag集,例如实现命令行界面下的子命令。FlagSet的方法和包水平的函数是非常类似的。
// These examples demonstrate more intricate uses of the flag package.
package flag_test
import (
"errors"
"flag"
"fmt"
"strings"
"time"
)
// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")
// Example 2: Two flags sharing a variable, so we can have a shorthand.
// The order of initialization is undefined, so make sure both use the
// same default value. They must be set up with an init function.
var gopherType string
func init() {
const (
defaultGopher = "pocket"
usage = "the variety of gopher"
)
flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
}
// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
return fmt.Sprint(*i)
}
// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *interval) Set(value string) error {
// If we wanted to allow the flag to be set multiple times,
// accumulating values, we would delete this if statement.
// That would permit usages such as
// -deltaT 10s -deltaT 15s
// and other combinations.
if len(*i) > 0 {
return errors.New("interval flag already set")
}
for _, dt := range strings.Split(value, ",") {
duration, err := time.ParseDuration(dt)
if err != nil {
return err
}
*i = append(*i, duration)
}
return nil
}
// Define a flag to accumulate durations. Because it has a special type,
// we need to use the Var function and therefore create the flag during
// init.
var intervalFlag interval
func init() {
// Tie the command-line flag to the intervalFlag variable and
// set a usage message.
flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
}
func Example() {
// All the interesting pieces are with the variables declared above, but
// to enable the flag package to see the flags defined there, one must
// execute, typically at the start of main (not init!):
// flag.Parse()
// We don't run it here because this is not a main function and
// the testing suite has already parsed the flags.
}