flag
23 分钟阅读
Package flag implements command-line flag parsing.
flag
包实现了命令行标志解析。
Usage
Define flags using flag.String(), Bool(), Int(), etc.
使用 flag.String()、Bool()、Int() 等函数定义标志。
This declares an integer flag, -n, stored in the pointer nFlag, with type *int:
下面的代码声明了一个整数标志 -n,存储在指针 nFlag 中,类型为 *int
:
|
|
If you like, you can bind the flag to a variable using the Var() functions.
如果您喜欢,可以使用 Var() 函数将标志绑定到变量。
|
|
Or you can create custom flags that satisfy the Value interface (with pointer receivers) and couple them to flag parsing by
或者,您可以创建满足 Value 接口(使用指针接收器)的自定义标志,并将它们与标志解析耦合:
flag.Var(&flagVal, "name", "help message for flagname")
For such flags, the default value is just the initial value of the variable.
对于这种标志,默认值就是变量的初始值。
After all flags are defined, call
在定义好所有标志之后,调用
flag.Parse()
to parse the command line into the defined flags.
将命令行参数解析为定义好的标志。
Flags may then be used directly. If you’re using the flags themselves, they are all pointers; if you bind to variables, they’re values.
然后可以直接使用标志。如果使用标志本身,它们都是指针;如果与变量绑定,它们就是值。
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments following the flags are available as the slice flag.Args() or individually as flag.Arg(i). The arguments are indexed from 0 through flag.NArg()-1.
解析后,跟在标志后面的参数可以作为 flag.Args()
切片使用,也可以单独作为 flag.Arg(i) 使用。参数的索引从 0 到 flag.NArg()-1
。
Command line flag syntax
The following forms are permitted:
以下形式是允许的:
-flag
--flag // 也允许双短线
-flag=x
-flag x // 只适用于非布尔标志
One or two dashes may be used; they are equivalent. The last form is not permitted for boolean flags because the meaning of the command
可以使用一个或两个短线,它们是等价的。最后一种形式不适用于布尔标志,因为命令
cmd -x *
where * is a Unix shell wildcard, will change if there is a file called 0, false, etc. You must use the -flag=false form to turn off a boolean flag.
其中 *
是 Unix shell 通配符,如果存在名为 0
、false
等的文件,其含义将发生变化。您必须使用 -flag=false 形式来关闭布尔标志。
Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator “–”.
标志解析在第一个非标志参数("-“是非标志参数)之前或终止符”–“之后停止。
Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags may be:
整数标志接受 1234、0664、0x1234 等值,也可以是负数。布尔标志可以是:
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
Duration flags accept any input valid for time.ParseDuration.
时间间隔标志接受任何符合 time.ParseDuration 的输入。
The default set of command-line flags is controlled by top-level functions. The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of FlagSet are analogous to the top-level functions for the command-line flag set.
默认的命令行标志集由顶级函数控制。FlagSet 类型允许定义独立的标志集,例如实现命令行接口中的子命令。FlagSet 的方法类似于命令行标志集的顶级函数。
Example
|
|
常量
This section is empty.
变量
|
|
CommandLine is the default set of command-line flags, parsed from os.Args. The top-level functions such as BoolVar, Arg, and so on are wrappers for the methods of CommandLine.
CommandLine 是默认的命令行标志集,从 os.Args 解析而来。诸如 BoolVar、Arg 等顶级函数是 CommandLine 方法的包装器。
|
|
ErrHelp is the error returned if the -help or -h flag is invoked but no such flag is defined.
ErrHelp 是在调用 -help 或 -h 标志,但没有定义这样的标志时返回的错误。
|
|
Usage prints a usage message documenting all defined command-line flags to CommandLine’s output, which by default is os.Stderr. It is called when an error occurs while parsing flags. The function is a variable that may be changed to point to a custom function. By default it prints a simple header and calls PrintDefaults; for details about the format of the output and how to control it, see the documentation for PrintDefaults. Custom usage functions may choose to exit the program; by default exiting happens anyway as the command line’s error handling strategy is set to ExitOnError.
Usage 打印一个使用说明,文档化所有已定义的命令行标志到 CommandLine 的输出,这个输出默认是 os.Stderr。当解析标志时发生错误时会调用它。该函数是一个变量,可以更改指向自定义函数。默认情况下,它会打印一个简单的标题并调用 PrintDefaults;关于输出格式及如何控制它的详细信息,请参阅 PrintDefaults 的文档。自定义的使用函数可能选择退出程序;默认情况下,退出仍会发生,因为命令行的错误处理策略设置为 ExitOnError
。
函数
func Arg
|
|
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.
Arg函数返回第 i 个命令行参数。Arg(0) 是在处理标志后剩下的第一个参数。如果请求的元素不存在,则 Arg 返回空字符串。
func Args
|
|
Args returns the non-flag command-line arguments.
Args函数返回非标志命令行参数。
func 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.
Bool函数定义一个具有指定名称、默认值和用法字符串的布尔标志。返回值是存储标志值的布尔变量的地址。
func BoolFunc <- go1.21.0
|
|
BoolFunc defines a flag with the specified name and usage string without requiring values. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.
BoolFunc
定义了一个具有指定名称和使用说明的标志,无需提供值。每次看到该标志时,都会调用 fn
并传递该标志的值。如果 fn
返回非空错误,则会将其视为标志值解析错误。
BoolFunc Example
|
|
func BoolVar
|
|
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.
BoolVar函数定义一个具有指定名称、默认值和用法字符串的布尔标志。参数 p 指向一个布尔变量,用于存储标志的值。
func Duration
|
|
Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
Duration函数定义一个具有指定名称、默认值和用法字符串的 time.Duration 标志。返回值是存储标志值的 time.Duration 变量的地址。标志接受符合 time.ParseDuration 的值。
func DurationVar
|
|
DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
DurationVar函数定义一个具有指定名称、默认值和用法字符串的 time.Duration 标志。参数 p 指向一个 time.Duration 变量,用于存储标志的值。标志接受符合 time.ParseDuration 的值。
func Float64
|
|
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
Float64函数定义一个具有指定名称、默认值和用法字符串的 float64 标志。返回值是存储标志值的 float64 变量的地址。
func Float64Var
|
|
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
Float64Var函数定义一个具有指定名称、默认值和用法字符串的 float64 标志。参数 p 指向一个 float64 变量,用于存储标志的值。
func Func <- go1.16
|
|
Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.
Func函数定义一个具有指定名称和用法字符串的标志。每次看到标志时,fn 都会使用标志的值调用。如果 fn 返回非 nil 错误,则将其视为标志值解析错误。
Func Example
|
|
func Int
|
|
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
Int函数定义一个具有指定名称、默认值和用法字符串的 int 标志。返回值是存储标志值的 int 变量的地址。
func 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.
Int64函数定义一个具有指定名称、默认值和用法字符串的 int64 标志。返回值是存储标志值的 int64 变量的地址。
func Int64Var
|
|
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
Int64Var函数定义一个具有指定名称、默认值和用法字符串的 int64 标志。参数 p 指向一个 int64 变量,用于存储标志的值。
func IntVar
|
|
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.
IntVar函数定义一个具有指定名称、默认值和用法字符串的 int 标志。参数 p 指向一个 int 变量,用于存储标志的值。
func NArg
|
|
NArg is the number of arguments remaining after flags have been processed.
NArg函数返回在处理标志后剩余的参数数目。
func NFlag
|
|
NFlag returns the number of command-line flags that have been set.
NFlag函数返回已设置的命令行标志数目。
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.
Parse函数从 os.Args[1:]
解析命令行标志。必须在定义所有标志并在程序访问标志之前调用它。
func Parsed
|
|
Parsed reports whether the command-line flags have been parsed.
Parsed函数报告命令行标志是否已被解析。
func PrintDefaults
|
|
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined command-line flags. For an integer valued flag x, the default output has the form
PrintDefaults函数打印一个使用说明,显示所有已定义命令行标志的默认设置。对于值为整数的标志 x,默认输出的格式为:
-x int
usage-message-for-x (default 7)
The usage message will appear on a separate line for anything but a bool flag with a one-byte name. For bool flags, the type is omitted and if the flag name is one byte the usage message appears on the same line. The parenthetical default is omitted if the default is the zero value for the type. The listed type, here int, can be changed by placing a back-quoted name in the flag’s usage string; the first such item in the message is taken to be a parameter name to show in the message and the back quotes are stripped from the message when displayed. For instance, given
使用方法会在除了具有一个字节名称的 bool 标志外的其他地方显示在单独的一行上。对于 bool 标志,类型被省略,如果标志名称为一个字节,则使用说明消息出现在同一行上。如果默认值是类型的零值,则括号中的默认值被省略。列出的类型(这里是 int)可以通过在标志的使用说明字符串中放置一个带反引号的名称来更改;消息中的第一个此类项被认为是要在消息中显示的参数名称,并在显示消息时从消息中删除反引号。例如,给定
flag.String("I", "", "search `directory` for include files")
the output will be
输出将是
-I directory
search directory for include files.
To change the destination for flag messages, call CommandLine.SetOutput.
要更改标志消息的目标,请调用 CommandLine.SetOutput。
func Set
|
|
Set sets the value of the named command-line flag.
Set函数设置命名的命令行标志的值。
func 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.
String函数定义一个具有指定名称、默认值和用法字符串的字符串标志。返回值是存储标志值的字符串变量的地址。
func StringVar
|
|
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.
StringVar函数定义一个具有指定名称、默认值和用法字符串的字符串标志。参数 p 指向一个字符串变量,用于存储标志的值。
func TextVar <- go1.19
|
|
TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p’s UnmarshalText method. The type of the default value must be the same as the type of p.
TextVar函数定义一个具有指定名称、默认值和用法字符串的标志。参数 p 必须是一个指向将保存标志值的变量的指针,并且 p 必须实现 encoding.TextUnmarshaler 接口。如果使用标志,则将标志值传递给 p 的 UnmarshalText 方法。默认值的类型必须与 p 的类型相同。
TextVar Example
|
|
func Uint
|
|
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
Uint函数定义一个具有指定名称、默认值和用法字符串的 uint 标志。返回值是存储标志值的 uint 变量的地址。
func Uint64
|
|
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
Uint64函数定义一个指定名称、默认值和使用说明的 uint64 标志。返回值是一个 uint64 变量的地址,该变量存储标志的值。
func Uint64Var
|
|
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
Uint64Var函数定义一个指定名称、默认值和使用说明的 uint64 标志。参数 p 指向一个 uint64 变量,用于存储标志的值。
func UintVar
|
|
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
UintVar函数定义一个指定名称、默认值和使用说明的 uint 标志。参数 p 指向一个 uint 变量,用于存储标志的值。
func UnquoteUsage <- go1.5
|
|
UnquoteUsage extracts a back-quoted name from the usage string for a flag and returns it and the un-quoted usage. Given “a name
to show” it returns (“name”, “a name to show”). If there are no back quotes, the name is an educated guess of the type of the flag’s value, or the empty string if the flag is boolean.
UnquoteUsage函数从标志的使用说明中提取带反引号的名称,并返回未引用的名称和使用说明。给定 “a name
to show”,返回 (“name”, “a name to show”)。如果没有反引号,则名称是标志值类型的猜测,如果标志是布尔值,则为空字符串。
func Var
|
|
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Var函数定义一个具有指定名称和使用说明的标志。标志的类型和值由第一个参数 Value 表示,该参数通常持有 Value 的用户定义实现。例如,调用者可以创建一个标志,将逗号分隔的字符串转换为字符串切片,给定的切片具有 Value 的方法;特别是,Set 会将逗号分隔的字符串分解为切片。
func Visit
|
|
Visit visits the command-line flags in lexicographical order, calling fn for each. It visits only those flags that have been set.
Visit函数按字典顺序访问命令行标志,并为每个标志调用 fn。它仅访问已设置的标志。
func VisitAll
|
|
VisitAll visits the command-line flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
VisitAll函数按字典顺序访问命令行标志,并为每个标志调用 fn。它访问所有标志,即使未设置。
类型
type ErrorHandling
|
|
ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
ErrorHandling定义了FlagSet.Parse在解析失败时的行为。
|
|
These constants cause FlagSet.Parse to behave as described if the parse fails.
如果解析失败,这些常量会使FlagSet.Parse按照描述的行为进行操作。
type Flag
|
|
A Flag represents the state of a flag.
Flag表示标志的状态。
func Lookup
|
|
Lookup returns the Flag structure of the named command-line flag, returning nil if none exists.
Lookup函数返回命名的命令行标志的Flag结构,如果不存在则返回nil。
type FlagSet
|
|
A FlagSet represents a set of defined flags. The zero value of a FlagSet has no name and has ContinueOnError error handling.
FlagSet表示一组已定义的标志。FlagSet的零值没有名称并具有ContinueOnError错误处理。
Flag names must be unique within a FlagSet. An attempt to define a flag whose name is already in use will cause a panic.
标志名称必须在FlagSet内唯一。尝试定义其名称已在使用中的标志将导致恐慌。
func NewFlagSet
|
|
NewFlagSet returns a new, empty flag set with the specified name and error handling property. If the name is not empty, it will be printed in the default usage message and in error messages.
NewFlagSet函数返回一个新的、空的FlagSet,其中包含指定的名称和错误处理属性。如果名称不为空,它将在默认用法消息和错误消息中打印。
(*FlagSet) Arg
|
|
Arg returns the i’th 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.
Arg方法返回第i个参数。Arg(0)是在处理标志后剩余的第一个参数。如果请求的元素不存在,则Arg返回空字符串。
(*FlagSet) Args
|
|
Args returns the non-flag arguments.
Args方法返回非标志参数。
(*FlagSet) 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.
Bool方法定义具有指定名称、默认值和用法字符串的布尔标志。返回值是一个bool变量的地址,该变量存储标志的值。
(*FlagSet) BoolFunc <-go1.21.0
|
|
BoolFunc defines a flag with the specified name and usage string without requiring values. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.
(*FlagSet) BoolVar
|
|
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.
BoolVar方法定义具有指定名称、默认值和用法字符串的布尔标志。参数p指向一个bool变量,用于存储标志的值。
(*FlagSet) Duration
|
|
Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
Duration方法定义具有指定名称、默认值和用法字符串的time.Duration标志。返回值是一个time.Duration变量的地址,该变量存储标志的值。该标志接受一个可接受的time.ParseDuration值。
(*FlagSet) DurationVar
|
|
DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
DurationVar方法定义具有指定名称、默认值和用法字符串的time.Duration标志。参数p指向一个time.Duration变量,用于存储标志的值。该标志接受一个可接受的time.ParseDuration值。
(*FlagSet) ErrorHandling <- go1.10
|
|
ErrorHandling returns the error handling behavior of the flag set.
ErrorHandling方法返回标志集的错误处理行为。
(*FlagSet) Float64
|
|
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
Float64方法定义了一个指定名称、默认值和用法说明的 float64 标志(flag)。返回值是一个 float64 变量的地址,该变量存储标志的值。
(*FlagSet) Float64Var
|
|
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
Float64Var方法定义了一个指定名称、默认值和用法说明的 float64 标志。参数 p 是一个指向 float64 变量的指针,用于存储标志的值。
(*FlagSet) Func <- go1.16
|
|
Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.
Func方法定义了一个指定名称和用法说明的标志。每次遇到该标志时,都会将标志的值传递给 fn 函数。如果 fn 函数返回一个非 nil 错误,则将其视为标志值解析错误。
(*FlagSet) Init
|
|
Init sets the name and error handling property for a flag set. By default, the zero FlagSet uses an empty name and the ContinueOnError error handling policy.
Init方法为一个标志集设置名称和错误处理属性。默认情况下,零值 FlagSet 使用一个空名称和 ContinueOnError 错误处理策略。
(*FlagSet) Int
|
|
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
Int方法定义了一个指定名称、默认值和用法说明的 int 标志。返回值是一个 int 变量的地址,该变量存储标志的值。
(*FlagSet) 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.
Int64 函数定义了一个指定名称、默认值和用法说明的 int64 标志。返回值是一个 int64 变量的地址,该变量存储标志的值。
(*FlagSet) Int64Var
|
|
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
Int64Var方法定义了一个指定名称、默认值和用法说明的 int64 标志。参数 p 是一个指向 int64 变量的指针,用于存储标志的值。
(*FlagSet) IntVar
|
|
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.
IntVar方法定义了一个指定名称、默认值和用法说明的 int 标志。参数 p 是一个指向 int 变量的指针,用于存储标志的值。
(*FlagSet) Lookup
|
|
Lookup returns the Flag structure of the named flag, returning nil if none exists.
Lookup方法返回指定名称的标志(flag)结构体,如果不存在则返回nil。
(*FlagSet) NArg
|
|
NArg is the number of arguments remaining after flags have been processed.
NArg方法返回处理完flag后剩余的参数个数。
(*FlagSet) NFlag
|
|
NFlag returns the number of flags that have been set.
NFlag方法返回已设置的flag的数量。
(*FlagSet) Name <- go1.10
|
|
Name returns the name of the flag set.
Name返回设置的标志的名称。
Name方法返回flag set的名称。
(*FlagSet) Output <- go1.10
|
|
Output returns the destination for usage and error messages. os.Stderr is returned if output was not set or was set to nil.
Output方法返回用于使用说明和错误信息的目标io.Writer。如果未设置或设置为nil,则返回os.Stderr。
(*FlagSet) Parse
|
|
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help or -h were set but not defined.
Parse方法从参数列表中解析flag定义,该列表不应包括命令名称。必须在FlagSet中定义所有flag并在程序访问flag之前调用。如果设置了但未定义-help或-h,则返回ErrHelp。
(*FlagSet) Parsed
|
|
Parsed reports whether f.Parse has been called.
Parsed方法返回是否已调用f.Parse。
(*FlagSet) PrintDefaults
|
|
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set. See the documentation for the global function PrintDefaults for more information.
PrintDefaults方法将标志集合中所有定义的命令行标志的默认值打印到标准错误(除非另有配置)。有关更多信息,请参见全局函数PrintDefaults的文档。
(*FlagSet) Set
|
|
Set sets the value of the named flag.
Set方法设置指定名称的flag的值。
(*FlagSet) SetOutput
|
|
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
SetOutput方法设置用于帮助信息和错误信息输出的目标。如果output为nil,则使用os.Stderr。
(*FlagSet) 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.
String方法定义了一个指定名称、默认值和用法说明的字符串标志。返回值是一个string类型变量的地址,该变量存储标志的值。
(*FlagSet) StringVar
|
|
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.
StringVar方法定义了一个指定名称、默认值和用法说明的字符串标志。参数p指向一个string类型的变量,用于存储标志的值。
(*FlagSet) TextVar <- go1.19
|
|
TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p’s UnmarshalText method. The type of the default value must be the same as the type of p.
TextVar方法定义了一个指定名称、默认值和用法说明的标志。参数p必须是一个指向将保存标志值的变量的指针,p必须实现encoding.TextUnmarshaler接口。如果使用了该标志,则标志值将传递给p的UnmarshalText方法。默认值的类型必须与p的类型相同。
(*FlagSet) Uint
|
|
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
Uint方法定义了一个指定名称、默认值和用法说明的uint标志。返回值是一个uint变量的地址,该变量存储标志的值。
(*FlagSet) Uint64
|
|
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
Uint64方法定义了一个指定名称、默认值和用法说明的uint64标志。返回值是一个uint64变量的地址,该变量存储标志的值。
(*FlagSet) Uint64Var
|
|
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
Uint64Var方法定义了一个指定名称、默认值和用法说明的uint64标志。参数p指向一个uint64类型的变量,用于存储标志的值。
(*FlagSet) UintVar
|
|
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
UintVar方法定义了一个指定名称、默认值和用法说明的uint标志。参数p指向一个uint类型的变量,用于存储标志的值。
(*FlagSet) Var
|
|
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Var方法定义了一个指定名称和用法说明的标志。标志的类型和值由第一个参数Value表示,通常Value持有一个用户定义的实现。例如,调用方可以创建一个标志,通过给该slice的方法提供Value,将逗号分隔的字符串转换为字符串的slice;特别地,Set会将逗号分隔的字符串分解为slice。
(*FlagSet) Visit
|
|
Visit visits the flags in lexicographical order, calling fn for each. It visits only those flags that have been set.
Visit方法按词典顺序访问设置了值的标志,对于每个标志调用fn函数。它只访问已设置的标志。
(*FlagSet) VisitAll
|
|
VisitAll visits the flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
VisitAll方法按词典顺序访问所有标志,对于每个标志调用fn函数,即使标志未设置也会访问。
type Getter <- go1.2
|
|
Getter is an interface that allows the contents of a Value to be retrieved. It wraps the Value interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All Value types provided by this package satisfy the Getter interface, except the type used by Func.
Getter是一个接口,允许检索存储在标志中的值。它包装了Value接口,而不是作为其一部分,因为它出现在Go 1之后,并具有其兼容性规则。由此包提供的所有Value类型都满足Getter接口,除了Func使用的类型。
type Value
|
|
Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)
Value是标志中存储的动态值的接口(默认值表示为字符串)。
If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.
如果Value具有IsBoolFlag() bool方法返回true,则命令行解析器将-name等同于-name=true,而不是使用下一个命令行参数。
Set is called once, in command line order, for each flag present. The flag package may call the String method with a zero-valued receiver, such as a nil pointer.
对于每个存在的标志,Set在命令行顺序中调用一次。标志包可以使用零值接收器(如nil指针)调用String方法。
Value Example
|
|