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>2014-03-11 19:29:43 +0400
committerRonan Collobert <ronan@collobert.com>2014-03-11 19:29:43 +0400
commit6c963b88b03caa48a3195705063b27bbbdbd4446 (patch)
tree135b01f6439cd584c68b960db9512bf21b7330e7
parenta6ef0ffe61ae71445edd050fbc6b41c4a3633605 (diff)
added support for global "call" option
-rw-r--r--README.md26
-rw-r--r--init.lua22
2 files changed, 41 insertions, 7 deletions
diff --git a/README.md b/README.md
index 27e96b5..9fc5c82 100644
--- a/README.md
+++ b/README.md
@@ -397,6 +397,32 @@ false arguments:
}
```
+#### Function call
+
+In case of success, the argument checker can call a function if
+needed. Some users might find it more convenient than calling the argument
+checker inside the function of interest. Taking back the first example, one
+could use the `call` option and rewrite it as:
+```lua
+addfive = argcheck{
+ {name="x", type="number"},
+
+ call = function(x)
+ print(string.format('%f + 5 = %f', x, x+5))
+ end
+}
+
+> addfive(5)
+5.000000 + 5 = 10.000000
+
+> addfive()
+stdin:1: arguments:
+{
+ x = number --
+}
+```
+
+
#### Debug
Adding `debug=true` as global option will simply dump the corresponding code
diff --git a/init.lua b/init.lua
index a9dad42..13c8aaa 100644
--- a/init.lua
+++ b/init.lua
@@ -201,6 +201,9 @@ local function argcheck(rules)
-- upvalues
table.insert(txt, 'local isoftype')
table.insert(txt, 'local usage')
+ if rules.call then
+ table.insert(txt, 'local call')
+ end
for ridx, rule in ipairs(rules) do
if rule.default or rule.defaultf then
table.insert(txt, string.format('local arg%dd', ridx))
@@ -226,14 +229,15 @@ local function argcheck(rules)
table.insert(ret, string.format('arg%d', ridx))
end
end
+ ret = table.concat(ret, ', ')
if rules.pack then
- ret = table.concat(ret, ', ')
- ret = (rules.quiet and 'true, ' or '') .. '{' .. ret .. '}'
- elseif rules.quiet then
- table.insert(ret, 1, 'true')
- ret = table.concat(ret, ', ')
- else
- ret = table.concat(ret, ', ')
+ ret = '{' .. ret .. '}'
+ end
+ if rules.call then
+ ret = 'call(' .. ret .. ')'
+ end
+ if rules.quiet then
+ ret = 'true' .. (#ret > 0 and ', ' or '') .. ret
end
table.insert(txt, ' local narg = select("#", ...)')
@@ -286,6 +290,10 @@ local function argcheck(rules)
debug.setupvalue(func, upvalue2idx(func, 'usage'), generateusage(rules))
+ if rules.call then
+ debug.setupvalue(func, upvalue2idx(func, 'call'), rules.call)
+ end
+
return func
end