Jump to content




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



  • You cannot reply to this topic
3 replies to this topic

#1 Cross_Sans

  • Members
  • 122 posts
  • LocationFrance

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 by Sixestla (Redall), 03 January 2018 - 11:45 AM.


#2 Luca_S

  • Members
  • 407 posts
  • LocationGermany

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))


#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

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 by SquidDev, 03 January 2018 - 12:00 PM.


#4 Cross_Sans

  • Members
  • 122 posts
  • LocationFrance

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users