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

前端 未结 2 1331
故里飘歌
故里飘歌 2020-12-15 08:05

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 arg

相关标签:
2条回答
  • 2020-12-15 08:42

    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

    0 讨论(0)
  • 2020-12-15 08:53

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

    0 讨论(0)
提交回复
热议问题