-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_config.go
170 lines (124 loc) · 3.01 KB
/
parser_config.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
package argh
type ParserConfig struct {
Prog *CommandConfig
ScannerConfig *ScannerConfig
}
type ParserOption func(*ParserConfig)
func NewParserConfig(opts ...ParserOption) *ParserConfig {
pCfg := &ParserConfig{}
for _, opt := range opts {
if opt != nil {
opt(pCfg)
}
}
if pCfg.Prog == nil {
pCfg.Prog = &CommandConfig{}
pCfg.Prog.init()
}
if pCfg.ScannerConfig == nil {
pCfg.ScannerConfig = POSIXyScannerConfig
}
return pCfg
}
type CommandConfig struct {
NValue NValue
ValueNames []string
Flags *Flags
Commands *Commands
On func(Command) error `json:"-"`
}
func (cCfg *CommandConfig) init() {
if cCfg.ValueNames == nil {
cCfg.ValueNames = []string{}
}
if cCfg.Flags == nil {
cCfg.Flags = &Flags{}
}
if cCfg.Commands == nil {
cCfg.Commands = &Commands{}
}
}
func (cCfg *CommandConfig) GetCommandConfig(name string) (CommandConfig, bool) {
tracef("CommandConfig.GetCommandConfig(%q)", name)
if cCfg.Commands == nil {
cCfg.Commands = &Commands{Map: map[string]CommandConfig{}}
}
return cCfg.Commands.Get(name)
}
func (cCfg *CommandConfig) SetCommandConfig(name string, sCfg *CommandConfig) {
tracef("CommandConfig.SetCommandConfig(%q, ...)", name)
if cCfg.Commands == nil {
cCfg.Commands = &Commands{Map: map[string]CommandConfig{}}
}
sCfg.init()
sCfg.Flags.Parent = cCfg.Flags
cCfg.Commands.Set(name, sCfg)
}
func (cCfg *CommandConfig) GetFlagConfig(name string) (FlagConfig, bool) {
tracef("CommandConfig.GetFlagConfig(%q)", name)
if cCfg.Flags == nil {
cCfg.Flags = &Flags{Map: map[string]FlagConfig{}}
}
return cCfg.Flags.Get(name)
}
func (cCfg *CommandConfig) SetFlagConfig(name string, flCfg *FlagConfig) {
tracef("CommandConfig.SetFlagConfig(%q, ...)", name)
if cCfg.Flags == nil {
cCfg.Flags = &Flags{Map: map[string]FlagConfig{}}
}
cCfg.Flags.Set(name, flCfg)
}
type FlagConfig struct {
NValue NValue
Persist bool
ValueNames []string
On func(Flag) error `json:"-"`
}
type Flags struct {
Parent *Flags
Map map[string]FlagConfig
Automatic bool
}
func (fl *Flags) Get(name string) (FlagConfig, bool) {
tracef("Flags.Get(%q)", name)
if fl.Map == nil {
fl.Map = map[string]FlagConfig{}
}
flCfg, ok := fl.Map[name]
if !ok {
if fl.Automatic {
return FlagConfig{}, true
}
if fl.Parent != nil {
flCfg, ok = fl.Parent.Get(name)
return flCfg, ok && flCfg.Persist
}
}
return flCfg, ok
}
func (fl *Flags) Set(name string, flCfg *FlagConfig) {
tracef("Flags.Get(%q)", name)
if fl.Map == nil {
fl.Map = map[string]FlagConfig{}
}
fl.Map[name] = *flCfg
}
type Commands struct {
Map map[string]CommandConfig
}
func (cmd *Commands) Get(name string) (CommandConfig, bool) {
tracef("Commands.Get(%q)", name)
if cmd.Map == nil {
cmd.Map = map[string]CommandConfig{}
}
cmdCfg, ok := cmd.Map[name]
return cmdCfg, ok
}
func (cmd *Commands) Set(name string, cCfg *CommandConfig) {
tracef("Commands.Set(%q, ...)", name)
if cmd.Map == nil {
cmd.Map = map[string]CommandConfig{}
}
cCfg.init()
cmd.Map[name] = *cCfg
}