1. Home
  2. Docs
  3. golang
  4. 内置库(包-package)
  5. flag

flag

包flag实现了命令行flag的解析。

Variables

CommandLine

var CommandLine = NewFlagSet(os.Args[0], ExitOnError)


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方法的封装。

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")

flag.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标志。返回值是一个存储该标志值的bool变量的地址。

flag.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变量的地址。

flag.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:] 中解析命令行标志。必须在所有标志被定义后,在标志被程序访问前被调用。

Was this article helpful to you? Yes No

How can we help?