3 posts
Posted 08 December 2013 - 12:57 AM
Just what the title says. I have used tArgs, but am having trouble setting the variables from a file IF it exists or prompting for input IF it doesn't. I am somewhat new to lua and this is probably not that hard, but after googling "write/load tArgs to/from file" and looking through programs on the forums here I haven't found a solution :(/>
I won't bother repeating the standard tArgs code, and my attempts to modify it have utterly failed. Any help I can get would be greatly appreciated.
Thanks
Jnky
758 posts
Location
Budapest, Hungary
Posted 08 December 2013 - 02:32 AM
Just
serialize the arguments and save them. With this setup, the file is read only if there are no arguments:
local configPath = shell.getRunningProgram() .. ".cfg"
if fs.isReadOnly(configPath) then configPath = fs.getName(shell.getRunningProgram()) .. ".cfg" end
local shellArgs = {...}
local function saveConfig(tbl)
local handle = fs.open(configPath, "w")
if not handle then error("failed to save config") end
handle.write(textutils.serialize(tbl))
handle.close()
end
local function loadConfig()
local handle = fs.open(configPath, "r")
if not handle then error("failed to load config") end
local serialized = handle.readAll()
handle.close()
return textutils.unserialize(serialized)
end
if not fs.exists(configPath) or #shellArgs > 0 then
saveConfig(shellArgs)
end
local tArgs = loadConfig()
-- do whatever you want with tArgs
3 posts
Posted 08 December 2013 - 10:16 AM
Thank you for the response LBPHacker, I do believe that may be just what I am looking for. Trying to wrap my head around the code you posted and use it like I want it to be used.
Thanks again