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

[Lua] Working with and adding arguments?

Started by fauxiss, 02 May 2013 - 04:35 AM
fauxiss #1
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?
Shnupbups #2
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?
GravityScore #3
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)
theoriginalbit #4
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.
fauxiss #5
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).
remiX #6
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/>
fauxiss #7
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
GravityScore #8
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?
fauxiss #9
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
fauxiss #10
Posted 03 May 2013 - 03:32 AM
Maybe someone could shed a new light on my problem?
@theoriginalbit
@remiX
theoriginalbit #11
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