Get containing path of lua file

后端 未结 10 1925
粉色の甜心
粉色の甜心 2021-02-12 12:00

I am wondering if there is a way of getting the path to the currently executing lua script file?

This is specifically not the current working directory, which could be

10条回答
  •  孤独总比滥情好
    2021-02-12 12:17

    As lhf says:

    ~ e$ echo "print(arg[0])" > test.lua
    ~ e$ lua test.lua
    test.lua
    ~ e$ cd /
    / e$ lua ~/test.lua
    /Users/e/test.lua
    / e$ 
    

    Here's the same info using the debug.getinfo mechanism

    ~ e$ echo "function foo () print(debug.getinfo(1).source) end; foo()" > test.lua
    ~ e$ lua test.lua
    @test.lua
    ~ e$ cd /
    / e$ lua ~/test.lua
    @/Users/e/test.lua
    / e$ 
    

    This is available from the C API lua_getinfo

提交回复
热议问题