14
votes

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.

2

2 Answers

28
votes

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

1
votes

An easy way:

Command and output:

C:\LUAWORK\Estudio-Tut>lua -e "a=2 b=3 c=4 dofile(‘argu.lua’)"

2 3 4

4 6 8

File 1, argu.lua:

print (a , b ,c)
a=2*a
b=2*b
c=2*c
dofile ( ‘otro.lua’)

File 2, otro.lua:

print (a ,b, c)

Using -e "……." I set globals in the call to any chain of modules