@@ -182,45 +182,92 @@ func TestShortAndLong(t *testing.T) {
182182func TestComplete (t * testing.T ) {
183183 type config struct {
184184 Extensions []string `clap:"--extensions,-e,mandatory"`
185- Recursive bool `clap:"--recusrive ,-r"`
185+ Recursive bool `clap:"--recursive ,-r"`
186186 Verbose bool `clap:"--verbose,-v"`
187+ Size int `clap:"--size,-s"`
187188 Directories []string `clap:"trailing"`
188189 }
189190 cfg := & config {}
190191 var err error
191192 var results * clap.Results
192193 if results , err = clap .Parse ([]string {
193- "--extensions" , "jpg" , "png" , "bmp" , "-v" , "$home/temp" , "$home/tmp" , "/tmp" ,
194+ "--extensions" , "jpg" , "png" , "bmp" , "-v" , "-s" , "10" , " $home/temp" , "$home/tmp" , "/tmp" ,
194195 }, cfg ); err != nil {
195196 t .Errorf ("parsing error: %s" , err )
196197 }
197198 t .Logf ("t: %v\n " , results )
198199 wanted := & config {
199- Extensions : []string {"jpg" , "png" , "bmp" }, Verbose : true ,
200+ Extensions : []string {"jpg" , "png" , "bmp" }, Verbose : true , Size : 10 ,
200201 Directories : []string {"$home/temp" , "$home/tmp" , "/tmp" },
201202 }
202203 if ! reflect .DeepEqual (cfg , wanted ) {
203204 t .Errorf ("wanted: '%v', got '%v'" , wanted , cfg )
204205 }
205206}
206207
208+ func TestBooleans (t * testing.T ) {
209+ type config struct {
210+ Recursive bool `clap:"--recursive,-R"`
211+ }
212+ cfg := & config {
213+ Recursive : false ,
214+ }
215+ var err error
216+ var results * clap.Results
217+ if results , err = clap .Parse ([]string {"--recursive" }, cfg ); err != nil {
218+ t .Errorf ("parsing error: %s" , err )
219+ }
220+ t .Logf ("t: %v\n " , results )
221+ wanted := & config {Recursive : true }
222+ if ! reflect .DeepEqual (cfg , wanted ) {
223+ t .Errorf ("wanted: '%v', got '%v'" , wanted , cfg )
224+ }
225+ // with --no-recursive
226+ cfg = & config {
227+ Recursive : true ,
228+ }
229+ if results , err = clap .Parse ([]string {"--no-recursive" }, cfg ); err != nil {
230+ t .Errorf ("parsing error: %s" , err )
231+ }
232+ t .Logf ("t: %v\n " , results )
233+ wanted = & config {Recursive : false }
234+ if ! reflect .DeepEqual (cfg , wanted ) {
235+ t .Errorf ("wanted: '%v', got '%v'" , wanted , cfg )
236+ }
237+ }
238+
207239func TestTypes (t * testing.T ) {
208240 type config struct {
209241 String string `clap:"--string"`
210242 Int int `clap:"--int"`
211- Float float64 `clap:"--float"`
243+ Int8 int8 `clap:"--int8"`
244+ Int16 int16 `clap:"--int16"`
245+ Int32 int32 `clap:"--int32"`
246+ Int64 int64 `clap:"--int64"`
247+ UInt uint `clap:"--uint"`
248+ UInt8 uint8 `clap:"--uint8"`
249+ UInt16 uint16 `clap:"--uint16"`
250+ UInt32 uint32 `clap:"--uint32"`
251+ UInt64 uint64 `clap:"--uint64"`
252+ Float32 float32 `clap:"--float32"`
253+ Float64 float64 `clap:"--float64"`
212254 Bool bool `clap:"--bool"`
255+ DefaultTrue bool `clap:"--defaulttrue"`
213256 StringSlice []string `clap:"--string-slice"`
214257 IntSlice []int `clap:"--int-slice"`
215258 StringArray [2 ]string `clap:"--string-array"`
216259 IntArray [3 ]int `clap:"--int-array"`
217260 Trailing []string `clap:"trailing"`
218261 }
219- cfg := & config {}
262+ cfg := & config {
263+ DefaultTrue : true ,
264+ }
220265 var err error
221266 var results * clap.Results
222267 if results , err = clap .Parse ([]string {
223- "--string" , "str" , "--int" , "10" , "--float" , "12.3" , "--bool" , "--string-slice" , "a" , "b" , "c" ,
268+ "--string" , "str" , "--int" , "10" , "--int8" , "8" , "--int16" , "16" , "--int32" , "32" , "--int64" , "64" ,
269+ "--uint" , "12" , "--uint8" , "65535" , "--uint16" , "65535" , "--uint32" , "65535" , "--uint64" , "65535" ,
270+ "--float32" , "12.32" , "--float64" , "12.64" , "--bool" , "--no-defaulttrue" , "--string-slice" , "a" , "b" , "c" ,
224271 "--int-slice" , "10" , "11" , "12" , "--string-array" , "a" , "b" , "--int-array" , "10" , "11" , "12" ,
225272 "w" , "x" , "y" , "z" ,
226273 }, cfg ); err != nil {
@@ -230,8 +277,19 @@ func TestTypes(t *testing.T) {
230277 wanted := & config {
231278 String : "str" ,
232279 Int : 10 ,
233- Float : 12.3 ,
280+ Int8 : 8 ,
281+ Int16 : 16 ,
282+ Int32 : 32 ,
283+ Int64 : 64 ,
284+ UInt : 12 ,
285+ UInt8 : 255 ,
286+ UInt16 : 65535 ,
287+ UInt32 : 65535 ,
288+ UInt64 : 65535 ,
289+ Float32 : 12.32 ,
290+ Float64 : 12.64 ,
234291 Bool : true ,
292+ DefaultTrue : false ,
235293 StringSlice : []string {"a" , "b" , "c" },
236294 IntSlice : []int {10 , 11 , 12 },
237295 StringArray : [2 ]string {"a" , "b" },
0 commit comments