lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I want to pass command line arguments to a piece of code, that I
> specify with -e:
> 
>     $ lua -e 'print(...)' 1 2 3

The patch below is a hack that seems to (almost) work.
To avoid the message "cannot open 1", you need to do
	lua -e'print("test",...);os.exit()' 1 2 3
or add a '-E' option as suggested earlier.
In any case, it is a hack.
--lhf

--- /tmp/lhf/lua-5.1.4/src/lua.c	2007-12-28 13:32:23.000000000 -0200
+++ ./lua.c	2010-12-12 15:56:03.000000000 -0200
@@ -138,7 +138,12 @@
 
 
 static int dostring (lua_State *L, const char *s, const char *name) {
-  int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
+  int n= (name[0]=='=' && name[1]=='(') ? lua_gettop(L)-1 : 0 ;
+  int status = luaL_loadbuffer(L, s, strlen(s), name);
+  if (status == 0) {
+    if (n>0) lua_insert(L, -n-1);
+    status = docall(L, n, 1);
+  }
   return report(L, status);
 }
 
@@ -301,6 +306,8 @@
         const char *chunk = argv[i] + 2;
         if (*chunk == '\0') chunk = argv[++i];
         lua_assert(chunk != NULL);
+  getargs(L, argv, n-1);  /* collect arguments */
+  lua_setglobal(L, "arg");
         if (dostring(L, chunk, "=(command line)") != 0)
           return 1;
         break;