diff options
author | Ronan Collobert <ronan@collobert.com> | 2016-01-28 00:36:13 +0300 |
---|---|---|
committer | Ronan Collobert <ronan@collobert.com> | 2016-01-28 00:36:13 +0300 |
commit | b59c0e2dddc6937d40bb31850f729217874dc68e (patch) | |
tree | 06ecab6cd391fa90dc6e05d7dfbc7533925ca280 | |
parent | 1559919e6df60ec308fb8437daa07bf5217db0e9 (diff) |
show argument types when an error occurs
-rw-r--r-- | graph.lua | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -1,5 +1,10 @@ local usage = require 'argcheck.usage' local utils = require 'argcheck.utils' +local env = require 'argcheck.env' +local sdascii +pcall(function() + sdascii = require 'sundown.ascii' + end) local function argname2idx(rules, name) for idx, rule in ipairs(rules) do @@ -344,7 +349,7 @@ function ACN:apply(func) end end -function ACN:usage() +function ACN:usage(...) local txt = {} local history = {} self:apply( @@ -354,7 +359,16 @@ function ACN:usage() table.insert(txt, usage(self.rules)) end end) - return table.concat(txt, '\n\nor\n\n') + local args = {} + for i=1,select('#', ...) do + table.insert(args, string.format("**%s**", env.type(select(i, ...)))) + end + local render = sdascii and sdascii.render or function(...) return ... end + return string.format( + "%s\n%s\n", + table.concat(txt, '\n\nor\n\n'), + sdascii.render(string.format("*Got:* %s", table.concat(args, ', '))) + ) end function ACN:generate(upvalues) @@ -416,9 +430,9 @@ function ACN:generate(upvalues) end ) if quiet then - table.insert(code, ' return false, graph:usage()') + table.insert(code, ' return false, graph:usage(...)') else - table.insert(code, ' error(string.format("%s\\ninvalid arguments!", graph:usage()))') + table.insert(code, ' error(string.format("%s\\ninvalid arguments!", graph:usage(...)))') end table.insert(code, 'end') return table.concat(code, '\n') |