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

[Lua][Question] shell.run file and return something

Started by WhoAmI, 22 May 2013 - 02:35 AM
WhoAmI #1
Posted 22 May 2013 - 04:35 AM
Hello.
This is the idea I have, but since i created this topic you can tell it isn't working.

startup

shell.run("test","hello world")

test

args={...}
if args[1]=="hello world" then
  return "hello user"
end

So is it possible to run a file and make the file return something except true?
Shnupbups #2
Posted 22 May 2013 - 04:46 AM
Not like that no :(/> sorry!
WhoAmI #3
Posted 22 May 2013 - 05:08 AM
I would like to do it because my files are getting too big and it's hard to keep an overview so I would like to seperate the big functions to their own files.
Is there another way to do that?
BigSHinyToys #4
Posted 22 May 2013 - 05:43 AM
shell.run or os.run won't do this but this will

[EDIT] added better handling of errors in program


local function run(path,...)
    if fs.exists(path) and not fs.isDir(path) then
	    file = fs.open(path,"r")
	    if file then
		    local sFunk = file.readAll()
		    file.close()
		    local prog,err = loadstring(sFunk)
		    if prog then
			    return prog(...)
		    else
			    error(err)
		    end
	    else
		    error("Unable to open file")
	    end
    else
	    error("File not found")
    end
end
print(run("prog"))
WhoAmI #5
Posted 22 May 2013 - 06:04 AM
Omg, you really are a Lua god!
Thank you so much the script :)/>
Kingdaro #6
Posted 22 May 2013 - 06:09 AM
The dofile function returns results from scripts.


-- test
return 'bacon'

-- startup
local str = dofile('test')
print(str) --> bacon
BigSHinyToys #7
Posted 22 May 2013 - 06:15 AM
The dofile function returns results from scripts.

– snip
and this is why I wanted "lua amature" not "lua god"
WhoAmI #8
Posted 22 May 2013 - 06:51 AM
Wow.. I think I'll take dofile then.
Thank you for helping me, all of you :)/>

Edit: how do I parse arguments with dofile?
Kingdaro #9
Posted 22 May 2013 - 08:11 AM
…Huh. It seems as though you can't pass arguments using dofile, haha.
Lyqyd #10
Posted 22 May 2013 - 01:11 PM
This seems like you should be using an API instead, to be honest.