Advertisement
Wyvern67

dl

Jan 22nd, 2013 (edited)
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usage:" )
  3.     print( "dl <filename> [-e extension] [-s] [-i] [-r]" )
  4. end
  5.  
  6. function getOpt(tArgs)
  7.     local function isOption(text)
  8.         --if text starts with a dash like "-something" consider it an option
  9.         if (text == nil) or (string.sub(text, 0, 1) == "-") then
  10.             return true
  11.         else
  12.             return false
  13.         end
  14.     end
  15.     local opt = {}
  16.     local i = 1
  17.     while i <= #tArgs do
  18.         if isOption(tArgs[i]) then
  19.             if isOption(tArgs[i+1]) then
  20.                 --if no argument (no next arg or it's an option too)
  21.                 --interpret like options.something = true
  22.                 opt[string.sub(tArgs[i], 2)] = true
  23.             else
  24.                 --interpret like options.something = value
  25.                 opt[string.sub(tArgs[i], 2)] = tArgs[i+1]
  26.                 i = i+1 --skip the next arg, it was the argument for -something
  27.             end
  28.         else
  29.             --if not an option
  30.             --aka a random arg, do options[number] = argument
  31.             table.insert(opt, tArgs[i])
  32.         end
  33.  
  34.         i = i+1
  35.     end
  36.     return opt
  37. end
  38.  
  39. local tArgs = { ... }
  40. local options = getOpt(tArgs)
  41.  
  42. local fileName = options[1]
  43.  
  44. local extension = options.e
  45. if not extension then extension = "lua" end
  46.  
  47. local file=http.get("http://wyvern67.free.fr/Minecraft/scripts/"..fileName.."."..extension)
  48. if not file then print("Url invalide") return false end
  49.  
  50. local cFile = file.readAll()
  51. file.close()
  52.  
  53. local fHandle = fs.open(fileName.."."..extension, "w")
  54. fHandle.write(cFile)
  55. fHandle.close()
  56.  
  57. local action
  58. if options.i then
  59.     if fs.exists("startup.lua") then
  60.         fs.delete("startup.lua")
  61.         action = " and startup overwritten"
  62.     else
  63.         action = " and startup written"
  64.     end
  65.  
  66.     fHandle = fs.open("startup.lua", "w")
  67.     if options.e then
  68.         fHandle.writeLine('shell.run("dl", "' .. options[1] .. '", "-e", ' .. extension .. '"-s")')
  69.     else
  70.         fHandle.writeLine('shell.run("dl", "' .. options[1] .. '", "-s")')
  71.     end
  72.     fHandle.writeLine('shell.run("'..fileName.."."..extension..'")')
  73.     fHandle.close()
  74. else
  75.     action = ""
  76. end
  77.  
  78. if options.s == nil then
  79.     print("Downloaded as "..fileName.."."..extension..action)
  80. end
  81.  
  82. if options.r then
  83.     os.reboot()
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement