torch / argcheck

A powerful (and blazing fast) argument checker and function overloading system for Lua or LuaJIT
Other
54 stars 17 forks source link

Missing argument description when help main field is provided #12

Open Atcold opened 8 years ago

Atcold commented 8 years ago

In the documentation we have

> 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]
}

in my terminal I get

th> addfive('')
[string "argcheck"]:49: 
This function is going to do a simple addition. Give a number, it adds 5.
Amazing.

Got: string
invalid arguments!
stack traceback:
        [C]: in function 'error'
        [string "argcheck"]:49: in function 'check'
        [string "function addfive(...)..."]:2: in function 'addfive'
        [string "_RESULT={addfive('')}"]:1: in main chunk
        [C]: in function 'xpcall'
        /Users/atcold/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
        ...cold/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
        [C]: at 0x010ca74630
                                                                      [0.0009s]
  1. So, the last part, with the argument explanation, is missing.
  2. Moreover, it looks like the \n is ignored.

Also ? addfive does not show the documentation. How do I get to see it?

In the same way, for the multi argument case, on the documentation it's shown that one may expect an explanation like the following

arguments:
{
  [x   = number]  -- the age of the captain [default=0]
   msg = string   -- a message
}

where one knows that msg does not have any default value, but what I get instead is

Got: number
invalid arguments!
stack traceback:
        [C]: in function 'error'
        [string "argcheck"]:74: in function 'check'
        [string "function addfive(...)..."]:2: in function 'addfive'
        [string "_RESULT={addfive(4)}"]:1: in main chunk
        [C]: in function 'xpcall'
        /Users/atcold/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
        ...cold/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
        [C]: at 0x010ca74630
Atcold commented 8 years ago

OK, found the bug. It's about the help field. If it is not specified, everything works as expected

th> check = argcheck{
   {name="x", type="number", default=0, help="the age of the captain"},
   {name="msg", type="string", help="a message"}
}
                                                                      [0.0010s]
th> check(4)
[string "argcheck"]:74: 
Arguments:

({
  [x   = number]  -- the age of the captain [default=0]
   msg = string   -- a message
})

Got: number
invalid arguments!
stack traceback:
        [C]: in function 'error'
        [string "argcheck"]:74: in function 'check'
        [string "_RESULT={check(4)}"]:1: in main chunk
        [C]: in function 'xpcall'
        /Users/atcold/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
        ...cold/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
        [C]: at 0x010ca74630
                                                                      [0.0005s]

If it is specified, then it breaks without knowledge of what went wrong

th> 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"}
}
                                                                      [0.0012s]
th> check(4)
[string "argcheck"]:74: 
This function is going to do a simple addition. Give a number, it adds 5.
Amazing.

Got: number
invalid arguments!
stack traceback:
        [C]: in function 'error'
        [string "argcheck"]:74: in function 'check'
        [string "_RESULT={check(4)}"]:1: in main chunk
        [C]: in function 'xpcall'
        /Users/atcold/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
        ...cold/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
        [C]: at 0x010ca74630
                                                                      [0.0006s]