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
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseFlag(t *testing.T) {
cases := []struct {
name string
args string
flag string
expect string
expectErr string
}{
{
name: "bar=one",
args: "--foo --bar=one --baz",
flag: "--bar",
expect: "one",
},
{
name: "bar one",
args: "--foo --bar one --baz",
flag: "--bar",
expect: "one",
},
{
name: "bar one first flag",
args: "--bar one --foo two --baz three",
flag: "--bar",
expect: "one",
},
{
name: "bar one last flag",
args: "--foo --baz --bar one",
flag: "--bar",
expect: "one",
},
{
name: "non-existent flag",
args: "--foo one",
flag: "--bar",
expectErr: "missing flag",
},
{
name: "empty args",
args: "",
flag: "--foo",
expectErr: "missing flag",
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
args := strings.Split(tt.args, " ")
result, err := parseFlag(args, tt.flag)
if tt.expectErr != "" {
require.ErrorContains(t, err, tt.expectErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.expect, result)
}
})
}
}