Go Flag 用法说明 包含 Word 值

Go Flag Usage Description Contains the Word value

我定义了一个自定义标志来接受一段字符串:

type strSliceFlag []string

func (i *strSliceFlag) String() string {
    return fmt.Sprint(*i)
}

func (i *strSliceFlag) Set(value string) error {
    *i = append(*i, value)
    return nil
}

然后我用

解析它
    ...
    var tags strSliceFlag
    flag.Var(&tags, "t", tFlagExpl)
    flag.Parse()
    ...

当我构建这个程序时,运行 它带有帮助标志:main -h,它打印出:

Usage of main:
  -t value
        Test explanation

我的问题是,value这个词是从哪里来的?我找不到如何删除它。我认为这可能与标志的默认值有关。

valueflag.UnquoteUsage for custom types (rendered via flag.(*FlagSet).PrintDefaults) 选择的默认参数名称。

您可以在使用文本中使用反引号覆盖默认值。反引号从使用文本中删除。例如:

package main

import (
    "flag"
    "fmt"
)

type stringSlice []string

func (s *stringSlice) String() string {
    return fmt.Sprint(*s)
}

func (s *stringSlice) Set(v string) error {
    *s = append(*s, v)
    return nil
}

func main() {
    var s stringSlice
    flag.Var(&s, "foo", "append a foo to the list")
    flag.Var(&s, "foo2", "append a `foo` to the list")
    flag.Parse()
}

运行 -h 显示参数名称如何变化:

Usage of ./flagusage:
  -foo value
        append a foo to the list
  -foo2 foo
        append a foo to the list