-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
159 lines (119 loc) · 3.08 KB
/
flag.go
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
package argo
import "fmt"
// Flag represents a single CLI flag which may have an argument.
type Flag interface {
// ShortForm returns the short-form character representing this Flag.
ShortForm() byte
// HasShortForm indicates whether this Flag has a short-form character.
HasShortForm() bool
// LongForm returns the long-form string representing this Flag.
LongForm() string
// HasLongForm indicates whether this Flag has a long-form string.
HasLongForm() bool
// Description returns the help-text description of this flag.
Description() string
// HasDescription indicates whether this Flag has a help-text description.
HasDescription() bool
// Argument returns the argument value attached to this Flag.
//
// If this Flag does not have an Argument attached, this method will return
// nil.
Argument() Argument
// HasArgument indicates whether this Flag accepts an argument.
HasArgument() bool
// IsRequired indicates whether this Flag is required.
//
// Required flags must be present in the CLI call.
IsRequired() bool
// RequiresArgument indicates whether this flag has an argument that is
// required.
RequiresArgument() bool
// WasHit indicates whether this flag was used in the CLI call.
WasHit() bool
// HitCount returns the number of times that this flag was used in the CLI
// call.
HitCount() int
AppendWarning(warning string)
isHelpFlag() bool
hit() error
hitWithArg(rawArg string) error
executeCallback()
}
// A FlagCallback is a function that, if set on a flag, will be called by the
// CLI parsing process if that flag is used in the CLI call.
//
// The flag callback will be called after CLI parsing has completed.
type FlagCallback = func(flag Flag)
type flag struct {
hits uint16
short byte
required bool
isHelp bool
arg Argument
long string
desc string
callback FlagCallback
warnings *WarningContext
}
func (f flag) ShortForm() byte {
return f.short
}
func (f flag) HasShortForm() bool {
return f.short != 0
}
func (f flag) LongForm() string {
return f.long
}
func (f flag) HasLongForm() bool {
return len(f.long) > 0
}
func (f flag) Description() string {
return f.desc
}
func (f flag) HasDescription() bool {
return len(f.desc) > 0
}
func (f flag) Argument() Argument {
return f.arg
}
func (f flag) HasArgument() bool {
return f.arg != nil
}
func (f flag) IsRequired() bool {
return f.required
}
func (f flag) RequiresArgument() bool {
return f.arg != nil && f.arg.IsRequired()
}
func (f flag) isHelpFlag() bool {
return f.isHelp
}
func (f *flag) hit() error {
f.hits++
if f.HasArgument() && f.arg.IsRequired() {
return fmt.Errorf("flag %s requires an input", printFlagNames(f))
}
return nil
}
func (f *flag) hitWithArg(rawArg string) error {
f.hits++
if f.arg != nil {
return f.arg.setValue(rawArg)
} else {
return nil // TODO: warning for this
}
}
func (f flag) WasHit() bool {
return f.hits > 0
}
func (f flag) HitCount() int {
return int(f.hits)
}
func (f flag) AppendWarning(warning string) {
f.warnings.appendWarning(warning)
}
func (f flag) executeCallback() {
if f.callback != nil {
f.callback(&f)
}
}