add ability to parse []string types

This commit is contained in:
Thesyncim
2013-09-24 02:41:31 +01:00
parent e8d8047f49
commit 4d9038a156
3 changed files with 54 additions and 8 deletions

34
flag.go
View File

@@ -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