-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflag.go
58 lines (53 loc) · 1022 Bytes
/
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
package cortana
import (
"reflect"
"strings"
)
type flag struct {
name string // the field name
long string
short string
required bool
defaultValue string
description string
rv reflect.Value
}
// nonflag is in fact a flag without prefix "-"
type nonflag flag
func parseFlag(tag string, name string, rv reflect.Value) *flag {
f := &flag{name: name, rv: rv}
parts := strings.Split(tag, ",")
const (
long = iota
short
defaultValue
description
)
state := long
for i := 0; i < len(parts); i++ {
p := strings.TrimSpace(parts[i])
switch state {
case long:
f.long = p
state = short
case short:
f.short = p
state = defaultValue
case defaultValue:
if p == "-" {
f.required = true
} else {
// set to empty value
if p == `''` || p == `""` {
p = ""
}
f.defaultValue = p
}
state = description
case description:
f.description = strings.TrimSpace(strings.Join(parts[i:], ","))
return f
}
}
return f
}