Generic parsers as flag types

This commit is contained in:
Summer Mousa
2014-04-15 09:16:47 -05:00
parent 640826c88f
commit 13e88629f5
3 changed files with 88 additions and 1 deletions

28
flag.go
View File

@@ -37,6 +37,34 @@ func eachName(longName string, fn func(string)) {
}
}
// Generic is a generic parseable type identified by a specific flag
type Generic interface {
Set(value string) error
String() string
Value() interface{}
}
// GenericFlag is the flag type for types implementing Generic
type GenericFlag struct {
Name string
Value Generic
Usage string
}
func (f GenericFlag) String() string {
return fmt.Sprintf("%s%s %v\t`%v` %s", prefixFor(f.Name), f.Name, f.Value, "-"+f.Name+" option -"+f.Name+" option", f.Usage)
}
func (f GenericFlag) Apply(set *flag.FlagSet) {
eachName(f.Name, func(name string) {
set.Var(f.Value, name, f.Usage)
})
}
func (f GenericFlag) getName() string {
return f.Name
}
type StringSlice []string
func (f *StringSlice) Set(value string) error {