Advertisement
kreezxil

Do - Command Line Stacking 2.0

Aug 31st, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. --[[
  2. Created by Kreezxil
  3. On 8/28/2013
  4. Changelog:
  5. 5/31/2014 - cleaned up code, optimized explode and implode, fixed parameter dropping
  6.  
  7. pastebin: wSWv68HL
  8.  
  9. Description: Allows turtles and computers to have
  10. command line scripting.
  11.  
  12. Usage:
  13.  do <<command>[args];[<command>[args];...]>
  14.  
  15. --]]
  16.  
  17. local debug = false
  18.  
  19. --[[
  20.   explode function by Kreezxil
  21.   cause: learned by example. yay. \o/
  22. --]]
  23. function explode(div,str)
  24.   if(div=='') then
  25.       return false
  26.   end
  27.  
  28.   if string.sub(str,-1,-1) ~= ";" then
  29.       str = str .. ";"
  30.   end
  31.  
  32.   local tmp = ""
  33.   local arr = {}
  34.  
  35.   for i=1,#str do
  36.       local char = string.sub(str,i,i)
  37.       if char == div then
  38.           if tmp ~= "" then
  39.             print("Adding [" .. tmp .."] to result array")
  40.             table.insert(arr,tmp)
  41.           end
  42.           tmp = ""
  43.       else
  44.           if char ~= nil then
  45.             tmp = tmp .. char
  46.           end
  47.       end
  48.   end
  49.  
  50.   return arr
  51. end
  52.  
  53. --[[
  54.   implode function by Kreezxil
  55.   cause: learned by example. yay. \o/
  56. --]]
  57. function implode(div,arr)
  58.   if(div=='') then
  59.       return false
  60.   end
  61.  
  62.   result = ""
  63.   for i=1,#arr do
  64.       result = result .. div .. arr[i]
  65.   end
  66.   return result
  67. end
  68.  
  69. local args = { ... }
  70. if #args <1 then
  71.   print ("Usage:")
  72.   print("")
  73.   print("do <<commands>[args];[<commands>[args];...]>")
  74.   return
  75. end
  76.  
  77. if debug then
  78.     for i=1,#args do
  79.       print("Arg[" .. i .. "]: " .. args[i])
  80.     end
  81. end
  82. -- turn args into a string called compacted
  83. local compacted = implode(" ",args)
  84.  
  85. if debug then
  86.     print("Args after implosion:")
  87.     print(compacted)
  88. end
  89.  
  90. --[[
  91.   turn compacted string of args into our
  92.   array for use with shell.run
  93. --]]
  94. stack = explode(";" , compacted)
  95.  
  96. if debug then
  97.     print("Command Stack is: ")
  98.     for i=1,#stack do
  99.       print("CMD[" .. i .. "] " .. stack[i])
  100.     end
  101. end
  102.  
  103. print ("Processing command stack.")
  104.  
  105. for i=1,#stack do
  106.   shell.run(stack[i])
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement