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

Checking for missing arguments

Started by ChaosNicro, 17 April 2016 - 04:18 PM
ChaosNicro #1
Posted 17 April 2016 - 06:18 PM
Hey there,

I was writing a help function for one of my programs that would be displayed whenever no arguments were provided.
This is what I tried:

local arg = (...)
-- help
if arg[1] == nil then
print("text")
error()
end

The result was that calling the program without any arguments would return an "Attempt to call index? (a nil value)" error
while literally anything as an argument (or multiple for that matter) would trigger the if-statement and display the text.

The first reaction I can understand as I did attempt to call a nil value but the other I'm confused by.
How can I correctly check for a nil value when I expect it to be there?
Dog #2
Posted 17 April 2016 - 06:20 PM
You're using the wrong brackets for your first line. To encapsulate the results in a table you need to use {} instead

local arg = { ... }
ChaosNicro #3
Posted 17 April 2016 - 06:24 PM
You're using the wrong brackets for your first line. To encapsulate the results in a table you need to use {} instead

local arg = { ... }

Did not see that…
Thanks for the quick answer. ^_^/>