From ea0eb66096ddc62758d613e9eae84b9d8b3061dc Mon Sep 17 00:00:00 2001 From: BanceDev Date: Sun, 8 Sep 2024 12:41:18 -0400 Subject: added support for cli args for lua scripts --- src/lua_api.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/lua_api.c') diff --git a/src/lua_api.c b/src/lua_api.c index ef5bf9a..8314e2a 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -33,7 +33,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. static bool debug_mode = false; // -- script execution -- -void lua_load_script(lua_State *L, const char *script) { +void lua_load_script(lua_State *L, const char *script, char **args) { char script_path[512]; // check if script is in the current directory if (access(script, F_OK) == 0) { @@ -55,6 +55,16 @@ void lua_load_script(lua_State *L, const char *script) { return; } } + // add args global if args were passed + if (args[0] != NULL) { + lua_newtable(L); + for (int i = 0; args[i]; i++) { + lua_pushstring(L, args[i]); + // i + 1 since Lua is 1 based indexed + lua_rawseti(L, -2, i + 1); + } + lua_setglobal(L, "args"); + } // if we got here the file exists if (luaL_dofile(L, script_path) != LUA_OK) { printf("[C] Error reading script\n"); -- cgit v1.2.3-59-g8ed1b