This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LDShadowLord's profile picture

Check arguments passed?

Started by LDShadowLord, 15 December 2013 - 12:35 PM
LDShadowLord #1
Posted 15 December 2013 - 01:35 PM
Does anyone know if there is a way I can see how many arguments are being passed into a variable? Without needing to individually check each and every single argument?

I ask because currently I use that system of checking each and every variable…

function cSend(localf,server)
	if localf = nil then
		print("Incorrect arguments: localname")
		return
	elseif server = nil then
		print("Incorrect argument: servername")
		return
	end
...

But I would prefer to use the system of checking how many arguments it receives (as you do with tArgs=…)

if #tArgs < 1 or #tArgs > 4 then

Any information would be appreciated, thanks.
MKlegoman357 #2
Posted 15 December 2013 - 02:09 PM
You can use varargs (…) system here:


local function test (...)
  --// When dealing with varargs (those three dots) Lua automatically creates a local table called 'args'
  --// It is the same as doing this:
  local args = {...}
  args.n = #args

  print(args.n) --> Prints how many arguments there are

  for i, v in ipairs(args) do --// Prints all the given arguments
    print(v)
  end
end
LDShadowLord #3
Posted 15 December 2013 - 02:40 PM
Thanks :)/>
jay5476 #4
Posted 16 December 2013 - 05:53 AM
they way lua works you can do

if not value then
 stuff
end
or even this

value = value or DEFAULT_VALUE
Alice #5
Posted 16 December 2013 - 12:36 PM
The print value uses this iirc.

print = function ( ... )
local args = {...}
for i=1, #args do
printstuffandloadspace(args[i])
end
linereturncode
end