Command-Line Flags {.en}

命令行标志 {.zh}

::: {.en} Command-line flags are a common way to specify options for command-line programs. For example, in wc -l the -l is a command-line flag. :::

::: {.zh}

[Command-line flags](http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option)是指定命令行程序选项的常用方法。例如,在`wc -l中,-l`是acommand-line标志。

:::

  1. package main

::: {.en} Go provides a flag package supporting basic command-line flag parsing. We’ll use this package to implement our example command-line program. :::

::: {.zh}

Go提供了一个支持basiccommand-line标志解析的flag包。我们将使用此包来实现我们的示例命令行程序。

:::

  1. import "flag"
  2. import "fmt"
  3. func main() {

::: {.en} Basic flag declarations are available for string, integer, and boolean options. Here we declare a string flag word with a default value "foo" and a short description. This flag.String function returns a string pointer (not a string value); we’ll see how to use this pointer below. :::

::: {.zh}

基本标志声明可用于字符串,整数和布尔选项。这里我们声明astring标志word,默认值为“foo”和简短描述。这个flag.String函数返回一个字符串指针(不是字符串值);我们将看到如何在下面使用这个指针。

:::

  1. wordPtr := flag.String("word", "foo", "a string")

::: {.en} This declares numb and fork flags, using a similar approach to the word flag. :::

::: {.zh}

这声明了numbfork标志,使用与word标志类似的方法。

:::

  1. numbPtr := flag.Int("numb", 42, "an int")
  2. boolPtr := flag.Bool("fork", false, "a bool")

::: {.en} It’s also possible to declare an option that uses an existing var declared elsewhere in the program. Note that we need to pass in a pointer to the flag declaration function. :::

::: {.zh}

也可以声明一个使用在程序中其他地方声明的现有var的选项。注意我们需要传入一个指向flagdeclaration函数的指针。

:::

  1. var svar string
  2. flag.StringVar(&svar, "svar", "bar", "a string var")

::: {.en} Once all flags are declared, call flag.Parse() to execute the command-line parsing. :::

::: {.zh}

声明所有标志后,调用flag.Parse()来执行命令行解析。

:::

  1. flag.Parse()

::: {.en} Here we’ll just dump out the parsed options and any trailing positional arguments. Note that we need to dereference the pointers with e.g. *wordPtr to get the actual option values. :::

::: {.zh}

在这里,我们只会转出已解析的选项和任何尾随位置参数。请注意,我们需要取消引用指针,例如* wordPtr获取实际选项值。

:::

  1. fmt.Println("word:", *wordPtr)
  2. fmt.Println("numb:", *numbPtr)
  3. fmt.Println("fork:", *boolPtr)
  4. fmt.Println("svar:", svar)
  5. fmt.Println("tail:", flag.Args())
  6. }

::: {.en} To experiment with the command-line flags program it’s best to first compile it and then run the resulting binary directly. :::

::: {.zh}

要试验命令行标志程序,首先要编译它然后直接运行结果二进制文件。

:::

  1. $ go build command-line-flags.go

::: {.en} Try out the built program by first giving it values for all flags. :::

::: {.zh}

通过首先给它值forall标志来尝试构建的程序。

:::

  1. $ ./command-line-flags -word=opt -numb=7 -fork -svar=flag
  2. word: opt
  3. numb: 7
  4. fork: true
  5. svar: flag
  6. tail: []

::: {.en} Note that if you omit flags they automatically take their default values. :::

::: {.zh}

请注意,如果省略标记,它们会自动获取默认值。

:::

  1. $ ./command-line-flags -word=opt
  2. word: opt
  3. numb: 42
  4. fork: false
  5. svar: bar
  6. tail: []

::: {.en} Trailing positional arguments can be provided after any flags. :::

::: {.zh}

可以在后面的标志中提供尾随位置参数。

:::

  1. $ ./command-line-flags -word=opt a1 a2 a3
  2. word: opt
  3. ...
  4. tail: [a1 a2 a3]

::: {.en} Note that the flag package requires all flags to appear before positional arguments (otherwise the flags will be interpreted as positional arguments). :::

::: {.zh}

请注意,flag包需要在位置参数之前出现所有标志(否则标志将被解释为位置参数)。

:::

  1. $ ./command-line-flags -word=opt a1 a2 a3 -numb=7
  2. word: opt
  3. numb: 42
  4. fork: false
  5. svar: bar
  6. tail: [a1 a2 a3 -numb=7]

::: {.en} Use -h or --help flags to get automatically generated help text for the command-line program. :::

::: {.zh}

使用-h--help标志来获取命令行程序的自动生成的帮助文本。

:::

  1. $ ./command-line-flags -h
  2. Usage of ./command-line-flags:
  3. -fork=false: a bool
  4. -numb=42: an int
  5. -svar="bar": a string var
  6. -word="foo": a string

::: {.en} If you provide a flag that wasn’t specified to the flag package, the program will print an error message and show the help text again. :::

::: {.zh}

如果您提供未指定给flag包的标志,程序将打印错误消息并再次显示帮助文本。

:::

  1. $ ./command-line-flags -wat
  2. flag provided but not defined: -wat
  3. Usage of ./command-line-flags:
  4. ...

::: {.en} Next we’ll look at environment variables, another common way to parameterize programs. :::

::: {.zh}

接下来我们将看一下环境变量,这是另一个参数化程序的常用方法。

:::