Now I'm not really sure what you're asking… Do you want to write an API or a program that accepts arguments? Whatever, the latter seems more likely.
Vararg is one of the best things that come with Lua. We have it because when Lua passes variables between functions and such, it doesn't actually pass single variables but variable lists. That's why you can do things like:
local a, b, c = 1, 2, 3
We made a list there (1, 2, 3), and when we assign another list (a, b, c) to it, the first element of the (a, b, c) list gets assigned to the first element of the (1, 2, 3) list, the second to the second, and so on. In the end we have a assigned to 1, b assigned to 2 and c assigned to 3.
Now where do functions come in? Well, when you pass arguments to a function, you do essentially the same thing you did up there with a, b and c:
local function something(a, b, c)
print(a )
print(b )
print(c )
end
something(1, 2, 3)
When you're writing a program, you're actually writing a big function, with the difference that you can't see the enclosing
function(arguments) … end part. That's the point - if we had that part up there, we could write programs that accept arguments, like a function that accepts arguments. And here comes the vararg. This is a vararg function:
local function something(...)
local args = {...}
print(#args)
-- I really mean it, the "..." really is proper syntax
end
… represents a list here - we put it into a table, and *magic* we have a table that contains the arguments passed to the function!
Fortunately, CC Lua is kind enough (it's actually the
loadstring method which's this kind) to automatically make our programs vararg functions, so we can use the
… in our programs:
local args = {...}
print(args[1] or "Got no argument")
And there, we have a program that accepts arguments.