add ability to parse []string types
This commit is contained in:
34
flag.go
34
flag.go
@@ -8,14 +8,48 @@ type Flag interface {
|
||||
Apply(*flag.FlagSet)
|
||||
}
|
||||
|
||||
type SliceFlag interface {
|
||||
Value() []string
|
||||
}
|
||||
|
||||
func flagSet(name string, flags []Flag) *flag.FlagSet {
|
||||
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
||||
|
||||
for _, f := range flags {
|
||||
f.Apply(set)
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
type StringSlice []string
|
||||
|
||||
func (i *StringSlice) Set(value string) error {
|
||||
*i = append(*i, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *StringSlice) String() string {
|
||||
return fmt.Sprintf("%s", *i)
|
||||
}
|
||||
|
||||
func (i *StringSlice) Value() []string {
|
||||
return *i
|
||||
}
|
||||
|
||||
type StringSliceFlag struct {
|
||||
Name string
|
||||
Value *StringSlice
|
||||
Usage string
|
||||
}
|
||||
|
||||
func (f StringSliceFlag) String() string {
|
||||
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, "-"+f.Name+" option -"+f.Name+" option", f.Usage)
|
||||
}
|
||||
|
||||
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
||||
set.Var(f.Value, f.Name, f.Usage)
|
||||
}
|
||||
|
||||
type BoolFlag struct {
|
||||
Name string
|
||||
Usage string
|
||||
|
Reference in New Issue
Block a user