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

Running a program with arguments from a string

Started by TheRockettek, 03 April 2016 - 01:05 PM
TheRockettek #1
Posted 03 April 2016 - 03:05 PM
So basicaly im making a program via a chatbox to be able to run programs from chat so like if i say "!Give datOneGuy 40" it would run a program called !Give and 'datOneGuy 40' would be its arguements, I trued just doing shell.run(command) but it just says 'could not find file !Give datOneGuy 40' I looked into it and tried using a string splitter but i couldnt do it. Can anyone help :P/>
Anavrins #2
Posted 03 April 2016 - 07:09 PM
Here's what I did for mine :P/>

local function split(s, p)
  local t = {}
  s:gsub("([^"..p.."]+)", function(v) t[#t + 1] = v end)
  return t
end
while true do
  local event, side, name, msg = os.pullEvent()
  if event == "chat_message" then
    if msg:sub(1,1) == "!" then #-- If chat is a command
      local args = split(msg:sub(2), " ")
      if args[1] == "Give" then
        print("Give command with args ", select(2, unpack(args)))
        shell.run(unpack(args)) #-- Run "/Give" program with datOneGuy and 40 as arguments
      end
    end
  end
end
Edited on 03 April 2016 - 05:12 PM