flags : generic Flag validity check using a type switch
This commit is contained in:
parent
1fed129838
commit
7e3529aaee
1 changed files with 18 additions and 2 deletions
|
|
@ -19,13 +19,29 @@ type Val interface {
|
|||
}
|
||||
|
||||
// with generic Val field, instantiate like Flag[string]
|
||||
type Flag[V Val] struct {
|
||||
// TODO: probably struct inheritance would be cleaner, but Flag[bool] is cooler than FlagBool.
|
||||
type Flag[T Val] struct {
|
||||
Long string
|
||||
Short string
|
||||
Val V
|
||||
Val T
|
||||
Help string
|
||||
}
|
||||
|
||||
// generic Flag validity check
|
||||
// NOTE: you cannot define a method on an instantiated type, only the generic
|
||||
// https://www.reddit.com/r/golang/comments/1n6xasx/comment/nc3dbhd/
|
||||
func (f Flag[Val]) IsSet() bool {
|
||||
// must do this weird dance: convert to empty interface, then cast:
|
||||
switch any(f.Val).(type) {
|
||||
case string:
|
||||
return any(f.Val).(string) != ""
|
||||
case bool:
|
||||
return any(f.Val).(bool)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func HasDashPrefix(s string) bool {
|
||||
return strings.HasPrefix(s, "-")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue