-
Notifications
You must be signed in to change notification settings - Fork 2
/
flag_test.go
211 lines (186 loc) · 5.66 KB
/
flag_test.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
package mcli
import (
"bytes"
"flag"
"net"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_flag_DefaultValue(t *testing.T) {
resetDefaultApp()
var args struct {
A bool `cli:"-a" default:"true"`
B string `cli:"-b" default:"astr"`
D time.Duration `cli:"-d" default:"1.5s"`
Arg1 string `cli:"arg1" default:"arg1str"`
Arg2 string `cli:"arg2" default:"arg2str"`
}
fs, err := Parse(&args, WithErrorHandling(flag.ContinueOnError),
WithArgs([]string{"-d", "1000ms", "cmdlineArg1"}))
assert.Nil(t, err)
assert.Equal(t, true, args.A)
assert.Equal(t, "astr", args.B)
assert.Equal(t, 1000*time.Millisecond, args.D)
assert.Equal(t, "cmdlineArg1", args.Arg1)
assert.Equal(t, "arg2str", args.Arg2)
var buf bytes.Buffer
fs.SetOutput(&buf)
fs.Usage()
got := buf.String()
assert.Contains(t, got, "Flags:\n")
assert.Contains(t, got, " -a")
assert.Contains(t, got, "[default: true]")
assert.Contains(t, got, " -b <string>")
assert.Contains(t, got, `[default: "astr"]`)
assert.Contains(t, got, " -d <duration>")
assert.Contains(t, got, "[default: 1.5s]")
assert.Contains(t, got, "Arguments:\n")
assert.Contains(t, got, " arg1 <string>")
assert.Contains(t, got, `[default: "arg1str"]`)
assert.Contains(t, got, " arg2 <string>")
assert.Contains(t, got, `[default: "arg2str"]`)
}
func Test_flag_CompositeType(t *testing.T) {
table := [][]any{
{&struct {
S1 []uint8 `cli:"s1"`
}{}, "s1 <[]uint>"},
{&struct {
S2 []time.Duration `cli:"s2"`
}{}, "s2 <[]duration>"},
{&struct {
M1 map[string]float64 `cli:"m1"`
}{}, "m1 <map[string]float>"},
{&struct {
M2 map[string]time.Duration `cli:"m2"`
}{}, "m2 <map[string]duration>"},
}
for _, row := range table {
resetDefaultApp()
args := row[0]
fs, err := Parse(args, WithErrorHandling(flag.ContinueOnError), WithArgs([]string{}))
assert.Nil(t, err)
var buf bytes.Buffer
fs.SetOutput(&buf)
fs.Usage()
got := buf.String()
want := row[1]
assert.Contains(t, got, want)
}
}
type MyMap map[string]string
func Test_flag_Map(t *testing.T) {
resetDefaultApp()
var args struct {
M1 map[string]string `cli:"-m1"`
M2 MyMap `cli:"-m2"`
M3 map[string]time.Duration `cli:"-m3"`
}
fs, err := Parse(&args, WithErrorHandling(flag.ContinueOnError),
WithArgs([]string{
"-m1", "key1=val1",
"-m1", "key2=val2",
"-m2", "key3=val3",
"-m2", "key4=val4",
"-m3", "key5=1s",
"-m3", "key6=100ms",
}))
_ = fs
assert.Nil(t, err)
assert.Equal(t, map[string]string{"key1": "val1", "key2": "val2"}, args.M1)
assert.Equal(t, MyMap{"key3": "val3", "key4": "val4"}, args.M2)
assert.Equal(t, map[string]time.Duration{"key5": time.Second, "key6": 100 * time.Millisecond}, args.M3)
}
func Test_flag_TextValue(t *testing.T) {
type args struct {
A1 net.IP `cli:"-a1"`
A2 time.Time `cli:"-a2"`
A3 *time.Time `cli:"-a3"`
}
app1 := NewApp()
args1 := &args{}
fs, err := app1.parseArgs(args1, WithErrorHandling(flag.ContinueOnError),
WithArgs([]string{
"-a1", "4.3.2.1",
"-a2", "2022-06-23T01:00:00+08:00",
"-a3", "2022-06-23T02:00:00+08:00",
}))
_ = fs
assert.Nil(t, err)
assert.Equal(t, "4.3.2.1", args1.A1.String())
assert.Equal(t, "2022-06-23T01:00:00+08:00", args1.A2.Format(time.RFC3339))
assert.Equal(t, "2022-06-23T02:00:00+08:00", args1.A3.Format(time.RFC3339))
app2 := NewApp()
args2 := &args{}
fs, err = app2.parseArgs(args2, WithErrorHandling(flag.ContinueOnError), WithArgs([]string{}))
_ = fs
assert.Nil(t, err)
assert.Zero(t, args2.A1)
assert.True(t, args2.A2.IsZero())
assert.Nil(t, args2.A3)
}
type stringArray []string
func (t stringArray) String() string {
return strings.Join(t, ", ")
}
func (t *stringArray) Set(s string) error {
*t = append(*t, s)
return nil
}
func Test_flag_isZero(t *testing.T) {
var args struct {
A1 net.IP `cli:"-a1" default:"1.2.3.4"`
A2 stringArray `cli:"#R, -a2"`
}
app := NewApp()
fs, err := app.parseArgs(&args, WithErrorHandling(flag.ContinueOnError), WithArgs([]string{}))
assert.Equal(t, err.Error(), "flag is required but not set: -a2")
assert.NotNil(t, fs.Lookup("a1"))
assert.NotNil(t, fs.Lookup("a2"))
assert.Equal(t, "1.2.3.4", fs.Lookup("a1").DefValue)
assert.Equal(t, "1.2.3.4", args.A1.String())
}
func Test_flag_isZero_2(t *testing.T) {
var args struct {
A1 net.IP `cli:"#R, -a1" default:"1.2.3.4"`
A2 stringArray `cli:"-a2"`
}
app := NewApp()
fs, err := app.parseArgs(&args, WithErrorHandling(flag.ContinueOnError), WithArgs([]string{}))
assert.Nil(t, err)
assert.NotNil(t, fs.Lookup("a1"))
assert.NotNil(t, fs.Lookup("a2"))
assert.Equal(t, "1.2.3.4", fs.Lookup("a1").DefValue)
assert.Equal(t, "1.2.3.4", fs.Lookup("a1").Value.String())
assert.Equal(t, "1.2.3.4", args.A1.String())
var args1 struct {
A1 net.IP `cli:"#R, -a1"`
A2 stringArray `cli:"-a2"`
}
app.resetParsingContext()
fs, err = app.parseArgs(&args1, WithErrorHandling(flag.ContinueOnError),
WithArgs([]string{"-a1", ""}))
_ = fs
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "flag is required but not set: -a1")
}
func Test_findFlagIndex(t *testing.T) {
tests := map[string]struct {
input []string
want int
}{
"flag not found no values": {input: []string{}, want: 0},
"flag not found": {input: []string{"subcommand", "argument"}, want: 2},
"flag found": {input: []string{"-subcommand", "argument"}, want: 0},
"flag found middle": {input: []string{"v", "-flag", "argument"}, want: 1},
"flag found end": {input: []string{"v", "argument", "-flag"}, want: 2},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := findFlagIndex(tc.input)
assert.Equal(t, tc.want, got)
})
}
}