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

How can I set the "..." used as arguments ?

Started by Cross_Sans, 03 January 2018 - 10:41 AM
Cross_Sans #1
Posted 03 January 2018 - 11:41 AM
EDIT:I just saw that I post this at the wrong section, if you are a moderator, please move it to Ask a pro. Thanks.

Hi, I am currently making a shell for ComputerCraft, and I am currently facing a problem: How do I set the to what the shell receives ?

For example:

... = arguments; -- This is not possible, but then how CraftOS did set this ?
local env = { ... };
setmetatable(env, { __index = getfenv() });
local initFn, err = assert(loadfile(path, env));
-- check and run

Thank you and have a nice day, Alex.
Edited on 03 January 2018 - 10:45 AM
Luca_S #2
Posted 03 January 2018 - 11:54 AM
The default CraftOS shell uses os.run:
CC Github
os.run takes 2 arguments and then a vararg(The …)
The first argument is the enviroment, which you could use getfenv() for.
The second argument is the program to call.
The varargs is the arguments passed to the program.
E.G.:

os.run(getfenv(), "someprogram", "hello", "world")
If you parsed your arguments to a table you can use unpack():

os.run(getfenv(), "someprogram", unpack(args))
SquidDev #3
Posted 03 January 2018 - 12:00 PM
Whilst Luca_S's post is correct as far as shell.run works, I'd recommend reading this part of the PIL book, as it explains … in more detail. In the case of files (and anything loaded with load, loadstring or loadfile), is set to any arguments you pass in when calling the loaded function:

local initFn, err = assert(loadfile(path, env))
initFn(1, 2, 3) --# ... is set to 1, 2, 3
Edited on 03 January 2018 - 11:00 AM
Cross_Sans #4
Posted 03 January 2018 - 12:56 PM
Thank you for both users that helped me, and especially for the helpful links. :)/>
Have a nice afternoon (well, that depends on where you both live);
Anyways, bye :)/>
Alex.