Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass parameters to a Lua file when loading it from another Lua file?

Tags:

lua

I need some help on parsing the Command Line for a lua file. I am executing a lua file and that lua file has a command "dofile(2nd.lua-file)", but, I want to pass some argument to this 2nd lua file through this 1st lua file.

example- a.lua has dofile("b.lua"), and now I have to pass some argument to b.lua through this a.lua and how can I do this.

like image 479
Invictus Avatar asked Mar 16 '12 21:03

Invictus


People also ask

How to pass arguments to Lua script?

To pass arguments into Lua, the script name and arguments must be enclosed within double quotes. The arguments are stored within the arg variable in Lua, which is a table of strings.

What does require() do in Lua?

Lua offers a higher-level function to load and run libraries, called require . Roughly, require does the same job as dofile , but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work.


1 Answers

Try this. In file `a.lua':

assert(loadfile("b.lua"))(10,20,30)

In file b.lua:

local a,b,c=...

or

local arg={...}

The arguments to b.lua are received as varargs, hence the ....

like image 200
lhf Avatar answered Oct 09 '22 04:10

lhf