kit/cli: Add support for []string

pull/10616/head
Jacob Marble 2018-10-03 11:24:46 -07:00
parent 709310d05e
commit fb4cd36e9b
2 changed files with 16 additions and 0 deletions

View File

@ -87,6 +87,13 @@ func NewCommand(p *Program) *cobra.Command {
cmd.Flags().DurationVar(o.DestP.(*time.Duration), o.Flag, o.Default.(time.Duration), o.Desc)
viper.BindPFlag(o.Flag, cmd.Flags().Lookup(o.Flag))
*o.DestP.(*time.Duration) = viper.GetDuration(o.Flag)
case *[]string:
if o.Default == nil {
o.Default = []string{}
}
cmd.Flags().StringSliceVar(o.DestP.(*[]string), o.Flag, o.Default.([]string), o.Desc)
viper.BindPFlag(o.Flag, cmd.Flags().Lookup(o.Flag))
*o.DestP.(*[]string) = viper.GetStringSlice(o.Flag)
default:
// if you get a panic here, sorry about that!
// anyway, go ahead and make a PR and add another type.

View File

@ -11,6 +11,7 @@ func ExampleNewCommand() {
var number int
var sleep bool
var duration time.Duration
var stringSlice []string
cmd := NewCommand(&Program{
Run: func() error {
fmt.Println(monitorHost)
@ -19,6 +20,7 @@ func ExampleNewCommand() {
}
fmt.Println(sleep)
fmt.Println(duration)
fmt.Println(stringSlice)
return nil
},
Name: "myprogram",
@ -47,6 +49,12 @@ func ExampleNewCommand() {
Default: time.Minute,
Desc: "how long to sleep",
},
{
DestP: &stringSlice,
Flag: "string-slice",
Default: []string{"foo", "bar"},
Desc: "things come in lists",
},
},
})
@ -59,4 +67,5 @@ func ExampleNewCommand() {
// 1
// true
// 1m0s
// [foo bar]
}