@@ -315,13 +315,14 @@ describe("Testing cliargs library parsing commandlines", function()
315
315
assert .are .equal (" " , result .compress )
316
316
end )
317
317
318
- describe (" Tests parsing with callback" , function ()
318
+ describe (" Tests options parsing with callback" , function ()
319
319
local cb = {}
320
320
321
321
local function callback (key , value , altkey , opt )
322
322
cb .key , cb .value , cb .altkey = key , value , altkey
323
323
return true
324
324
end
325
+
325
326
local function callback_fail (key , value , altkey , opt )
326
327
return nil , " bad argument to " .. opt
327
328
end
@@ -331,42 +332,124 @@ describe("Testing cliargs library parsing commandlines", function()
331
332
end )
332
333
333
334
it (" tests short-key option" , function ()
334
- cli :add_option (" -k, --long-key=VALUE" , " key descriptioin " , " " , callback )
335
+ cli :add_option (" -k, --long-key=VALUE" , " key description " , " " , callback )
335
336
local expected = { k = " myvalue" , [" long-key" ] = " myvalue" }
336
337
local result = cli :parse ({ " -k" , " myvalue" })
337
338
assert .are .same (expected , result )
338
- assert .are .equal (cb . key , " k" )
339
- assert .are .equal (cb . value , " myvalue" )
340
- assert .are .equal (cb . altkey , " long-key" )
339
+ assert .are .equal (" k" , cb . key )
340
+ assert .are .equal (" myvalue" , cb . value )
341
+ assert .are .equal (" long-key" , cb . altkey )
341
342
end )
342
343
343
344
it (" tests expanded-key option" , function ()
344
- cli :add_option (" -k, --long-key=VALUE" , " key descriptioin " , " " , callback )
345
+ cli :add_option (" -k, --long-key=VALUE" , " key description " , " " , callback )
345
346
local expected = { k = " val" , [" long-key" ] = " val" }
346
347
local result = cli :parse ({ " --long-key" , " val" })
347
348
assert .are .same (expected , result )
348
- assert .are .equal (cb . key , " long-key" )
349
- assert .are .equal (cb . value , " val" )
350
- assert .are .equal (cb . altkey , " k" )
349
+ assert .are .equal (" long-key" , cb . key )
350
+ assert .are .equal (" val" , cb . value )
351
+ assert .are .equal (" k" , cb . altkey )
351
352
end )
352
353
353
354
it (" tests expanded-key flag with not short-key" , function ()
354
355
cli :add_flag (" --version" , " prints the version and exits" , callback )
355
356
local expected = { version = true }
356
357
local result = cli :parse ({ " --version" })
357
358
assert .are .same (expected , result )
358
- assert .are .equal (cb . key , " version" )
359
- assert .are .equal (cb .value , true )
360
- assert .are .equal (cb .altkey , nil )
359
+ assert .are .equal (" version" , cb . key )
360
+ assert .are .equal (true , cb .value )
361
+ assert .are .equal (nil , cb .altkey )
361
362
end )
362
363
363
364
it (" tests callback returning error" , function ()
364
365
cli :set_name (' myapp' )
365
- cli :add_option (" -k, --long-key=VALUE" , " key descriptioin " , " " , callback_fail )
366
+ cli :add_option (" -k, --long-key=VALUE" , " key description " , " " , callback_fail )
366
367
local result , err = cli :parse ({ " --long-key" , " val" }, true --[[ no print]] )
367
368
assert (result == nil , " Failure in callback returns nil" )
368
369
assert (type (err ) == " string" , " Expected an error string" )
369
370
assert .are .equal (err , " myapp: error: bad argument to --long-key; re-run with --help for usage." )
370
371
end )
371
372
end )
373
+
374
+ describe (" Tests argument parsing with callback" , function ()
375
+ local cb = {}
376
+
377
+ local function callback (key , value )
378
+ cb .key , cb .value = key , value
379
+ return true
380
+ end
381
+
382
+ local function callback_arg (key , value )
383
+ table.insert (cb , { key = key , value = value })
384
+ return true
385
+ end
386
+
387
+ local function callback_fail (key , value )
388
+ return nil , " bad argument for " .. key
389
+ end
390
+
391
+ before_each (function ()
392
+ cb = {}
393
+ end )
394
+
395
+ it (" tests one required argument" , function ()
396
+ cli :add_arg (" ARG" , " arg description" , callback )
397
+ local expected = { ARG = " arg_val" }
398
+ local result = cli :parse ({ " arg_val" })
399
+ assert .are .same (expected , result )
400
+ assert .are .equal (" ARG" , cb .key )
401
+ assert .are .equal (" arg_val" , cb .value )
402
+ end )
403
+
404
+ it (" tests required argument callback returning error" , function ()
405
+ cli :set_name (' myapp' )
406
+ cli :add_arg (" ARG" , " arg description" , callback_fail )
407
+ local expected = { ARG = " arg_val" }
408
+ local result , err = cli :parse ({ " arg_val" }, true --[[ no print]] )
409
+ assert (result == nil , " Failure in callback returns nil" )
410
+ assert (type (err ) == " string" , " Expected an error string" )
411
+ assert .are .equal (err , " myapp: error: bad argument for ARG; re-run with --help for usage." )
412
+ end )
413
+
414
+ it (" tests many required arguments" , function ()
415
+ cli :add_arg (" ARG1" , " arg1 description" , callback_arg )
416
+ cli :add_arg (" ARG2" , " arg2 description" , callback_arg )
417
+ cli :add_arg (" ARG3" , " arg3 description" , callback_arg )
418
+ local expected = { ARG1 = " arg1_val" , ARG2 = " arg2_val" , ARG3 = " arg3_val" }
419
+ local result = cli :parse ({ " arg1_val" , " arg2_val" , " arg3_val" })
420
+ assert .are .same (expected , result )
421
+ assert .are .same ({ key = " ARG1" , value = " arg1_val" }, cb [1 ])
422
+ assert .are .same ({ key = " ARG2" , value = " arg2_val" }, cb [2 ])
423
+ assert .are .same ({ key = " ARG3" , value = " arg3_val" }, cb [3 ])
424
+ end )
425
+
426
+ it (" tests one optional argument" , function ()
427
+ cli :optarg (" OPTARG" , " optional arg description" , nil , 1 , callback )
428
+ local expected = { OPTARG = " opt_arg" }
429
+ local result = cli :parse ({ " opt_arg" })
430
+ assert .are .same (expected , result )
431
+ assert .are .equal (" OPTARG" , cb .key )
432
+ assert .are .equal (" opt_arg" , cb .value )
433
+ end )
434
+
435
+ it (" tests optional argument callback returning error" , function ()
436
+ cli :set_name (' myapp' )
437
+ cli :optarg (" OPTARG" , " optinoal arg description" , nil , 1 , callback_fail )
438
+ local expected = { ARG = " arg_val" }
439
+ local result , err = cli :parse ({ " opt_arg" }, true --[[ no print]] )
440
+ assert (result == nil , " Failure in callback returns nil" )
441
+ assert (type (err ) == " string" , " Expected an error string" )
442
+ assert .are .equal (err , " myapp: error: bad argument for OPTARG; re-run with --help for usage." )
443
+ end )
444
+
445
+ it (" tests many optional arguments" , function ()
446
+ cli :optarg (" OPTARG" , " optional arg description" , nil , 3 , callback_arg )
447
+ local expected = { OPTARG = { " opt_arg1" , " opt_arg2" , " opt_arg3" } }
448
+ local result = cli :parse ({ " opt_arg1" , " opt_arg2" , " opt_arg3" })
449
+ assert .are .same (expected , result )
450
+ assert .are .same ({ key = " OPTARG" , value = " opt_arg1" }, cb [1 ])
451
+ assert .are .same ({ key = " OPTARG" , value = " opt_arg2" }, cb [2 ])
452
+ assert .are .same ({ key = " OPTARG" , value = " opt_arg3" }, cb [3 ])
453
+ end )
454
+ end )
372
455
end )
0 commit comments