-
Notifications
You must be signed in to change notification settings - Fork 44
/
tag.go
151 lines (125 loc) · 3.04 KB
/
tag.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
package cli
import (
"reflect"
"strings"
)
const (
tagCli = "cli"
tagPw = "pw" // password
tagEdit = "edit"
tagUsage = "usage"
tagDefaut = "dft"
tagName = "name"
tagPrompt = "prompt"
tagParser = "parser"
tagSep = "sep" // used to seperate key/value pair of map, default is `=`
dashOne = "-"
dashTwo = "--"
sepName = ", "
defaultSepForKeyValueOfMap = "="
)
type tagProperty struct {
// is a required flag?
isRequired bool `cli:"*x" pw:"*y" edit:"*z"`
// is a force flag?
isForce bool `cli:"!x" pw:"!y" edit:"!z"`
// is a password flag?
isPassword bool `pw:"xxx"`
// is a edit flag?
isEdit bool `edit:"xxx"`
editFile string `edit:"FILE:xxx"`
usage string `usage:"usage string"`
dft string `dft:"default value or expression"`
name string `name:"tag reference name"`
prompt string `prompt:"prompt string"`
sep string `sep:"string for seperate kay/value pair of map"`
parserCreator FlagParserCreator `parser:"parser for flag"`
// flag names
shortNames []string
longNames []string
}
func parseTag(fieldName string, structTag reflect.StructTag) (p *tagProperty, isEmpty bool, err error) {
p = &tagProperty{
shortNames: []string{},
longNames: []string{},
}
cliLikeTagCount := 0
tag := newMultiTag(string(structTag))
// `cli` TAG
cli := tag.Get(tagCli)
if cli != "" {
cliLikeTagCount++
}
// `pw` TAG
if pw := tag.Get(tagPw); pw != "" {
p.isPassword = true
cli = pw
cliLikeTagCount++
}
// `edit` TAG
if edit := tag.Get(tagEdit); edit != "" {
// specific filename for editor
sepIndex := strings.Index(edit, ":")
if sepIndex > 0 {
p.editFile = edit[:sepIndex]
edit = edit[sepIndex+1:]
}
p.isEdit = true
cli = edit
cliLikeTagCount++
}
if cliLikeTagCount > 1 {
err = errCliTagTooMany
return
}
// `usage` TAG
p.usage = tag.Get(tagUsage)
// `dft` TAG
p.dft = tag.Get(tagDefaut)
// `name` TAG
p.name = tag.Get(tagName)
// `prompt` TAG
p.prompt = tag.Get(tagPrompt)
// `parser` TAG
if parserName := tag.Get(tagParser); parserName != "" {
if parserCreator, ok := parserCreators[parserName]; ok {
p.parserCreator = parserCreator
}
}
// `sep` TAG
p.sep = defaultSepForKeyValueOfMap
if sep := tag.Get(tagSep); sep != "" {
p.sep = sep
}
cli = strings.TrimSpace(cli)
for {
if strings.HasPrefix(cli, "*") {
p.isRequired = true
cli = strings.TrimSpace(strings.TrimPrefix(cli, "*"))
} else if strings.HasPrefix(cli, "!") {
p.isForce = true
cli = strings.TrimSpace(strings.TrimPrefix(cli, "!"))
} else {
break
}
}
names := strings.Split(cli, ",")
isEmpty = true
for _, name := range names {
if name = strings.TrimSpace(name); name == dashOne {
return nil, false, nil
}
if len(name) == 0 {
continue
} else if len(name) == 1 {
p.shortNames = append(p.shortNames, dashOne+name)
} else {
p.longNames = append(p.longNames, dashTwo+name)
}
isEmpty = false
}
if isEmpty {
p.longNames = append(p.longNames, dashTwo+fieldName)
}
return
}