Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/argcheck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonan Collobert <ronan@collobert.com>2016-01-28 04:43:11 +0300
committerRonan Collobert <ronan@collobert.com>2016-01-28 04:43:11 +0300
commit1f1a43685f6fba2f4e27b7e305ea68a4037747c1 (patch)
treeaa2b44efb3651882fc3f45f732de8d5cf9c831b7
parente6d8fe4f63715601afc7f45eb7b1913dfc5f12f7 (diff)
improve usage message
-rw-r--r--graph.lua17
-rw-r--r--usage.lua89
2 files changed, 73 insertions, 33 deletions
diff --git a/graph.lua b/graph.lua
index 94108a4..32e3a0c 100644
--- a/graph.lua
+++ b/graph.lua
@@ -1,10 +1,5 @@
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
@@ -356,18 +351,14 @@ function ACN:usage(...)
function(self)
if self.rules and not history[self.rules] then
history[self.rules] = true
- table.insert(txt, usage(self.rules))
+ table.insert(txt, usage(true, self.rules))
end
- end)
- 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
+ end
+ )
return string.format(
"%s\n%s\n",
table.concat(txt, '\n\nor\n\n'),
- sdascii.render(string.format("*Got:* %s", table.concat(args, ', ')))
+ usage(false, self, ...)
)
end
diff --git a/usage.lua b/usage.lua
index 8a272ff..d4090b8 100644
--- a/usage.lua
+++ b/usage.lua
@@ -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