github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/cmd/flag.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 ) 6 7 // A string value compatible with a flag var 8 // that allows you to assign multiple flags 9 // to the same string value. If the value is 10 // set twice either by duplicating the flag 11 // or using the original and alias then an error 12 // is raised. 13 type aliasedStringValue string 14 15 func newAliasedStringValue(p *string, val string) *aliasedStringValue { 16 *p = val 17 return (*aliasedStringValue)(p) 18 } 19 20 func (s *aliasedStringValue) Set(val string) error { 21 if *s != "" { 22 return fmt.Errorf("Value already set to %v", val) 23 } 24 *s = aliasedStringValue(val) 25 return nil 26 } 27 28 func (s *aliasedStringValue) Get() interface{} { return string(*s) } 29 30 func (s *aliasedStringValue) String() string { return string(*s) } 31 32 // A string value compatible with a flag var 33 // that allows you to assign multiple strings 34 // to the same string value as a string slice. 35 type multiStringValue []string 36 37 func NewMultiStringValue(p *[]string) *multiStringValue { 38 return (*multiStringValue)(p) 39 } 40 41 func (s *multiStringValue) Set(val string) error { 42 *s = append(*s, val) 43 return nil 44 } 45 46 func (s *multiStringValue) Get() interface{} { return []string(*s) } 47 48 func (s *multiStringValue) String() string { return fmt.Sprintf("%v\n", *s) } 49 50 // A string value that indicates that it is not yet implemented if it's used. 51 type notimplStringValue string 52 53 func newNotimplStringValue() *notimplStringValue { 54 var s string 55 return (*notimplStringValue)(&s) 56 } 57 58 func (s *notimplStringValue) Set(val string) error { 59 return fmt.Errorf("Not yet implemented") 60 } 61 62 func (s *notimplStringValue) Get() interface{} { return string(*s) } 63 64 func (s *notimplStringValue) String() string { return string(*s) } 65 66 // A boolean value that indicates that it is not yet implemented if it's used. 67 type notimplBoolValue bool 68 69 func newNotimplBoolValue() *notimplBoolValue { 70 var b bool 71 return (*notimplBoolValue)(&b) 72 } 73 74 func (b *notimplBoolValue) Set(val string) error { 75 return fmt.Errorf("Not yet implemented") 76 } 77 78 func (b *notimplBoolValue) Get() interface{} { return bool(*b) } 79 80 func (b *notimplBoolValue) String() string { return "false" } 81 82 func (b *notimplBoolValue) IsBoolFlag() bool { return true }