This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clarg_test.go
235 lines (188 loc) · 5.26 KB
/
clarg_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package clarg
import (
"flag"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// this is just one way to do it. the flag back variables can be anywhere.
var cmdDefault struct {
*flag.FlagSet
count int
data string
}
var cmdList struct {
*flag.FlagSet
age int
name string
}
var cmdSend struct {
*flag.FlagSet
dst string
payload string
}
func prepCmd() {
cmdDefault.FlagSet = flag.NewFlagSet("", flag.ExitOnError)
cmdList.FlagSet = flag.NewFlagSet("list", flag.ExitOnError)
cmdSend.FlagSet = flag.NewFlagSet("send", flag.ExitOnError)
cmdDefault.IntVar(&cmdDefault.count, "cnt", 0, "")
cmdDefault.StringVar(&cmdDefault.data, "data", "", "")
cmdList.IntVar(&cmdList.age, "age", -1, "")
cmdList.StringVar(&cmdList.name, "name", "", "")
cmdSend.StringVar(&cmdSend.dst, "dst", "", "")
cmdSend.StringVar(&cmdSend.payload, "p", "", "")
}
func TestNoArg(t *testing.T) {
assert := assert.New(t)
args := []string{}
prepCmd()
n, err := parse(args, cmdDefault.FlagSet, cmdSend.FlagSet, cmdList.FlagSet)
assert.NoError(err)
assert.Equal("", n.Name())
}
func TestTopArg(t *testing.T) {
assert := assert.New(t)
args := []string{"-data", "Hi!", "-cnt", "66"}
prepCmd()
n, err := parse(args, cmdDefault.FlagSet, cmdSend.FlagSet, cmdList.FlagSet)
assert.Equal("", n.Name())
assert.NoError(err)
assert.Equal("Hi!", cmdDefault.data)
assert.Equal(66, cmdDefault.count)
}
func TestMultipleCommands01(t *testing.T) {
assert := assert.New(t)
args := []string{"-data", "Hi!", "-cnt", "66",
"list", "-age", "20", "-name", "Kaveh"}
prepCmd()
n, err := parse(args, cmdDefault.FlagSet, cmdSend.FlagSet, cmdList.FlagSet)
assert.Equal("list", n.Name())
assert.NoError(err)
assert.Equal("Hi!", cmdDefault.data)
assert.Equal(66, cmdDefault.count)
assert.Equal(20, cmdList.age)
assert.Equal("Kaveh", cmdList.name)
}
func TestMultipleCommands02(t *testing.T) {
assert := assert.New(t)
args := []string{"-data", "Hi!", "-cnt", "66",
"send", "-dst", "10", "-p", "QWERTY"}
prepCmd()
n, err := parse(args, cmdDefault.FlagSet, cmdSend.FlagSet, cmdList.FlagSet)
assert.Equal("send", n.Name())
assert.NoError(err)
assert.Equal("Hi!", cmdDefault.data)
assert.Equal(66, cmdDefault.count)
assert.Equal("10", cmdSend.dst)
assert.Equal("QWERTY", cmdSend.payload)
}
func TestNonDefined(t *testing.T) {
assert := assert.New(t)
args := []string{"hey"}
prepCmd()
n, err := parse(args, cmdDefault.FlagSet, cmdSend.FlagSet, cmdList.FlagSet)
assert.True(nil == n)
assert.Error(err)
assert.Contains(err.Error(), "command hey is not defined")
}
func TestNilTop(t *testing.T) {
assert := assert.New(t)
args := []string{"list", "-age", "20", "-name", "Kaveh"}
prepCmd()
n, err := parse(args, nil, cmdSend.FlagSet, cmdList.FlagSet)
assert.Equal("list", n.Name())
assert.NoError(err)
assert.Equal(20, cmdList.age)
assert.Equal("Kaveh", cmdList.name)
}
func ExampleParse() {
topFlags := flag.NewFlagSet("", flag.ExitOnError)
cmdListFlags := flag.NewFlagSet("list", flag.ExitOnError)
cmdSendFlags := flag.NewFlagSet("send", flag.ExitOnError)
var cmdDefault struct {
count int
data string
}
var cmdList struct {
age int
name string
}
var cmdSend struct {
dst string
payload string
}
topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")
cmdListFlags.IntVar(&cmdList.age, "age", -1, "-age <age>")
cmdListFlags.StringVar(&cmdList.name, "name", "", "-name <name>")
cmdSendFlags.StringVar(&cmdSend.dst, "dst", "", "-dst <destination>")
cmdSendFlags.StringVar(&cmdSend.payload, "p", "", "-p <payload>")
if cmd, err := Parse(topFlags,
cmdListFlags,
cmdSendFlags); err != nil {
// show/handle error
} else {
_ = cmd // the matched *flag.FlagSet
}
// use values of back fields for flags
}
func ExampleParse_env() {
topFlags := flag.NewFlagSet("", flag.ExitOnError)
var cmdDefault struct {
count int
data string
}
topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")
if cmd, err := Parse(topFlags); err != nil {
// show/handle error
} else {
fromenv := func(set *flag.FlagSet, flagName, envName string) error {
f := set.Lookup(flagName)
if f == nil {
return nil
}
if f.Value.String() != f.DefValue {
return nil
}
e := os.Getenv(envName)
if e == "" {
return nil
}
if e == f.DefValue {
return nil
}
return f.Value.Set(e)
}
topFlags.VisitAll(func(f *flag.Flag) {
// sample env var name convention: APPNAME_FLAGNAME
if err := fromenv(topFlags,
f.Name,
strings.ToUpper(filepath.Base(os.Args[0])+"_"+f.Name)); err != nil {
log.Fatal(err)
}
})
_ = cmd // the matched *flag.FlagSet
}
// use values of back fields for flags
}
func ExampleParse_nonArgs() {
topFlags := flag.NewFlagSet("", flag.ExitOnError)
var cmdDefault struct {
count int
data string
}
topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")
if cmd, err := Parse(topFlags); err != nil {
// show/handle error
} else {
nonFlags := topFlags.Args()
_ = cmd // the matched *flag.FlagSet
_ = nonFlags
}
// use values of back fields for flags
}