lua_parseargs

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_parseargs

Nicolas Devillard
Hello to all:

The following snippet is useful to carry over the command-line arguments
you have received into a Lua space. Maybe a useful addition to 4.1?

void lua_parseargs(lua_State * L, int argc, char * argv[])
{
    int     i ;

    /* Create a new table and push it onto the stack */
    lua_newtable(L);
    /* insert argc keys into table */
    for (i=0 ; i<argc ; i++) {
        lua_pushstring(L, argv[i]);
        lua_rawseti(L, 1, i);
    }
    /* Set table as global variable 'args' */
    lua_setglobal(L, "args");
    return ;
}

Cheers
-- 
Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Luiz Henrique de Figueiredo
>The following snippet is useful to carry over the command-line arguments
>you have received into a Lua space. Maybe a useful addition to 4.1?

lua.c in 4.0 already exports "getargs", which returns a table with all
arguments.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Nicolas Devillard
> lua.c in 4.0 already exports "getargs", which returns a table with all
> arguments.

What about placing that in the library, rather than in lua.c ?
-- 
Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Luiz Henrique de Figueiredo
In reply to this post by Nicolas Devillard
>What about placing that in the library, rather than in lua.c ?

"getargs" needs access to argv, which only lua.c has.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Reuben Thomas-3
> "getargs" needs access to argv, which only lua.c has.

??? Surely getargs is passed argv, so it can quite happily be in a library!
getopts works this way. Am I missing something?

-- 
http://sc3d.org/rrt/ | certain, a.  insufficiently analysed


Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Luiz Henrique de Figueiredo
In reply to this post by Nicolas Devillard
>> "getargs" needs access to argv, which only lua.c has.
>
>??? Surely getargs is passed argv, so it can quite happily be in a library!
>getopts works this way. Am I missing something?

As you know, a library function in Lua is a function of the form

	static int f (lua_State *L) { ... }

which is then registered in Lua.
This means that you cannot send argv to f directly.
So, argv must either be available as a global variable or as an upvale,
as lua.c does for l_getargs with register_getargs. In any case, whoever
opens the library needs to give access to argv, either by copying it to
a global variable or by pushing it as an upvalue. It seems to me that
only main can do that.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua_parseargs

Reuben Thomas-3
> >> "getargs" needs access to argv, which only lua.c has.
> >
> >??? Surely getargs is passed argv, so it can quite happily be in a library!
> >getopts works this way. Am I missing something?
>
> As you know, a library function in Lua is a function of the form
>
> 	static int f (lua_State *L) { ... }
>
> which is then registered in Lua.
> This means that you cannot send argv to f directly.
> So, argv must either be available as a global variable or as an upvale,
> as lua.c does for l_getargs with register_getargs. In any case, whoever
> opens the library needs to give access to argv, either by copying it to
> a global variable or by pushing it as an upvalue. It seems to me that
> only main can do that.

True, main has to cooperate.

-- 
http://sc3d.org/rrt/ | competent, a.  underpromoted