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

Give Arguments To A Program

Started by Binarin, 27 February 2012 - 09:00 PM
Binarin #1
Posted 27 February 2012 - 10:00 PM
This should be a simple question, but I didn't find the question anywhere.
How do I write a program that demands arguments?

Like, for example, excavate that prints "Usage: excavate <radius>" if you don't give it the radius when you start it.
Liraal #2
Posted 27 February 2012 - 10:04 PM
use variablename={…} to get a table with every argument give to the program.
Binarin #3
Posted 27 February 2012 - 10:13 PM
Ah, thanks I got it to work.
ChaosBeing #4
Posted 27 February 2012 - 10:22 PM
EDIT:
Ah, Ninja'd. :P/>/>

To make a program require a certain number of arguments, do something like this:


args = {...}

function printHelp()
print("Usage:  MYProgram Arg1 Arg2 etc...")
end

argsReq = 2   --2 is an example, here.

if #args ~= argsReq then
return printHelp()   -- When not in a function, return acts more like "quit"
		  -- but in the case of returning a function, it means "do this then quit".
end

--Continue program

To get arguments, type args[argNum]
EX: firstArg = args[1] would return "Test" if you ran "progName Test"
Binarin #5
Posted 28 February 2012 - 08:32 AM
Thanks you too :(/>/>
That's actually how I solved it.