parsers.go 5.3 KB

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