lunarmodules / ldoc

LDoc is a LuaDoc-compatible documentation generator which can also process C extension source. Markdown may be optionally used to render comments, as well as integrated readme documentation and pretty-printed example files.
https://lunarmodules.github.io/ldoc/
Other
787 stars 174 forks source link

Add support for default values for optional parameters to functions #57

Closed Yonaba closed 11 years ago

Yonaba commented 11 years ago

Hi Steve,

Feature request...I don't know if (syntactically speaking) there's already a convention style in Lua for this, but I wish LDoc can feature default values for optional parameters to function. Instead of writing something like this:

 ----
 -- slice like a ninja
 -- @param item the item
 -- @param[opt] weapon the weapon. Defaults to `katana` when not given
 funtion slice(item, weapon) .. end

Is it possible to have a syntax to specify weapon defaults to katana when omitted...? That would be very handy, to me. And it could render like this in the HTML output:

function splice(item [,weapon=katana])

Well that's just a suggestion, I let you be judge.

Best regards, Roland Y.

stevedonovan commented 11 years ago

I'm wondering what the doc comment should look like. Something like [opt='katana'] would be a logical extension. Or something like this:

   @param (default 'katana') .....

(inspired by how Lapp does it. But this may be a bit too free-form?)

stevedonovan commented 11 years ago

This is how I've done it (just pushed to master, ping me if you need a tag to download).

See e.g. tests/styles/four.lua

--- a function with typed args.
-- Note the the standard tparam aliases, and how the 'opt' and 'optchain'
-- modifiers may also be used. If the Lua function has varargs, then
-- you may document an indefinite number of extra arguments!
-- @string name person's name
-- @int age
-- @string[opt='gregorian'] calender optional calendar
-- @int[optchain=0] offset optional offset
-- @treturn string
function one (name,age,...)
end

Still find 'optchain' a bit hard to type. Perhaps if an 'opt' follows an 'opt' then it's implicitly 'optchain'?

Yonaba commented 11 years ago

It just works like a charm. Gratitude for the swiftness! I'll just close this issue. And for the 'optchain' stuff, I am thinking the same way. It can be inferred when the previous arg is opt.