argcheck
========
A powerful function argument checker and function overloading system for
Lua or LuaJIT.
`argcheck` generates specific code for checking arguments of a function. This
allows complex argument checking (possibly with optional values), with
little overhead (with [LuaJIT](http://luajit.org)). `argcheck` computes a
tree of all possible variants of arguments, allowing efficient overloading
and default argument management.
Installation
------------
The easiest is to use [luarocks](http://www.luarocks.org).
If you use Torch, simply do
```sh
luarocks install argcheck
```
else
```sh
luarocks build https://raw.github.com/torch/argcheck/master/rocks/argcheck-scm-1.rockspec
```
You can also copy the `argcheck` directory where `luajit` (or `lua`) will
find it.
Changelog
---------
- Version 2.0 (git)
- Rewrote completely the code generation.
- Now creates a tree of possible argument paths (much more efficient).
- Thanks to the tree approach, many bugs have been fixed.
- `argcheck` will produce an error if there are ambiguous argument rules.
- The feature `chain` is still deprecated (but available). Use `overload` instead.
- True overloading is happening. Contrary to `chain`, `overload` functions must be overwritten.
- Same functionalities than in 1.0, plus
- Handles named method calls
- Can generate a dot graphviz file of the argument paths for debugging purposes.
- Version 1.0
- Simplified the way of calling `argcheck`.
- Same functionalities than before, except named method call were not handled anymore.
- Added the `call` option to call a function if the arguments match given rules.
- Added the `chain` option to chain several `argcheck` function calls (a cheap version of overloading).
- Simplified the way of adding a type.
- Version 0.5
- Handling of default arguments (including defaulting to another argument), optional arguments (which might be `nil`), named arguments, named only option.
- Complicated way to handle method and functions.
- Calling function is mandatory.
Documentation
------------
To use `argcheck`, you have to first `require` it:
```lua
local argcheck = require 'argcheck'
```
In the following, we assume this has been done in your script.
Note that `argcheck` does not import anything globally, to avoid cluttering
the global namespace. The value returned by the require is a function: for
most usages, it will be the only thing you need.
_Note that in the following examples we do not use local variables for
check functions or example functions. This is bad practice, but helpful if
you want to cut-and-paste the code in your interactive lua to see how
this is running._
The `argcheck()` function creates a fast pre-compiled function for checking
arguments, according to rules provided by the user. Assume you have a
function which requires a unique number argument:
```lua
function addfive(x)
print(string.format('%f + 5 = %f', x, x + 5))
end
```
You can make sure everything goes fine by creating the rule:
```lua
check = argcheck{
{name="x", type="number"}
}
function addfive(...)
local x = check(...)
print(string.format('%f + 5 = %f', x, x + 5))
end
```
If a user try to pass a wrong argument, too many arguments, or no arguments
at all, `argcheck` will complain:
```lua
arguments:
{
x = number --
}
stdin:2: invalid arguments
```
A rule must at least take a `name` field. The `type` field is optional
(even though it is highly recommended!). If `type` is not provided, `argcheck` will make
sure the given argument is not `nil`. If you want also to accept `nil` arguments, see the
[`opt` option](#argcheck.opt).
### Default arguments
Arguments can have defaults:
```lua
check = argcheck{
{name="x", type="number", default=0}
}
```
In which case, if the argument is missing, `argcheck` will pass the default
one to your function:
```lua
> addfive()
0.000000 + 5 = 5.000000
```
### Help (or doc)
`argcheck` encourages you to add help to your function. You can document each argument:
```lua
check = argcheck{
{name="x", type="number", default=0, help="the age of the captain"}
}
```
Or even document the function:
```lua
check = argcheck{
help=[[
This function is going to do a simple addition.
Give a number, it adds 5. Amazing.
]],
{name="x", type="number", default=0, help="the age of the captain"}
}
```
Then, if the user makes a mistake in the arguments, the error message
becomes more clear:
```lua
> addfive('')
stdin:2: invalid arguments
This function is going to do a simple addition.
Give a number, it adds 5. Amazing.
arguments:
{
[x = number] -- the age of the captain [default=0]
}
```
Note that is (equivalently) possible to use the key `doc=` instead of `help=`.
### Multiple arguments
Until now, our function had only one argument. Obviously, `argcheck` can
handle as many as you wish:
```lua
check = argcheck{
help=[[
This function is going to do a simple addition.
Give a number, it adds 5. Amazing.
]],
{name="x", type="number", default=0, help="the age of the captain"},
{name="msg", type="string", help="a message"}
}
function addfive(...)
local x, msg = check(...)
print(string.format('%f + 5 = %f', x, x + 5))
print(msg)
end
```
`argcheck` handles well various cases, including those where some arguments
with defaults values might be missing:
```lua
> addfive(4, 'hello world')
4.000000 + 5 = 9.000000
hello world
>
> addfive('hello world')
0.000000 + 5 = 5.000000
hello world
>
> addfive(4)
stdin:2: invalid arguments
This function is going to do a simple addition.
Give a number, it adds 5. Amazing.
arguments:
{
[x = number] -- the age of the captain [default=0]
msg = string -- a message
}
```
### Default argument defaulting to another argument
Arguments can have a default value coming from another argument, with the
`defaulta` option. In the following
```lua
check = argcheck{
{name="x", type="number"},
{name="y", type="number", defaulta="x"}
}
function mul(...)
local x, y = check(...)
print(string.format('%f x %f = %f', x, y, x * y))
end
```
argument `y` will take the value of `x` if it is not passed during the function call:
```lua
> mul(3, 4)
3.000000 x 4.000000 = 12.000000
> mul(3)
3.000000 x 3.000000 = 9.000000
```
### Default arguments function
In some more complex cases, sometimes one needs to run a particular function when
the given argument is not provided. The option `defaultf` is here to help.
```lua
idx = 0
check = argcheck{
{name="x", type="number"},
{name="y", type="number", defaultf=function() idx = idx + 1 return idx end}
}
function mul(...)
local x, y = check(...)
print(string.format('%f x %f = %f', x, y, x * y))
end
```
This will output the following:
```lua
> mul(3)
3.000000 x 1.000000 = 3.000000
> mul(3)
3.000000 x 2.000000 = 6.000000
> mul(3)
3.000000 x 3.000000 = 9.000000
```
<a name="argcheck.opt"/>
### Optional arguments
Arguments with a default value can be seen as optional. However, as they
do have a default value, the underlying function will never receive a `nil`
value. In some situations, one might need to declare an optional argument
with no default value. You can do this with the `opt` option.
```lua
check = argcheck{
{name="x", type="number", default=0, help="the age of the captain"},
{name="msg", type="string", help="a message", opt=true}
}
function addfive(...)
local x, msg = check(...)
print(string.format('%f + 5 = %f', x, x + 5))
print(msg)
end
```
In this example, one might call `addfive()` without the `msg` argument. Of
course, the underlying function must be able to handle `nil` values:
```lua
> addfive('hello world')
0.000000 + 5 = 5.000000
hello world
> addfive()
0.000000 + 5 = 5.000000
nil
```
### Torch Tensors
`argcheck` supports Torch `Tensors` type checks.
Specific tensor types like `Int`, `Float`, or `Double` can be
checked with `torch.<Type>Tensor`.
Any tensor type can be checked with `tor
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Torch7安装库(Ubuntu 20.04环境) (2000个子文件)
0261d9ebffb12c4ba4d1b516d593903ff8dfc8 282B
06a5f87ca86a3cdc09cfbb1037ce1ec555ee90 179B
0f63ff58147f65c9228ec396ba67d08fc59681 211B
lua.1 4KB
lua.1 4KB
luac.1 4KB
luac.1 4KB
luac.1 3KB
luac.1 3KB
luajit.1 2KB
luajit.1 2KB
lua.1 2KB
lua.1 2KB
10d6c58aa5975f176cd719880986e7941ccc22 341B
118c1d7e392043c0f85974142b8399b8ee7204 326B
129a504ea6d580f12873940da0df1eef0a596c 241B
1345fb0c6395b94aa5eb71a1315ae215a23d7c 286B
16b07233483dc7c688c40d2e6f2d0c5ade2c78 621B
20642f1afb40e7ff17e6c7e6e3721760393f42 55B
2b877361ce8692e4a218cb6e7d51243e9b75cc 53B
2e9365e6a06fbdda863cffc2978c078d073029 195B
312349a316f693410c3a467aad96b305dfe402 241B
315ea4381250236f10153d32a2ba3746a9f509 752B
39ea57c49458481fc368eddc66e1095355f627 238B
40bd55c2891e497f6f554b364075467ddddfb7 238B
4706c0768d1b447e0d1c9e69e3e18135983f67 182B
478d95e754cc5aacac4d0a0897303c8b4be5b3 171B
4a7db9632843c57f05f0a135dc58979e7b0840 132B
4eac72bbd504969cbfbd6574066d9984f69d02 95B
4fe515c466302252a3f85f32cbfeea55898e8b 353B
5245cbbbffb984ca770253c5c76081032655a0 248B
537ffe91ba33429cdcf5ed424e770d04749586 230B
54c15918e7a1daff5eb2a701e5f971a1e3d7a8 182B
564a1cc0b41c16cc8faa16c8987f7d222ffe92 166B
56e1378146853c13189df9db7f8d844cf816e1 3KB
59f92a8fab827aad76c04be2756e1ea53dc067 67B
5b5a143482857f80237181d5fde0a3ba20477b 234B
5d46c5a56f4c0d9f9031752f6b392726ccb83a 163B
608098586e8ccfc0292df382ff797b39c1990e 224B
652ee0b34dab764c9723900d36403380f5db3b 133B
654e202ecdff8edfd1f5aeb708a39150974a20 67B
6686b6febc352465e07ccb29404ff1aae1c7d4 241B
68b0ff92713fdb1787756ef6b4d87ea974412a 1KB
6d4b962c1f75e9827f6a9b83ab9649b3992168 241B
6fc60aeefb4a24f673272a59fd183841082071 772B
740b25db95239c4f23458c96c0c69464b122b2 139B
7609b1234a9b8806c5a05da6c866e480aa148d 22B
7c05d562dba79be3d424b74b9deba0c3404130 210B
7c34117e163c006f0ab4bb315748b85e381c98 152B
7c93f930068b8fb43e4257cd9e4e3c27bb3bdb 288B
7cf5e05ce6b62816fce07001fbc8661d8ee61c 3KB
7d4dd4caba376ea11c0bdd573872a15fa8975e 278B
802510fab1a78e21031866e2769916649c24a5 377B
8269b85d557d307a68bfdde7073f530d5cafb3 241B
8424ac5b638686410304c60c31693849f3d1dd 253B
8773ade06fd3591dcdf400e719eaf0cd00ffdf 241B
8e039122870f09a3d6ea82302957c7b6fbf54d 241B
934d8171bb2286522163ae84885bfcb20dca39 212B
9526db68f7e41856b55c3d094b87488f3fef66 241B
963cc5f199b5e7a6d52464f1cbab40255d5d4c 320B
96a4d4f44cd0c8069ca7ae8a72b901f3f146ff 53B
9df996a5971176db83a3b6d4ce818a81501818 133B
9dfbc38f8395d14f4d347e737c4a68b69536c0 182B
9e2918ee481f3a22a73408e224f8075ccc5dff 3KB
9ec7c8ec749fd1b744aac7a3a643a34389d370 256B
a41a701cd04519fe0b490e88b320877450afc5 621B
acb6d88d4a05aa22d41651983d8d4713237d99 3KB
manual.adoc 17KB
performance.adoc 4KB
README.adoc 822B
b41a55f2ccaa84b210653a2d76733662ac4a70 170B
b98dd4ed82065f0e7c5407949abb460ed6774d 363B
bac36fc16193b958ea4f4d38433a90275533d3 245B
install.bat 36KB
install-deps.bat 18KB
install.bat 4KB
install.bat 4KB
msvcbuild.bat 4KB
msvcbuild.bat 4KB
ps4build.bat 3KB
ps4build.bat 3KB
xb1build.bat 3KB
xedkbuild.bat 3KB
xedkbuild.bat 3KB
psvitabuild.bat 3KB
psvitabuild.bat 3KB
test.bat 3KB
build.bat 3KB
msvcbuild.bat 2KB
luarocksw.bat 1KB
luavs.bat 1KB
luavs.bat 1KB
uninstall.bat 947B
clean.bat 899B
generate_call_h.bat 273B
testing.bat 231B
luarocks-admin.bat 82B
luarocks.bat 76B
bc0c78184f1f44b669cfce77562e26316ed177 296B
bdedd9a3e448275a0eec85b9bc894cda96eb44 95B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
AI布道
- 粉丝: 184
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功