-
Notifications
You must be signed in to change notification settings - Fork 309
/
arg_test.go
163 lines (123 loc) · 3.71 KB
/
arg_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
package flags
import (
"testing"
)
func TestPositional(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Command int
Filename string
Rest []string
} `positional-args:"yes" required:"yes"`
}{}
p := NewParser(&opts, Default)
ret, err := p.ParseArgs([]string{"10", "arg_test.go", "a", "b"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
return
}
if opts.Positional.Command != 10 {
t.Fatalf("Expected opts.Positional.Command to be 10, but got %v", opts.Positional.Command)
}
if opts.Positional.Filename != "arg_test.go" {
t.Fatalf("Expected opts.Positional.Filename to be \"arg_test.go\", but got %v", opts.Positional.Filename)
}
assertStringArray(t, opts.Positional.Rest, []string{"a", "b"})
assertStringArray(t, ret, []string{})
}
func TestPositionalRequired(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Command int
Filename string
Rest []string
} `positional-args:"yes" required:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"10"})
assertError(t, err, ErrRequired, "the required argument `Filename` was not provided")
}
func TestPositionalRequiredRest1Fail(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"yes"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{})
assertError(t, err, ErrRequired, "the required argument `Rest (at least 1 argument)` was not provided")
}
func TestPositionalRequiredRest1Pass(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"yes"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"rest1"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
return
}
if len(opts.Positional.Rest) != 1 {
t.Fatalf("Expected 1 positional rest argument")
}
assertString(t, opts.Positional.Rest[0], "rest1")
}
func TestPositionalRequiredRest2Fail(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"2"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"rest1"})
assertError(t, err, ErrRequired, "the required argument `Rest (at least 2 arguments, but got only 1)` was not provided")
}
func TestPositionalRequiredRest2Pass(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"2"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"rest1", "rest2", "rest3"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
return
}
if len(opts.Positional.Rest) != 3 {
t.Fatalf("Expected 3 positional rest argument")
}
assertString(t, opts.Positional.Rest[0], "rest1")
assertString(t, opts.Positional.Rest[1], "rest2")
assertString(t, opts.Positional.Rest[2], "rest3")
}
func TestPositionalRequiredRestRangeFail(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"1-2"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"rest1", "rest2", "rest3"})
assertError(t, err, ErrRequired, "the required argument `Rest (at most 2 arguments, but got 3)` was not provided")
}
func TestPositionalRequiredRestRangeEmptyFail(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Positional struct {
Rest []string `required:"0-0"`
} `positional-args:"yes"`
}{}
p := NewParser(&opts, None)
_, err := p.ParseArgs([]string{"some", "thing"})
assertError(t, err, ErrRequired, "the required argument `Rest (zero arguments)` was not provided")
}