@@ -134,3 +134,51 @@ func TestArgument_SetValue(t *testing.T) {
134
134
assert .NoErr (t , err )
135
135
assert .Eq (t , 12 , arg .Val ())
136
136
}
137
+
138
+ func TestCliArgs_AddArg_panic (t * testing.T ) {
139
+ is := assert .New (t )
140
+ c := gflag.Arguments {}
141
+ c .SetName ("test" )
142
+
143
+ arg := c .AddArg ("arg0" , "arg desc" , true )
144
+ is .Eq (0 , arg .Index ())
145
+
146
+ ret := c .ArgByIndex (0 )
147
+ is .Eq (ret , arg )
148
+
149
+ assert .PanicsMsg (t , func () {
150
+ c .ArgByIndex (1 )
151
+ }, "gflag: get not exists argument #1" )
152
+
153
+ arg = c .AddArg ("arg1" , "arg1 desc" )
154
+ is .Eq (1 , arg .Index ())
155
+
156
+ ret = c .Arg ("arg1" )
157
+ is .Eq (ret , arg )
158
+
159
+ is .PanicsMsg (func () {
160
+ c .Arg ("not-exist" )
161
+ }, "gflag: get not exists argument 'not-exist'" )
162
+
163
+ is .Len (c .Args (), 2 )
164
+
165
+ is .PanicsMsg (func () {
166
+ c .AddArg ("" , "desc" )
167
+ }, "gflag: the command argument name cannot be empty" )
168
+
169
+ is .PanicsMsg (func () {
170
+ c .AddArg (":)&dfd" , "desc" )
171
+ }, "gflag: the argument name ':)&dfd' is invalid, must match: ^[a-zA-Z][\\ w-]*$" )
172
+
173
+ is .PanicsMsg (func () {
174
+ c .AddArg ("arg1" , "desc" )
175
+ }, "gflag: the argument name 'arg1' already exists in command 'test'" )
176
+ is .PanicsMsg (func () {
177
+ c .AddArg ("arg2" , "arg2 desc" , true )
178
+ }, "gflag: required argument 'arg2' cannot be defined after optional argument" )
179
+
180
+ c .AddArg ("arg3" , "arg3 desc" , false , true )
181
+ is .PanicsMsg (func () {
182
+ c .AddArg ("argN" , "desc" , true )
183
+ }, "gflag: have defined an array argument, you cannot add argument 'argN'" )
184
+ }
0 commit comments