diff options
author | Ronan Collobert <ronan@collobert.com> | 2016-01-28 04:43:11 +0300 |
---|---|---|
committer | Ronan Collobert <ronan@collobert.com> | 2016-01-28 04:43:11 +0300 |
commit | 1f1a43685f6fba2f4e27b7e305ea68a4037747c1 (patch) | |
tree | aa2b44efb3651882fc3f45f732de8d5cf9c831b7 /usage.lua | |
parent | e6d8fe4f63715601afc7f45eb7b1913dfc5f12f7 (diff) |
improve usage message
Diffstat (limited to 'usage.lua')
-rw-r--r-- | usage.lua | 89 |
1 files changed, 69 insertions, 20 deletions
@@ -1,3 +1,4 @@ +local env = require 'argcheck.env' local sdascii pcall(function() sdascii = require 'sundown.ascii' @@ -19,6 +20,10 @@ end local function generateargt(rules) local txt = {} table.insert(txt, '```') + table.insert(txt, string.format( + '%s%s', + rules.noordered and '' or '(', + rules.nonamed and '' or '{')) local size = 0 for _,rule in ipairs(rules) do @@ -33,7 +38,6 @@ local function generateargt(rules) .. (rule.type and (' = ' .. rule.type) or '') .. ((rule.opt or rule.default ~= nil or rule.defaulta or rule.defaultf) and ']' or '') ) - local default = '' if rule.defaulta then default = string.format(' [defaulta=%s]', rule.defaulta) @@ -61,6 +65,10 @@ local function generateargt(rules) for i=1,#arg do table.insert(txt, string.format(" %s %s -- %s", arg[i], string.rep(' ', size-#arg[i]), hlp[i])) end + table.insert(txt, string.format( + '%s%s', + rules.nonamed and '' or '}', + rules.noordered and '' or ')')) table.insert(txt, '```') txt = table.concat(txt, '\n') @@ -68,30 +76,71 @@ local function generateargt(rules) return txt end -local function usage(rules) - local doc = rules.help or rules.doc +local function usage(truth, rules, ...) + if truth then + local doc = rules.help or rules.doc - if doc then - doc = doc:gsub('@ARGP', - function() - return generateargp(rules) - end) + if doc then + doc = doc:gsub('@ARGP', + function() + return generateargp(rules) + end) - doc = doc:gsub('@ARGT', - function() - return generateargt(rules) - end) - end + doc = doc:gsub('@ARGT', + function() + return generateargt(rules) + end) + end - if not doc then - doc = '\n*Arguments:*\n' .. generateargt(rules) - end + if not doc then + doc = '\n*Arguments:*\n' .. generateargt(rules) + end - if sdascii then - doc = sdascii.render(doc) - end + if sdascii then + doc = sdascii.render(doc) + end - return doc + return doc + else + local self = rules + local args = {} + for i=1,select('#', ...) do + table.insert(args, string.format("**%s**", env.type(select(i, ...)))) + end + local argtblidx + if self:hasruletype('N') then + if select("#", ...) == 1 and env.istype(select(1, ...), "table") then + argtblidx = 1 + end + elseif self:hasruletype('M') then + if select("#", ...) == 2 and env.istype(select(2, ...), "table") then + argtblidx = 2 + end + end + if argtblidx then + local argtbl = {} + local tbl = select(argtblidx, ...) + local n = 0 + for k,v in pairs(tbl) do + n = n + 1 + if n > 20 then + table.insert(argtbl, '...') + break + end + if type(k) == 'string' then + table.insert(argtbl, string.format("**%s=%s**", k, env.type(v))) + else + table.insert(argtbl, string.format("**[%s]**=?", env.type(k))) + end + end + args[argtblidx] = string.format("**table**={ %s }", table.concat(argtbl, ', ')) + end + local doc = string.format("*Got:* %s", table.concat(args, ', ')) + if sdascii then + doc = sdascii.render(doc) + end + return doc + end end return usage |