Jump to content




[Question]How does the shell handle arguments?


4 replies to this topic

#1 Bossman201

  • New Members
  • 92 posts

Posted 16 June 2012 - 01:54 AM

I'm sure I understand this bit of code, will comment on what I think each line does/mean.
local tArgs = { ... } --Array containing all arguments
if #tArgs > 0 then --'#' returns index of an array, line checks tArgs to make sure there is at least one value.
print( "" ) --Prints program usage or coder-defined action.
print( "" )
return  --good practice to return all functions, I do not do it but I may have to start.
end

My question is basically how can I emulate this inside a program? I want to decode a message from rednet into "arguments" and then pass these to a function.

Bonus question (for bonus points): What does the ellipse in lua mean? (ex. '...')

#2 OmegaVest

  • Members
  • 436 posts

Posted 16 June 2012 - 02:21 AM

The elipse means the value of the data passed to the program. Like writing "param" in a function's variable requirements.

And potentially the easiest way is to simply create a table, and a reader. Parse the message to a function that breaks down the string into words, separating them by commas and/or spaces. Such as:

id, msg = rednet.receive() -- Think that's the right order. Well, swap them if I'm wrong.

comm = stringbreaker(msg)

function stringbreaker(mist)
   outgoing = {}
   outgoing[1] = ""
   outRep = 1
   for x = 1, #mist do
      thisChar = string.sub(mist, x,x)
      if (thisChar ~= " ") or (thisChar ~= ",") then
         outgoing[outRep] = outgoing[outRep] .. mist
      else
         outRep = outRep+1
         outgoing[outRep] = ""
      end
   end
   return outgoing
end

Then just put the table in the shell.run().

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 16 June 2012 - 06:23 PM

Taken from the shell program:

local tWords = {} -- create a table to store the words
for match in string.gmatch(sLine, "[^ t]+") do -- for each word in the string
  table.insert( tWords, match ) -- add the word to the table
end

local sCommand = tWords[1] -- get the command (should be the first word)
if sCommand then
  shell.run( sCommand, unpack( tWords, 2 ) ) -- run the program
end

You probably don't know some functions there (like string.gmatch and unpack), they are lua functions, so you can look at the manual and lua tutorials to know how to use them.

And the ellipse (...) means variable arguments. So you can make a function that receives a variable amount of arguments, and use ... to access them:
function abc(...)
  for i, v in pairs({ ... }) do
    print(i, ": ", v)
  end
end
That would print all the arguments passed to the function.
It's used in programs because the whole program is actually a function, that's called by the shell, with any arguments you give.

#4 Bossman201

  • New Members
  • 92 posts

Posted 16 June 2012 - 07:24 PM

Mystic I'm glad you used 'in pairs' in your example, I've seen it in the default programs and I'm wondering what it does and how to use it?

Thanks guys, I think I understand it now.

EDIT: (I haven't looked at the manual or lua library yet)For unpack, I'm thinking it returns all of the values in a table. Which, by your code, would be given to a program as arguments. Not sure what the second argument is though. Is that right?

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 16 June 2012 - 08:15 PM

View PostBossman201, on 16 June 2012 - 07:24 PM, said:

Mystic I'm glad you used 'in pairs' in your example, I've seen it in the default programs and I'm wondering what it does and how to use it?
That's also in the lua manual:

Quote

Returns three values: the next function, the table t, and nil, so that the construction
for k,v in pairs(t) do body end

will iterate over all key–value pairs of table t.
See function next for the caveats of modifying the table during its traversal.

View PostBossman201, on 16 June 2012 - 07:24 PM, said:

EDIT: (I haven't looked at the manual or lua library yet)For unpack, I'm thinking it returns all of the values in a table. Which, by your code, would be given to a program as arguments. Not sure what the second argument is though. Is that right?
Yes, unpack returns the values inside the table. It has three arguments: the table to get the values, the start index and the end index. So it returns the values between the given indexes. The default indexes are from 1 to the lenght of the table (the whole table).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users