15

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 Answers 2

30

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the following error when I run it with dofile(b.lua)(10), I get "unexpected symbol near '.'" and my b.lua as the following local a=.
@Invictus, it's three dots ... as posted. That's the Lua syntax for varargs. Try it.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.