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

Help With Targs

Started by ATMunn, 29 October 2013 - 10:49 AM
ATMunn #1
Posted 29 October 2013 - 11:49 AM
So, I'm wanting to figure out how to use tArgs. I made a simple program that appears to output "0" if I didn't type anything after the program name, or "1" if I did. (Link to code: www.pastebin.com/VnebttDQ)
What I want to figure out is how to make the program do different things depending on what I put in. I know this is possible because I've seen other pepole do it, but I want to figure out how to do it myself.

If anyone knows how, and would be willing to help me, that would be greatly apreciated.
Thanks,
-ATMunn
Cranium #2
Posted 29 October 2013 - 04:56 PM
You want to use vararg expressions such as this:

local tArgs = {...} --#this will create a table named tArgs, with the contents of the arguments you enter at runtime in individual indices.
Put that at the top of your code before calling to it, and you will be able to use arguments with your programs like this:

if tArgs[1] then print("There was an argument entered at runtime: "..[tArgs[1]) end
Bomb Bloke #3
Posted 29 October 2013 - 05:16 PM
Looks like you accidentally put an extra [ in that second line.

It's worth noting that every argument (or "parameter") you pass to your script this way ends up being treated as a string (text). Since you'll often want numbers instead, you have to convert them.

Say you save this in a script called "add":

local tArgs = {...}

if #tArgs ~= 2 then   -- Checks how many entries are in the "tArgs" table, as this example calls for two.
  error("Gimme TWO numbers as arguments!")
end

tArgs[1] = tonumber(tArgs[1])  -- If a number wasn't entered, then the conversion 
tArgs[2] = tonumber(tArgs[2])  -- fails and "tonumber" returns nil.

if not tArgs[1] or not tArgs[2] then  -- Variables set to nil are treated as false.
  error("Gimme two NUMBERS as arguments!")
end

print(tArgs[1].." plus "..tArgs[2].." equals "..tArgs[1]+tArgs[2])

… you could then run it with a command like eg "add 64 25".
ATMunn #4
Posted 29 October 2013 - 05:27 PM
I understand it now. Thanks for the help!
spdkils #5
Posted 29 October 2013 - 05:36 PM
I like to rename and check my args at the top…


local tArgs = { ... }

local depth = tonumber(tArgs[1])
local width = tonumber(tArgs[2])

if not depth or not width then
print("what are you doing?!?!?!")
return
end

print(width * depth)
Bomb Bloke #6
Posted 29 October 2013 - 05:42 PM
Might want to make that an "or" instead of an "and".
spdkils #7
Posted 29 October 2013 - 06:10 PM
yeah. :D/> or
ATMunn #8
Posted 29 October 2013 - 08:48 PM
Good idea! I might use that too in my programs :)/>
theoriginalbit #9
Posted 29 October 2013 - 09:01 PM
alternatively to reduce on code and to improve readability you can also do


if not (depth and width) then
  print("What are you doing?!?!")
  return
end
spdkils #10
Posted 29 October 2013 - 09:24 PM
Yeah, I had it in my brain both ways… I knew I wanted to and them, to ensure both were true, but then thru out the not because wasn't thinking, and then because I was notting, then I had to OR, then … well. I should just think before I type. What are we saying?
Zudo #11
Posted 30 October 2013 - 02:17 AM
BTW, you don't need to call it tArgs, that is just a thing that the CC developers did.


local arguments = {...}

for k, v in pairs(arguments) do
 print(v)
end

That still works.
Engineer #12
Posted 30 October 2013 - 03:47 AM
BTW, you don't need to call it tArgs, that is just a thing that the CC developers did.


local arguments = {...}

for k, v in pairs(arguments) do
print(v)
end

That still works.
The developers did that because its the best way of naming things imo.

tArgs
table
Arguments

Makes sense, right? Its the same as sPath:

sPath
string
path

and bSucces:

boolean
succes
ATMunn #13
Posted 30 October 2013 - 12:21 PM
Oh, I didn't know that! ok thanks :P/>