flags.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // mauflag - An extendable command-line argument parser for Golang
  2. // Copyright (C) 2016 Maunium
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. package mauflag
  15. // Flag represents a single flag
  16. type Flag struct {
  17. longKeys []string
  18. shortKeys []string
  19. Value Value
  20. defaul string
  21. usage string
  22. usageCat string
  23. usageValName string
  24. }
  25. // Make creates and registers a flag
  26. func (fs *Set) Make() *Flag {
  27. flag := &Flag{}
  28. val := stringValue("")
  29. flag.Value = &val
  30. flag.usageCat = "Application"
  31. flag.activateDefaultValue()
  32. fs.flags = append(fs.flags, flag)
  33. return flag
  34. }
  35. // MakeKey creates and registers a flag with the given short and long keys
  36. func (fs *Set) MakeKey(short, long string) *Flag {
  37. return fs.Make().Key(short, long)
  38. }
  39. // MakeFull creates and registers a flag with the given short and long keys, usage string and default value
  40. func (fs *Set) MakeFull(short, long, usage, defVal string) *Flag {
  41. return fs.MakeKey(short, long).Usage(usage).Default(defVal)
  42. }
  43. func (flag *Flag) setValue(val string) error {
  44. return flag.Value.Set(val)
  45. }
  46. // Usage sets the usage of this Flag
  47. func (flag *Flag) Usage(usage string) *Flag {
  48. flag.usage = usage
  49. return flag
  50. }
  51. // UsageCategory sets the category of this flag (e.g. Application or Help)
  52. func (flag *Flag) UsageCategory(category string) *Flag {
  53. flag.usageCat = category
  54. return flag
  55. }
  56. // ValueName sets the value name in the usage page
  57. func (flag *Flag) ValueName(valname string) *Flag {
  58. flag.usageValName = valname
  59. return flag
  60. }
  61. // Default sets the default value of this Flag
  62. // The value given is passed to the Value container of this flag using `Set()`
  63. func (flag *Flag) Default(defaul string) *Flag {
  64. flag.defaul = defaul
  65. flag.activateDefaultValue()
  66. return flag
  67. }
  68. func (flag *Flag) activateDefaultValue() {
  69. if len(flag.defaul) > 0 && flag.Value != nil {
  70. flag.Value.Set(flag.defaul)
  71. }
  72. }
  73. // LongKey adds a long key to this Flag
  74. func (flag *Flag) LongKey(key string) *Flag {
  75. flag.longKeys = append(flag.longKeys, key)
  76. return flag
  77. }
  78. // ShortKey adds a short key to this Flag
  79. func (flag *Flag) ShortKey(key string) *Flag {
  80. flag.shortKeys = append(flag.shortKeys, key)
  81. return flag
  82. }
  83. // Key adds a long and a short key to this Flag
  84. func (flag *Flag) Key(short, long string) *Flag {
  85. return flag.ShortKey(short).LongKey(long)
  86. }
  87. // Custom sets a custom object that implemets Value as the value of this flag
  88. func (flag *Flag) Custom(val Value) {
  89. flag.Value = val
  90. flag.activateDefaultValue()
  91. }
  92. // Bool sets the type of this flag to a boolean and returns a pointer to the value
  93. func (flag *Flag) Bool() *bool {
  94. val := boolValue(false)
  95. flag.Value = &val
  96. flag.activateDefaultValue()
  97. return (*bool)(&val)
  98. }
  99. // String sets the type of this flag to a string and returns a pointer to the value
  100. func (flag *Flag) String() *string {
  101. val := stringValue("")
  102. flag.Value = &val
  103. flag.activateDefaultValue()
  104. return (*string)(&val)
  105. }
  106. // StringMap sets the type of this flag to a string-string map and returns a pointer to the value
  107. func (flag *Flag) StringMap() *map[string]string {
  108. val := ssMapValue{}
  109. flag.Value = &val
  110. flag.activateDefaultValue()
  111. return (*map[string]string)(&val)
  112. }
  113. // StringArray sets the type of this flag to a string array and returns a pointer to the value
  114. func (flag *Flag) StringArray() *[]string {
  115. val := stringArrayValue{}
  116. flag.Value = &val
  117. flag.activateDefaultValue()
  118. return (*[]string)(&val)
  119. }
  120. // IntArray sets the type of this flag to a signed default-length integer array and returns a pointer to the value
  121. func (flag *Flag) IntArray() *[]int {
  122. val := intArrayValue{}
  123. flag.Value = &val
  124. flag.activateDefaultValue()
  125. return (*[]int)(&val)
  126. }
  127. // Int64Array sets the type of this flag to a signed 64-bit integer array and returns a pointer to the value
  128. func (flag *Flag) Int64Array() *[]int64 {
  129. val := int64ArrayValue{}
  130. flag.Value = &val
  131. flag.activateDefaultValue()
  132. return (*[]int64)(&val)
  133. }
  134. // Int sets the type of this flag to a signed default-length integer and returns a pointer to the value
  135. func (flag *Flag) Int() *int {
  136. val := intValue(0)
  137. flag.Value = &val
  138. flag.activateDefaultValue()
  139. return (*int)(&val)
  140. }
  141. // Uint sets the type of this flag to an unsigned default-length integer and returns a pointer to the value
  142. func (flag *Flag) Uint() *uint {
  143. val := uintValue(0)
  144. flag.Value = &val
  145. flag.activateDefaultValue()
  146. return (*uint)(&val)
  147. }
  148. // Int8 sets the type of this flag to a signed 8-bit integer and returns a pointer to the value
  149. func (flag *Flag) Int8() *int8 {
  150. val := int8Value(0)
  151. flag.Value = &val
  152. flag.activateDefaultValue()
  153. return (*int8)(&val)
  154. }
  155. // Uint8 sets the type of this flag to an unsigned 8-bit integer and returns a pointer to the value
  156. func (flag *Flag) Uint8() *uint8 {
  157. val := uint8Value(0)
  158. flag.Value = &val
  159. flag.activateDefaultValue()
  160. return (*uint8)(&val)
  161. }
  162. // Byte sets the type of this flag to a byte (unsigned 8-bit integer) and returns a pointer to the value
  163. func (flag *Flag) Byte() *byte {
  164. val := byteValue(0)
  165. flag.Value = &val
  166. flag.activateDefaultValue()
  167. return (*byte)(&val)
  168. }
  169. // Int16 sets the type of this flag to a signed 16-bit integer and returns a pointer to the value
  170. func (flag *Flag) Int16() *int16 {
  171. val := int16Value(0)
  172. flag.Value = &val
  173. flag.activateDefaultValue()
  174. return (*int16)(&val)
  175. }
  176. // Uint16 sets the type of this flag to an unsigned 16-bit integer and returns a pointer to the value
  177. func (flag *Flag) Uint16() *uint16 {
  178. val := uint16Value(0)
  179. flag.Value = &val
  180. flag.activateDefaultValue()
  181. return (*uint16)(&val)
  182. }
  183. // Int32 sets the type of this flag to a signed 32-bit integer and returns a pointer to the value
  184. func (flag *Flag) Int32() *int32 {
  185. val := int32Value(0)
  186. flag.Value = &val
  187. flag.activateDefaultValue()
  188. return (*int32)(&val)
  189. }
  190. // Rune sets the type of this flag to a rune (signed 32-bit integer) and returns a pointer to the value
  191. func (flag *Flag) Rune() *rune {
  192. val := runeValue(0)
  193. flag.Value = &val
  194. flag.activateDefaultValue()
  195. return (*rune)(&val)
  196. }
  197. // Uint32 sets the type of this flag to an unsigned 32-bit integer and returns a pointer to the value
  198. func (flag *Flag) Uint32() *uint32 {
  199. val := uint32Value(0)
  200. flag.Value = &val
  201. flag.activateDefaultValue()
  202. return (*uint32)(&val)
  203. }
  204. // Int64 sets the type of this flag to a signed 64-bit integer and returns a pointer to the value
  205. func (flag *Flag) Int64() *int64 {
  206. val := int64Value(0)
  207. flag.Value = &val
  208. flag.activateDefaultValue()
  209. return (*int64)(&val)
  210. }
  211. // Uint64 sets the type of this flag to an unsigned 64-bit integer and returns a pointer to the value
  212. func (flag *Flag) Uint64() *uint64 {
  213. val := uint64Value(0)
  214. flag.Value = &val
  215. flag.activateDefaultValue()
  216. return (*uint64)(&val)
  217. }
  218. // Float32 sets the type of this flag to an 32-bit float and returns a pointer to the value
  219. func (flag *Flag) Float32() *float32 {
  220. val := float32Value(0)
  221. flag.Value = &val
  222. flag.activateDefaultValue()
  223. return (*float32)(&val)
  224. }
  225. // Float64 sets the type of this flag to an 64-bit float and returns a pointer to the value
  226. func (flag *Flag) Float64() *float64 {
  227. val := float64Value(0)
  228. flag.Value = &val
  229. flag.activateDefaultValue()
  230. return (*float64)(&val)
  231. }