1package kingpin 2 3import ( 4 "net" 5 "net/url" 6 "os" 7 "time" 8 9 "github.com/alecthomas/units" 10) 11 12type Settings interface { 13 SetValue(value Value) 14} 15 16type parserMixin struct { 17 value Value 18 required bool 19} 20 21func (p *parserMixin) SetValue(value Value) { 22 p.value = value 23} 24 25// StringMap provides key=value parsing into a map. 26func (p *parserMixin) StringMap() (target *map[string]string) { 27 target = &(map[string]string{}) 28 p.StringMapVar(target) 29 return 30} 31 32// Duration sets the parser to a time.Duration parser. 33func (p *parserMixin) Duration() (target *time.Duration) { 34 target = new(time.Duration) 35 p.DurationVar(target) 36 return 37} 38 39// Bytes parses numeric byte units. eg. 1.5KB 40func (p *parserMixin) Bytes() (target *units.Base2Bytes) { 41 target = new(units.Base2Bytes) 42 p.BytesVar(target) 43 return 44} 45 46// IP sets the parser to a net.IP parser. 47func (p *parserMixin) IP() (target *net.IP) { 48 target = new(net.IP) 49 p.IPVar(target) 50 return 51} 52 53// TCP (host:port) address. 54func (p *parserMixin) TCP() (target **net.TCPAddr) { 55 target = new(*net.TCPAddr) 56 p.TCPVar(target) 57 return 58} 59 60// TCPVar (host:port) address. 61func (p *parserMixin) TCPVar(target **net.TCPAddr) { 62 p.SetValue(newTCPAddrValue(target)) 63} 64 65// ExistingFile sets the parser to one that requires and returns an existing file. 66func (p *parserMixin) ExistingFile() (target *string) { 67 target = new(string) 68 p.ExistingFileVar(target) 69 return 70} 71 72// ExistingDir sets the parser to one that requires and returns an existing directory. 73func (p *parserMixin) ExistingDir() (target *string) { 74 target = new(string) 75 p.ExistingDirVar(target) 76 return 77} 78 79// ExistingFileOrDir sets the parser to one that requires and returns an existing file OR directory. 80func (p *parserMixin) ExistingFileOrDir() (target *string) { 81 target = new(string) 82 p.ExistingFileOrDirVar(target) 83 return 84} 85 86// File returns an os.File against an existing file. 87func (p *parserMixin) File() (target **os.File) { 88 target = new(*os.File) 89 p.FileVar(target) 90 return 91} 92 93// File attempts to open a File with os.OpenFile(flag, perm). 94func (p *parserMixin) OpenFile(flag int, perm os.FileMode) (target **os.File) { 95 target = new(*os.File) 96 p.OpenFileVar(target, flag, perm) 97 return 98} 99 100// URL provides a valid, parsed url.URL. 101func (p *parserMixin) URL() (target **url.URL) { 102 target = new(*url.URL) 103 p.URLVar(target) 104 return 105} 106 107// StringMap provides key=value parsing into a map. 108func (p *parserMixin) StringMapVar(target *map[string]string) { 109 p.SetValue(newStringMapValue(target)) 110} 111 112// Float sets the parser to a float64 parser. 113func (p *parserMixin) Float() (target *float64) { 114 return p.Float64() 115} 116 117// Float sets the parser to a float64 parser. 118func (p *parserMixin) FloatVar(target *float64) { 119 p.Float64Var(target) 120} 121 122// Duration sets the parser to a time.Duration parser. 123func (p *parserMixin) DurationVar(target *time.Duration) { 124 p.SetValue(newDurationValue(target)) 125} 126 127// BytesVar parses numeric byte units. eg. 1.5KB 128func (p *parserMixin) BytesVar(target *units.Base2Bytes) { 129 p.SetValue(newBytesValue(target)) 130} 131 132// IP sets the parser to a net.IP parser. 133func (p *parserMixin) IPVar(target *net.IP) { 134 p.SetValue(newIPValue(target)) 135} 136 137// ExistingFile sets the parser to one that requires and returns an existing file. 138func (p *parserMixin) ExistingFileVar(target *string) { 139 p.SetValue(newExistingFileValue(target)) 140} 141 142// ExistingDir sets the parser to one that requires and returns an existing directory. 143func (p *parserMixin) ExistingDirVar(target *string) { 144 p.SetValue(newExistingDirValue(target)) 145} 146 147// ExistingDir sets the parser to one that requires and returns an existing directory. 148func (p *parserMixin) ExistingFileOrDirVar(target *string) { 149 p.SetValue(newExistingFileOrDirValue(target)) 150} 151 152// FileVar opens an existing file. 153func (p *parserMixin) FileVar(target **os.File) { 154 p.SetValue(newFileValue(target, os.O_RDONLY, 0)) 155} 156 157// OpenFileVar calls os.OpenFile(flag, perm) 158func (p *parserMixin) OpenFileVar(target **os.File, flag int, perm os.FileMode) { 159 p.SetValue(newFileValue(target, flag, perm)) 160} 161 162// URL provides a valid, parsed url.URL. 163func (p *parserMixin) URLVar(target **url.URL) { 164 p.SetValue(newURLValue(target)) 165} 166 167// URLList provides a parsed list of url.URL values. 168func (p *parserMixin) URLList() (target *[]*url.URL) { 169 target = new([]*url.URL) 170 p.URLListVar(target) 171 return 172} 173 174// URLListVar provides a parsed list of url.URL values. 175func (p *parserMixin) URLListVar(target *[]*url.URL) { 176 p.SetValue(newURLListValue(target)) 177} 178 179// Enum allows a value from a set of options. 180func (p *parserMixin) Enum(options ...string) (target *string) { 181 target = new(string) 182 p.EnumVar(target, options...) 183 return 184} 185 186// EnumVar allows a value from a set of options. 187func (p *parserMixin) EnumVar(target *string, options ...string) { 188 p.SetValue(newEnumFlag(target, options...)) 189} 190 191// Enums allows a set of values from a set of options. 192func (p *parserMixin) Enums(options ...string) (target *[]string) { 193 target = new([]string) 194 p.EnumsVar(target, options...) 195 return 196} 197 198// EnumVar allows a value from a set of options. 199func (p *parserMixin) EnumsVar(target *[]string, options ...string) { 200 p.SetValue(newEnumsFlag(target, options...)) 201} 202 203// A Counter increments a number each time it is encountered. 204func (p *parserMixin) Counter() (target *int) { 205 target = new(int) 206 p.CounterVar(target) 207 return 208} 209 210func (p *parserMixin) CounterVar(target *int) { 211 p.SetValue(newCounterValue(target)) 212} 213