-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
223 lines (173 loc) · 4.87 KB
/
flags.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
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
package shellframework
import (
"flag"
"fmt"
"github.com/eshu0/shellframework/interfaces"
)
type CommandFlags struct {
sfinterfaces.IFlags
command sfinterfaces.ICommand
parsedflags map[string]sfinterfaces.IFlag
flags []sfinterfaces.IFlag
}
type CommandFlag struct {
sfinterfaces.IFlag
name string
usage string
defaultbvalue bool
defaultsvalue string
defaultivalue int
flagtype int
foundbvalue *bool
foundsvalue *string
foundivalue *int
}
func (flg *CommandFlag) String() string {
switch flg.GetFlagType() {
case 1:
if flg.GetStringValue() == nil {
return ""
} else {
return *flg.GetStringValue()
}
case 2:
if flg.GetBoolValue() == nil {
return ""
} else {
return fmt.Sprintf("%t", *flg.GetBoolValue())
}
case 3:
if flg.GetIntValue() == nil {
return ""
} else {
return fmt.Sprintf("%d", *flg.GetIntValue())
}
}
return "Unknown Type"
}
func (flg *CommandFlag) GetName() string {
return flg.name
}
func (flg *CommandFlag) GetFlagType() int {
return flg.flagtype
}
func (flg *CommandFlag) GetUsage() string {
return flg.usage
}
func (flg *CommandFlag) GetDefaultBoolValue() bool {
return flg.defaultbvalue
}
func (flg *CommandFlag) GetDefaultStringValue() string {
return flg.defaultsvalue
}
func (flg *CommandFlag) GetDefaultIntValue() int {
return flg.defaultivalue
}
func (flg *CommandFlag) GetStringValue() *string {
return flg.foundsvalue
}
func (flg *CommandFlag) GetBoolValue() *bool {
return flg.foundbvalue
}
func (flg *CommandFlag) GetIntValue() *int {
return flg.foundivalue
}
func (flg *CommandFlag) SetFlagValue(toread *flag.FlagSet) {
//flgs.parent.flagset = toread
if toread != nil {
switch flg.GetFlagType() {
case 1:
//toread.StringVar(flg.GetStringValue(), flg.GetName(), flg.GetDefaultStringValue(), flg.GetUsage())
flg.foundsvalue = toread.String(flg.GetName(), flg.GetDefaultStringValue(), flg.GetUsage())
case 2:
//toread.BoolVar(flg.GetBoolValue(), flg.GetName(), flg.GetDefaultBoolValue(), flg.GetUsage())
flg.foundbvalue = toread.Bool(flg.GetName(), flg.GetDefaultBoolValue(), flg.GetUsage())
case 3:
//toread.IntVar(flg.GetIntValue(), flg.GetName(), flg.GetDefaultIntValue(), flg.GetUsage())
flg.foundivalue = toread.Int(flg.GetName(), flg.GetDefaultIntValue(), flg.GetUsage())
}
} else {
}
}
// flags after here
func (sflgs *CommandFlags) Parsedflags() map[string]sfinterfaces.IFlag {
return sflgs.parsedflags
}
func (sflgs *CommandFlags) GetFlags() []sfinterfaces.IFlag {
return sflgs.flags
}
func (sflgs *CommandFlags) SetFlags(flgs []sfinterfaces.IFlag) {
sflgs.flags = flgs
}
func (flgs *CommandFlags) SetCommand(cmd sfinterfaces.ICommand) {
flgs.command = cmd
}
func (flgs *CommandFlags) GetCommand() sfinterfaces.ICommand {
return flgs.command
}
func (flgs *CommandFlags) PrintUsage() {
command := flgs.GetCommand()
//get she;;
shell := command.GetShell()
log := *shell.GetLog()
// flags
flags := flgs.GetFlags()
log.LogDebugf("PrintUsage()", " Number of flags %d for %s ", len(flags), command.GetName())
for _, flg := range flags {
shell.Println("----")
shell.Printlnf("-%s", flg.GetName())
shell.Printlnf("\t Type %d", flg.GetFlagType())
shell.Println("\t Usage:")
shell.Printlnf("\t\t%s", flg.GetUsage())
shell.Println("----")
log.LogDebugf("PrintUsage()", "%s %s", flg.GetName(), flg)
}
}
func (flgs *CommandFlags) Parse() {
command := flgs.GetCommand()
shell := command.GetShell()
log := *shell.GetLog()
// flags
flags := flgs.GetFlags()
// parsed flags
flgs.parsedflags = make(map[string]sfinterfaces.IFlag)
flgset := flag.NewFlagSet(command.GetName(), flag.ContinueOnError)
log.LogDebugf("Parse()", " Number of flags %d for %s ", len(flags), command.GetName())
for _, flg := range flags {
//fr := &FlagResult{}
//fr.SetFlag(flg)
//_, alreadythere := sc.formal[flg.name]
log.LogDebugf("Parse()", "Look up flag : %s ", flg.GetName())
alreadythere := flgset.Lookup(flg.GetName())
if alreadythere == nil {
log.LogDebugf("Parse()", "%s was nil which means it is missing so going to add", flg.GetName())
flg.SetFlagValue(flgset)
flgs.parsedflags[flg.GetName()] = flg
} else {
log.LogDebugf("Parse()", "%s was not nil so it will not be added", flg.GetName())
}
}
ci := command.GetCommandInput()
if ci != nil {
args := ci.GetArgs()
if args != nil {
flgset.Parse(args)
} else {
log.LogDebug("Parse()", "args was nil ")
}
} else {
log.LogDebug("Parse()", "ci was nil ")
}
/*
flgset.VisitAll(func(f *flag.Flag) {
log.LogDebugf("Parse()", "VisitAll - %s %s %s", f.Value, f.Name, f.Usage)
flag.Var(f.Value, f.Name, f.Usage)
})
*/
log.LogDebug("Parse()", "Set the parsed flags")
for _, sflag := range flgs.parsedflags {
log.LogDebugf("Parse()", "flag.GetName: %s", sflag.GetName())
log.LogDebugf("Parse()", "flag.GetFlagType: %d", sflag.GetFlagType())
log.LogDebugf("Parse()", "flag %s", sflag)
}
}