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

Make a Program with arguements

Started by Stormkrow, 01 December 2013 - 10:42 AM
Stormkrow #1
Posted 01 December 2013 - 11:42 AM
Hi Guys!

Just wanted to find out if any one could show me how to effectively make a program with arguments that I can load with os.loadAPI()
I already know how to make a function with arguements but I dont quite understand how to make a program with one!

Please Help Me!

Thanks!
Legato #2
Posted 01 December 2013 - 12:17 PM
Hm, I will admit that I\m not quite sure of how to use os.loadAPI(), however, I think that I know how to answer your question about arguments.
In short:

local args={...}
local varName1=tonumber(args[1])
local varName2=tonumber(args[2])
...
"local args={…}" gets the arguments you use when running the program, recorded as strings, which has to be converted to numbers (which I think is obvious since you have worked with function arguments, but I thought I should mention it anyway), and conveniently assigned to variables.
That… should be pretty much all there's to it, I think. I haven't really done a lot with lua, but my limited experience tells me that it works.
Was that what you where going for?
LBPHacker #3
Posted 01 December 2013 - 12:26 PM
Now I'm not really sure what you're asking… Do you want to write an API or a program that accepts arguments? Whatever, the latter seems more likely.


Vararg is one of the best things that come with Lua. We have it because when Lua passes variables between functions and such, it doesn't actually pass single variables but variable lists. That's why you can do things like:
local a, b, c = 1, 2, 3
We made a list there (1, 2, 3), and when we assign another list (a, b, c) to it, the first element of the (a, b, c) list gets assigned to the first element of the (1, 2, 3) list, the second to the second, and so on. In the end we have a assigned to 1, b assigned to 2 and c assigned to 3.

Now where do functions come in? Well, when you pass arguments to a function, you do essentially the same thing you did up there with a, b and c:
local function something(a, b, c)
    print(a )
    print(b )
    print(c )
end

something(1, 2, 3)

When you're writing a program, you're actually writing a big function, with the difference that you can't see the enclosing function(arguments) … end part. That's the point - if we had that part up there, we could write programs that accept arguments, like a function that accepts arguments. And here comes the vararg. This is a vararg function:
local function something(...)
    local args = {...}
    print(#args)
    -- I really mean it, the "..." really is proper syntax
end
represents a list here - we put it into a table, and *magic* we have a table that contains the arguments passed to the function!

Fortunately, CC Lua is kind enough (it's actually the loadstring method which's this kind) to automatically make our programs vararg functions, so we can use the in our programs:
local args = {...}
print(args[1] or "Got no argument")
And there, we have a program that accepts arguments.
Edited on 01 December 2013 - 11:27 AM
Stormkrow #4
Posted 01 December 2013 - 02:54 PM
Alright thanks, so if I wanted to add variables to the argument list would I have to include the variable names into both local function something(…) and local args = {…}

Like this?

local function something(var1, var2)
local args = {var1, var2}
print(#args)
end

Sorry I'm new to Lua! Im so very used to languages like Java
jay5476 #5
Posted 01 December 2013 - 03:42 PM
Im pretty sure you got it but ill give an example

--sav file as print
local arg = {...}
for k, v in pairs(arg) do
print(v)
end
-- run it like: print this will print
now that will store your program args in a table an then print them out
Bomb Bloke #6
Posted 01 December 2013 - 04:17 PM
Alright thanks, so if I wanted to add variables to the argument list would I have to include the variable names into both local function something(…) and local args = {…}
While your code is valid (and in many cases will be what you want to use), the intention is that you type the "…" bits verbatim. Lua will look at them and get the whole variable list you passed the function automatically.

This is very, very handy when it comes to making functions that can accept variable amounts of variables.

local function printer(...)
  local args = {...}  -- Take the list of variables passed to the function, dump it in a table called "args".
  for i=1,#args do    -- Start a loop that repeats once for each value in "args".
    print(args[i])
  end
end

printer(242,5,54,634,12,3,545,6)
printer("Asdfasdf","rfecaz","freadc")

You're hopefully familiar with Java's arrays. Lua's tables are similar, but rather more versatile.
Edited on 01 December 2013 - 03:19 PM