18 posts
Posted 02 May 2013 - 06:35 AM
I want the user to be forced to input two arguments like this "usage: strip <length> <amount>"
I went into the turtle tunneling program and found this;
local tArgs = {...}
if #tArgs ~=1 then
print( "Usage: strip <length>" )
return
end
local length = tonumber( tArgs[1] )
if length < 1 then
print( "Strip length must be positive" )
return
end
How would I add a second argument to this?
671 posts
Location
That place over there. Y'know. The one where I am.
Posted 02 May 2013 - 06:38 AM
local tArgs = {...}
if #tArgs ~= 2 then
print( "Usage: strip <length>" )
return
end
local length = tonumber( tArgs[1] )
if length < 1 then
print( "Strip length must be positive" )
return
end
-- Not sure what you want to do with the second argument.
What do you want the second argument to do?
799 posts
Location
Land of Meh
Posted 02 May 2013 - 06:40 AM
Well, in that example, tArgs is a variable that contains all the arguments that were passed to the program. The # operator (seen on the second line) is used to find out how many items are in the tArgs table. If we have the required number of arguments, we can proceed with the program.
So in your case for 2 arguments:
-- Get the arguments passed to the program
local args = {...}
-- Check if there is less than 2 arguments
if #args < 2 then
print("Usage: strip <length> <amount>")
return
end
-- Continue with the program
local length = args[1] -- the first argument passed
local amount = args[2] -- the second argument passed
-- You can do some checking here
if not tonumber(length) or tonumber(length) < 1 then
print("length must be a positive number")
return
end
if not tonumber(amount) or tonumber(amount) < 1 then
print("amount must be a positive number")
return
end
length = tonumber(length)
amount = tonumber(amount)
7508 posts
Location
Australia
Posted 02 May 2013 - 06:43 AM
-- Check if there is less than 2 arguments
if #args < 2 then
I would be more inclined to use
if #args ~= 2 then
that way if they have too many args it tells them the usage as well.
18 posts
Posted 02 May 2013 - 11:05 AM
Well, in that example, tArgs is a variable that contains all the arguments that were passed to the program. The # operator (seen on the second line) is used to find out how many items are in the tArgs table. If we have the required number of arguments, we can proceed with the program.
So in your case for 2 arguments:
-- Get the arguments passed to the program
local args = {...}
-- Check if there is less than 2 arguments
if #args < 2 then
print("Usage: strip <length> <amount>")
return
end
-- Continue with the program
local length = args[1] -- the first argument passed
local amount = args[2] -- the second argument passed
-- You can do some checking here
if not tonumber(length) or tonumber(length) < 1 then
print("length must be a positive number")
return
end
if not tonumber(amount) or tonumber(amount) < 1 then
print("amount must be a positive number")
return
end
length = tonumber(length)
amount = tonumber(amount)
Thanks this was exactly what I was looking for (a quick explanation of the parts and how to code it).
2088 posts
Location
South Africa
Posted 02 May 2013 - 11:13 AM
-- Check if there is less than 2 arguments
if #args < 2 then
I would be more inclined to use
if #args ~= 2 then
that way if they have too many args it tells them the usage as well.
Just use the first 2 provided arguments :P/>
18 posts
Posted 02 May 2013 - 12:00 PM
-- Get the arguments passed to the program
local args = {...}
-- Check if there is less than 2 arguments
if #args < 2 then
print("Usage: strip <length> <amount>")
return
end
-- Continue with the program
local length = args[1] -- the first argument passed
local amount = args[2] -- the second argument passed
-- You can do some checking here
if not tonumber(length) or tonumber(length) < 1 then
print("length must be a positive number")
return
end
if not tonumber(amount) or tonumber(amount) < 1 then
print("amount must be a positive number")
return
end
length = tonumber(length)
amount = tonumber(amount)
ok so now it is saying that it is expecting a number at this point: (if not tonumber(amount) or tonumber(amount) < 1 then)
It's as if the second number in the sequence doesn't get registered as a number.
"turtle:18: Expected number"
Heres my full program:
http://pastebin.com/6zpnbGYT
799 posts
Location
Land of Meh
Posted 02 May 2013 - 12:47 PM
Hmmm. Try replacing the number checking bit (starting line 9) with this:
-- You can do some checking here
if length and (not tonumber(length) or tonumber(length) < 1) then
print("length must be a positive number")
return
end
if amount and (not tonumber(amount) or tonumber(amount) < 1) then
print("amount must be a positive number")
return
end
Not sure if that will work. What did you type in as the arguments to the program to cause the error?
18 posts
Posted 02 May 2013 - 01:49 PM
Hmmm. Try replacing the number checking bit (starting line 9) with this:
-- You can do some checking here
if length and (not tonumber(length) or tonumber(length) < 1) then
print("length must be a positive number")
return
end
if amount and (not tonumber(amount) or tonumber(amount) < 1) then
print("amount must be a positive number")
return
end
Replacing the section starting from line 9 yielded the error:
"turtle:127: 'for' limit must be a number". Meaning it still isn't reregistering as a number.
I think you meant line replace it with the checking from at line 13. That still reads:
"turtle:18: Expected number"Not sure if that will work. What did you type in as the arguments to the program to cause the error?
strip 10 10
18 posts
Posted 03 May 2013 - 03:32 AM
Maybe someone could shed a new light on my problem?
@theoriginalbit
@remiX
7508 posts
Location
Australia
Posted 03 May 2013 - 03:53 AM
Maybe someone could shed a new light on my problem?
@theoriginalbit
Not too sure sorry, GravityScore's solution should work perfectly…
local args = {...}
if #args ~= 2 then
print('Usage: strip <length> <amount>')
end
local length = tonumber(args[1])
local amount = tonumber(args[2])
if not length or length < 1 then
print('Length must be a number above 1')
end
if not amount or amount < 1 then
print('Amount must be a number above 1')
end