Debian Codegraph @

flags.go

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