Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Created by Kreezxil
- On 8/28/2013
- Changelog:
- 5/31/2014 - cleaned up code, optimized explode and implode, fixed parameter dropping
- pastebin: wSWv68HL
- Description: Allows turtles and computers to have
- command line scripting.
- Usage:
- do <<command>[args];[<command>[args];...]>
- --]]
- local debug = false
- --[[
- explode function by Kreezxil
- cause: learned by example. yay. \o/
- --]]
- function explode(div,str)
- if(div=='') then
- return false
- end
- if string.sub(str,-1,-1) ~= ";" then
- str = str .. ";"
- end
- local tmp = ""
- local arr = {}
- for i=1,#str do
- local char = string.sub(str,i,i)
- if char == div then
- if tmp ~= "" then
- print("Adding [" .. tmp .."] to result array")
- table.insert(arr,tmp)
- end
- tmp = ""
- else
- if char ~= nil then
- tmp = tmp .. char
- end
- end
- end
- return arr
- end
- --[[
- implode function by Kreezxil
- cause: learned by example. yay. \o/
- --]]
- function implode(div,arr)
- if(div=='') then
- return false
- end
- result = ""
- for i=1,#arr do
- result = result .. div .. arr[i]
- end
- return result
- end
- local args = { ... }
- if #args <1 then
- print ("Usage:")
- print("")
- print("do <<commands>[args];[<commands>[args];...]>")
- return
- end
- if debug then
- for i=1,#args do
- print("Arg[" .. i .. "]: " .. args[i])
- end
- end
- -- turn args into a string called compacted
- local compacted = implode(" ",args)
- if debug then
- print("Args after implosion:")
- print(compacted)
- end
- --[[
- turn compacted string of args into our
- array for use with shell.run
- --]]
- stack = explode(";" , compacted)
- if debug then
- print("Command Stack is: ")
- for i=1,#stack do
- print("CMD[" .. i .. "] " .. stack[i])
- end
- end
- print ("Processing command stack.")
- for i=1,#stack do
- shell.run(stack[i])
- end
Add Comment
Please, Sign In to add comment