-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogify.go
More file actions
478 lines (397 loc) · 12.3 KB
/
logify.go
File metadata and controls
478 lines (397 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package logify
import (
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
)
// Logify is a simple flag parser with common options
type Logify struct {
flagSet *flag.FlagSet
description string
groups []flagGroup
flagData map[string]*FlagData
currentGroup string
}
type flagGroup struct {
name string
description string
}
// FlagData holds metadata about a flag
type FlagData struct {
short string
long string
usage string
group string
defaultValue interface{}
}
// New creates a new Logify instance
func New(description string) *Logify {
return &Logify{
flagSet: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
description: description,
groups: []flagGroup{},
flagData: make(map[string]*FlagData),
}
}
// CreateGroup creates a group and executes the setup function
func (l *Logify) CreateGroup(name, description string, setup func()) {
l.groups = append(l.groups, flagGroup{
name: name,
description: description,
})
l.currentGroup = name
if setup != nil {
setup()
}
l.currentGroup = ""
}
// StringVar adds a string flag with a longname
func (l *Logify) StringVar(field *string, long, defaultValue, usage string) *FlagData {
return l.StringVarP(field, long, "", defaultValue, usage)
}
// StringVarP adds a string flag with a shortname and longname
func (l *Logify) StringVarP(field *string, long, short, defaultValue, usage string) *FlagData {
*field = defaultValue
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.StringVar(field, short, defaultValue, usage)
l.flagData[short] = flagData
}
l.flagSet.StringVar(field, long, defaultValue, usage)
l.flagData[long] = flagData
return flagData
}
// String adds a string flag (legacy method for compatibility)
func (l *Logify) String(long, short, defaultValue, usage string) *string {
value := new(string)
l.StringVarP(value, long, short, defaultValue, usage)
return value
}
// BoolVar adds a boolean flag with a longname
func (l *Logify) BoolVar(field *bool, long string, defaultValue bool, usage string) *FlagData {
return l.BoolVarP(field, long, "", defaultValue, usage)
}
// BoolVarP adds a boolean flag with a shortname and longname
func (l *Logify) BoolVarP(field *bool, long, short string, defaultValue bool, usage string) *FlagData {
*field = defaultValue
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.BoolVar(field, short, defaultValue, usage)
l.flagData[short] = flagData
}
l.flagSet.BoolVar(field, long, defaultValue, usage)
l.flagData[long] = flagData
return flagData
}
// Bool adds a boolean flag (legacy method for compatibility)
func (l *Logify) Bool(long, short string, defaultValue bool, usage string) *bool {
value := new(bool)
l.BoolVarP(value, long, short, defaultValue, usage)
return value
}
// IntVar adds an integer flag with a longname
func (l *Logify) IntVar(field *int, long string, defaultValue int, usage string) *FlagData {
return l.IntVarP(field, long, "", defaultValue, usage)
}
// IntVarP adds an integer flag with a shortname and longname
func (l *Logify) IntVarP(field *int, long, short string, defaultValue int, usage string) *FlagData {
*field = defaultValue
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.IntVar(field, short, defaultValue, usage)
l.flagData[short] = flagData
}
l.flagSet.IntVar(field, long, defaultValue, usage)
l.flagData[long] = flagData
return flagData
}
// Int adds an integer flag (legacy method for compatibility)
func (l *Logify) Int(long, short string, defaultValue int, usage string) *int {
value := new(int)
l.IntVarP(value, long, short, defaultValue, usage)
return value
}
// DurationVar adds a duration flag with a longname
func (l *Logify) DurationVar(field *time.Duration, long string, defaultValue time.Duration, usage string) *FlagData {
return l.DurationVarP(field, long, "", defaultValue, usage)
}
// DurationVarP adds a duration flag with a shortname and longname
func (l *Logify) DurationVarP(field *time.Duration, long, short string, defaultValue time.Duration, usage string) *FlagData {
*field = defaultValue
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.DurationVar(field, short, defaultValue, usage)
l.flagData[short] = flagData
}
l.flagSet.DurationVar(field, long, defaultValue, usage)
l.flagData[long] = flagData
return flagData
}
// Duration adds a duration flag (legacy method for compatibility)
func (l *Logify) Duration(long, short string, defaultValue time.Duration, usage string) *time.Duration {
value := new(time.Duration)
l.DurationVarP(value, long, short, defaultValue, usage)
return value
}
// StringSliceVar adds a string slice flag with a longname
func (l *Logify) StringSliceVar(field *StringSlice, long string, defaultValue []string, usage string) *FlagData {
return l.StringSliceVarP(field, long, "", defaultValue, usage)
}
// StringSliceVarP adds a string slice flag with a shortname and longname
func (l *Logify) StringSliceVarP(field *StringSlice, long, short string, defaultValue []string, usage string) *FlagData {
// Set default values
for _, val := range defaultValue {
field.Set(val)
}
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.Var(field, short, usage)
l.flagData[short] = flagData
}
l.flagSet.Var(field, long, usage)
l.flagData[long] = flagData
return flagData
}
// FileStringSliceVar adds a string slice flag with file support and a longname
func (l *Logify) FileStringSliceVar(field *StringSlice, long string, defaultValue []string, usage string) *FlagData {
return l.FileStringSliceVarP(field, long, "", defaultValue, usage)
}
// FileStringSliceVarP adds a string slice flag with file support, shortname and longname
func (l *Logify) FileStringSliceVarP(field *StringSlice, long, short string, defaultValue []string, usage string) *FlagData {
// Enable file support
field.supportFile = true
// Set default values
for _, val := range defaultValue {
field.Set(val)
}
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.Var(field, short, usage)
l.flagData[short] = flagData
}
l.flagSet.Var(field, long, usage)
l.flagData[long] = flagData
return flagData
}
// StringSlice adds a string slice flag (basic, no file support) - legacy method
func (l *Logify) StringSlice(long, short string, usage string) *StringSlice {
value := NewStringSlice(false)
l.StringSliceVarP(value, long, short, []string{}, usage)
return value
}
// FileStringSlice adds a string slice flag with file support - legacy method
func (l *Logify) FileStringSlice(long, short string, usage string) *StringSlice {
value := NewStringSlice(true)
l.FileStringSliceVarP(value, long, short, []string{}, usage)
return value
}
// SizeVar adds a size flag with a longname
func (l *Logify) SizeVar(field *Size, long string, defaultValue string, usage string) *FlagData {
return l.SizeVarP(field, long, "", defaultValue, usage)
}
// SizeVarP adds a size flag with a shortname and longname
func (l *Logify) SizeVarP(field *Size, long, short string, defaultValue string, usage string) *FlagData {
if defaultValue != "" {
_ = field.Set(defaultValue)
}
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: defaultValue,
}
if short != "" {
l.flagSet.Var(field, short, usage)
l.flagData[short] = flagData
}
l.flagSet.Var(field, long, usage)
l.flagData[long] = flagData
return flagData
}
// Size adds a size flag (supports kb, mb, gb, tb) - legacy method
func (l *Logify) Size(long, short string, defaultValue string, usage string) *Size {
value := new(Size)
l.SizeVarP(value, long, short, defaultValue, usage)
return value
}
// CallbackVar adds a callback flag with a longname that executes fn when set
func (l *Logify) CallbackVar(fn func(), long string, usage string) *FlagData {
return l.CallbackVarP(fn, long, "", usage)
}
// CallbackVarP adds a callback flag with a shortname and longname that executes fn when set
func (l *Logify) CallbackVarP(fn func(), long, short string, usage string) *FlagData {
callback := &Callback{fn: fn}
flagData := &FlagData{
short: short,
long: long,
usage: usage,
group: l.currentGroup,
defaultValue: false,
}
if short != "" {
l.flagSet.Var(callback, short, usage)
l.flagData[short] = flagData
}
l.flagSet.Var(callback, long, usage)
l.flagData[long] = flagData
return flagData
}
// Callback adds a callback flag that executes fn when the flag is set - legacy method
func (l *Logify) Callback(long, short string, usage string, fn func()) *FlagData {
return l.CallbackVarP(fn, long, short, usage)
}
// Parse parses the command line arguments
func (l *Logify) Parse() error {
l.flagSet.Usage = l.printUsage
return l.flagSet.Parse(os.Args[1:])
}
// ParseArgs parses specific arguments (useful for testing)
func (l *Logify) ParseArgs(args []string) error {
l.flagSet.Usage = l.printUsage
return l.flagSet.Parse(args)
}
// printUsage prints the usage information with groups
func (l *Logify) printUsage() {
output := os.Stderr
if l.description != "" {
fmt.Fprintf(output, "%s\n\n", l.description)
}
fmt.Fprintf(output, "Usage: %s [options]\n\n", os.Args[0])
if len(l.groups) > 0 {
// Print grouped flags
for _, group := range l.groups {
fmt.Fprintf(output, "%s:\n", strings.ToUpper(group.description))
writer := tabwriter.NewWriter(output, 0, 0, 3, ' ', 0)
// Track seen flags to avoid duplicates
seen := make(map[string]bool)
for _, data := range l.flagData {
if data.group == group.name && !seen[data.long] {
seen[data.long] = true
l.printFlag(writer, data)
}
}
writer.Flush()
fmt.Fprintln(output)
}
// Print ungrouped flags
writer := tabwriter.NewWriter(output, 0, 0, 3, ' ', 0)
hasUngrouped := false
seen := make(map[string]bool)
for _, data := range l.flagData {
if data.group == "" && !seen[data.long] {
if !hasUngrouped {
fmt.Fprintf(output, "OTHER OPTIONS:\n")
hasUngrouped = true
}
seen[data.long] = true
l.printFlag(writer, data)
}
}
if hasUngrouped {
writer.Flush()
fmt.Fprintln(output)
}
} else {
// Print all flags without grouping
fmt.Fprintf(output, "Options:\n")
writer := tabwriter.NewWriter(output, 0, 0, 3, ' ', 0)
seen := make(map[string]bool)
for _, data := range l.flagData {
if !seen[data.long] {
seen[data.long] = true
l.printFlag(writer, data)
}
}
writer.Flush()
fmt.Fprintln(output)
}
}
// printFlag prints a single flag
func (l *Logify) printFlag(w *tabwriter.Writer, data *FlagData) {
var names []string
if data.short != "" {
names = append(names, "-"+data.short)
}
if data.long != "" {
names = append(names, "-"+data.long)
}
flagNames := " " + strings.Join(names, ", ")
// Get the flag to check its type
fl := l.flagSet.Lookup(data.long)
if fl != nil {
flagType := ""
// Determine type based on default value
switch data.defaultValue.(type) {
case string:
if data.defaultValue != "" {
flagType = " string"
}
case int:
flagType = " int"
case time.Duration:
flagType = " duration"
case bool:
flagType = ""
default:
flagType = " value"
}
fmt.Fprintf(w, "%s%s\t%s", flagNames, flagType, data.usage)
// Show default value if not empty/zero
if !isZeroValue(data.defaultValue) {
fmt.Fprintf(w, " (default: %v)", data.defaultValue)
}
fmt.Fprintln(w)
}
}
// Parsed returns true if Parse() has been called
func (l *Logify) Parsed() bool {
return l.flagSet.Parsed()
}
// Args returns the non-flag arguments
func (l *Logify) Args() []string {
return l.flagSet.Args()
}
// NArg returns the number of non-flag arguments
func (l *Logify) NArg() int {
return l.flagSet.NArg()
}