feat(cli): Extend flag support to pflag.Value types

Allows custom types that implement the pflag.Value interface,
which simplifies validation and error handling of flag processing.
pull/17102/head
Stuart Carnie 2020-03-05 08:21:13 -07:00
parent b1b2444fac
commit 9335800af1
No known key found for this signature in database
GPG Key ID: 848D9C9718D78B4F
1 changed files with 11 additions and 0 deletions

View File

@ -150,6 +150,17 @@ func BindOptions(cmd *cobra.Command, opts []Opt) {
}
mustBindPFlag(o.Flag, flagset)
*destP = viper.GetStringSlice(envVar)
case pflag.Value:
if hasShort {
flagset.VarP(destP, o.Flag, string(o.Short), o.Desc)
} else {
flagset.Var(destP, o.Flag, o.Desc)
}
if o.Default != nil {
destP.Set(o.Default.(string))
}
mustBindPFlag(o.Flag, flagset)
destP.Set(viper.GetString(envVar))
default:
// if you get a panic here, sorry about that!
// anyway, go ahead and make a PR and add another type.