Jump to content




i need help with arguements badly


  • You cannot reply to this topic
4 replies to this topic

#1 cheekycharlie101

  • Members
  • 231 posts

Posted 13 September 2012 - 07:50 PM

ok, so i want to make programs with arguements. a prime example would be the tunnel program built into a mining turtle. like you type
tunnel <length>

how does the <length> bit work !?!
please can someone show me a tutorial or explain to me in detail because i have no idea. i really wanna know how this is done and i know its probaly way above my level but i really wanna learn the language of lua. please someone help !
thanks -Cheeky

#2 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 13 September 2012 - 08:16 PM

2 ways, function or asking for input.

function tunnel(lenght)
for i = 1, lenght do
turtle.digUp()
turtle.dig
turtle.forward()
end
end
2:
write("lenght: ")
lenght = tonumber(read())
for i = 1, lenght do
turtle.digUp()
turtle.dig
turtle.forward()
end


#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 13 September 2012 - 08:26 PM

You can also do this:
local tArgs = {...} --takes any arguments from starting the program
if tArgs[1] == "argumentvalue" then --can be any number of arguments, tArgs[1],[2],etc
--do stuff here
elseif tArgs[2] == "argumentvalue2" then
--do other stuff here
end
Each argument is separated into table cells. Such as for "copy program1 program2", would run the "copy" program, with tArgs[1] being program1, and tArgs[2] being program2.

#4 cheekycharlie101

  • Members
  • 231 posts

Posted 14 September 2012 - 06:18 PM

er thanks, il try this out

#5 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 15 September 2012 - 05:22 AM

basically if you make a program with the code:
local tArgs={...}
print(tArgs[1])
name the program test and then enter in

test argument1 argument2

then it will only print 'argument1' as that is the first argument, a good way to use both methods is this:

Program usage: tunnel <length>

local tArgs={...}
if tArgs[1] then
  length=tonumber(tArgs[1])
else
  print('you have not specified a length, enter it now')
  write('Length: ')
  length=tonumber(read())
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users