go - Cobra 允许的标志值

标签 go go-cobra

Cobra 库中是否有内置工具(如果有,我该如何使用它?)来要求标志是多个值之一,并在标志不是允许值之一时抛出错误?我没有在 Github 页面上看到这个。

最佳答案

Cobra 允许您通过 pflag.(*FlagSet).Var() 定义自定义值类型以用作标志。方法(来自 Cobra 使用的 https://github.com/spf13/pflag 包)。您必须创建一个实现 pflag.Value 的新类型接口(interface):

type Value interface {
    String() string
    Set(string) error
    Type() string
}

示例类型定义:

type myEnum string

const (
    myEnumFoo myEnum = "foo"
    myEnumBar myEnum = "bar"
    myEnumMoo myEnum = "moo"
)

// String is used both by fmt.Print and by Cobra in help text
func (e *myEnum) String() string {
    return string(*e)
}

// Set must have pointer receiver so it doesn't change the value of a copy
func (e *myEnum) Set(v string) error {
    switch v {
    case "foo", "bar", "moo":
        *e = myEnum(v)
        return nil
    default:
        return errors.New(`must be one of "foo", "bar", or "moo"`)
    }
}

// Type is only used in help text
func (e *myEnum) Type() string {
    return "myEnum"
}

注册示例:

func init() {
    var flagMyEnum = myEnumFoo

    var myCmd = &cobra.Command{
        Use:   "mycmd",
        Short: "A brief description of your command",
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("myenum value:", flagMyEnum)
        },
    }

    rootCmd.AddCommand(myCmd)

    myCmd.Flags().Var(&flagMyEnum, "myenum", `my custom enum. allowed: "foo", "bar", "moo"`)
}

示例用法:(注意下面控制台输出中的第一行)

$ go run . mycmd --myenum raz
Error: invalid argument "raz" for "--myenum" flag: must be one of "foo", "bar", or "moo"
Usage:
  main mycmd [flags]

Flags:
  -h, --help            help for mycmd
      --myenum myEnum   my custom enum. allowed: "foo", "bar", "moo" (default foo)

exit status 1

$ go run . mycmd --myenum bar
myenum value: bar

完成

要为此添加自动完成功能,有点隐藏的文档页面 cobra/shell_completions.md#completions-for-flags有很大的帮助。对于我们的示例,您将添加如下内容:

func init() {
    // ...

    myCmd.Flags().Var(&flagMyEnum, "myenum", `my custom enum. allowed: "foo", "bar", "moo"`)

    myCmd.RegisterFlagCompletionFunc("myenum", myEnumCompletion)
}

// myEnumCompletion should probably live next to the myEnum definition
func myEnumCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    return []string{
        "foo\thelp text for foo",
        "bar\thelp text for bar",
        "moo\thelp text for moo",
    }, cobra.ShellCompDirectiveDefault
}

示例用法:

$ go build -o main .

$ source <(main completion bash)

$ main mycmd --myenum <TAB><TAB>
bar  (help text for bar)
foo  (help text for foo)
moo  (help text for moo)

关于go - Cobra 允许的标志值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824554/

相关文章:

heroku - 使用本地包的部署构建失败并显示 "import path does not begin with hostname"

file - 如何使用 Go 字节有效地替换两个字符串分隔符之间出现的字符串?

go - 为什么我会根据调用 BindPFlag 的位置收到零指针错误?

go - 眼镜蛇go cli库忽略标志

go - 如何防止负整数被视为简写标志

go - 如何在 Go 中使用 cobra 库在一行中接受输入

encryption - 从 PHP 到 Go 的 Mcrypt

go - 构建接口(interface)

go - cobra 和 viper 的配置文件

go - 通过引用分配