-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_types.go
144 lines (131 loc) · 4.28 KB
/
flag_types.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
package cline
import (
"strconv"
"strings"
"syscall"
)
// FlagInt defines an `Int` type flag.
type FlagInt struct {
// Name of the flag containing alphanumeric characters and dashes
// but without leading dashes, spaces or any kind of special chars.
Name string
// An optional summary for the flag.
Summary string
// An optional default value for the flag.
Value int
// An optional list of flag aliases containing single alphanumeric characters
// but without dashes, spaces or any special chars.
Aliases []string
// An optional environment variable containing uppercase alphanumeric characters
// and underscores but without dashes, spaces or any kind of special chars.
EnvVar string
flagValue AnyValue
flagAssigned bool
flagProvided bool
flagProvidedAsAlias bool
}
// It sets a default flag value via its associated `Value` prop
// or its environment variable (`EnvVar`) if so.
func (fi *FlagInt) initialize() {
val := AnyValue(strconv.Itoa(fi.Value))
ev, ok := syscall.Getenv(fi.EnvVar)
if ok {
s := AnyValue(ev)
if _, err := s.ToInt(); err == nil {
val = s
}
}
fi.flagValue = val
}
// FlagBool defines a `bool` type flag.
type FlagBool struct {
// Name of the flag containing alphanumeric characters and dashes
// but without leading dashes, spaces or any kind of special chars.
Name string
// An optional summary for the flag.
Summary string
// An optional default value for the flag.
Value bool
// An optional list of flag aliases containing single alphanumeric characters
// but without dashes, spaces or any special chars.
Aliases []string
// An optional environment variable containing uppercase alphanumeric characters
// and underscores but without dashes, spaces or any kind of special chars.
EnvVar string
flagValue AnyValue
flagAssigned bool
flagProvided bool
flagProvidedAsAlias bool
}
// It sets a default flag value via its associated `Value` prop
// or its environment variable (`EnvVar`) if so.
func (fb *FlagBool) initialize() {
val := AnyValue(strconv.FormatBool(fb.Value))
ev, ok := syscall.Getenv(fb.EnvVar)
if ok {
if b, err := AnyValue(ev).ToBool(); err == nil {
val = AnyValue(strconv.FormatBool(b))
}
}
fb.flagValue = val
}
// FlagString defines a `String` type flag.
type FlagString struct {
// Name of the flag containing alphanumeric characters and dashes
// but without leading dashes, spaces or any kind of special chars.
Name string
// An optional summary for the flag.
Summary string
// An optional default value for the flag.
Value string
// An optional list of flag aliases containing single alphanumeric characters
// but without dashes, spaces or any special chars.
Aliases []string
// An optional environment variable containing uppercase alphanumeric characters
// and underscores but without dashes, spaces or any kind of special chars.
EnvVar string
flagValue AnyValue
flagAssigned bool
flagProvided bool
flagProvidedAsAlias bool
}
// It sets a default flag value via its associated `Value` prop
// or its environment variable (`EnvVar`) if so.
func (fs *FlagString) initialize() {
val := AnyValue(fs.Value)
ev, ok := syscall.Getenv(fs.EnvVar)
if ok {
val = AnyValue(ev)
}
fs.flagValue = val
}
// FlagStringSlice defines a string slice type flag.
type FlagStringSlice struct {
// Name of the flag containing alphanumeric characters and dashes
// but without leading dashes, spaces or any kind of special chars.
Name string
// An optional default value for the flag.
Summary string
// An optional default value for the flag.
Value []string
// An optional list of flag aliases containing single alphanumeric characters
// but without dashes, spaces or any special chars.
Aliases []string
// An optional environment variable containing uppercase alphanumeric characters
// and underscores but without dashes, spaces or any kind of special chars.
EnvVar string
flagValue AnyValue
flagAssigned bool
flagProvided bool
flagProvidedAsAlias bool
}
// It sets a default flag value via its associated `Value` prop
// or its environment variable (`EnvVar`) if so.
func (fs *FlagStringSlice) initialize() {
val := AnyValue(strings.Join(fs.Value, ","))
ev, ok := syscall.Getenv(fs.EnvVar)
if ok {
val = AnyValue(ev)
}
fs.flagValue = val
}