Skip to content

mah0x211/lua-argexpected

Repository files navigation

lua-argexpected

test codecov

Helper module to check the function arguments.

Installation

luarocks install argexpected

Usage

local argexpected = require('argexpected')

--- @param a string
--- @param b number
local function foo(a, b)
    argexpected(type(a) == 'string', 1, 'string expected, got %s', type(a))
    argexpected(type(b) == 'number', 2, 'number expected, got %s', type(b))
end

-- following call will throw an error
--  bad argument #2 to 'foo' (number expected, got string)
foo('hello', 'world') 

argexpected(cond, idx, extramsg, ...)

Checks whether cond is true. If it is not, raises an error reporting a problem with extramsg as a comment:

bad argument #<idx> to 'funcname' (<extramsg>)

Parameters

  • cond:boolean: if true, does nothing, otherwise raises an error with the message described above.
  • idx:number: index of the argument.
  • extramsg:string: extra message to be shown in the error message.
  • ...: arguments to be passed to https://github.com/mah0x211/lua-string-format to format extramsg.

License

MIT

About

Helper module to check the function arguments.

Resources

License

Stars

Watchers

Forks