2019-05-22 21:16:55 +00:00
|
|
|
package flagutil
|
|
|
|
|
2019-06-18 07:26:44 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
// NewArray returns new Array with the given name and description.
|
2019-06-18 07:26:44 +00:00
|
|
|
func NewArray(name, description string) *Array {
|
|
|
|
var a Array
|
|
|
|
flag.Var(&a, name, description)
|
|
|
|
return &a
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Array holds an array of flag values
|
|
|
|
type Array []string
|
|
|
|
|
|
|
|
// String implements flag.Value interface
|
|
|
|
func (a *Array) String() string {
|
|
|
|
return strings.Join(*a, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements flag.Value interface
|
|
|
|
func (a *Array) Set(value string) error {
|
|
|
|
values := strings.Split(value, ",")
|
|
|
|
*a = append(*a, values...)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-06 13:51:32 +00:00
|
|
|
|
|
|
|
// GetOptionalArg returns optional arg under the given argIdx.
|
|
|
|
func (a *Array) GetOptionalArg(argIdx int) string {
|
|
|
|
x := *a
|
|
|
|
if argIdx >= len(x) {
|
|
|
|
if len(x) == 1 {
|
|
|
|
return x[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return x[argIdx]
|
|
|
|
}
|