Passing on C argv to the script environment
Are there plans to pass on the C argv into the script environment to allow scripts to do command line parsing? This would be quite useful for e.g. Duktape's DukLuv based JSON proxy.
I tried to figure out if there's any binding currently and it seems src/main.c gives argc/argv to uv_setup_args() but I guess it's not passed on otherwise?
I'll be happy to contribute a pull if necessary.
You're welcome to submit a pull request. I've been super busy with other projects at the moment.
Ok, I'll do that. The only non-trivial thing is to figure out where that array should go :)
It seems uv has a bunch of misc bindings as well, so maybe uv.argv or uv.get_argv()?
Initially I don't think uv is the right place, but then again it needs access to it anyway for proper setup so maybe it's not crazy after all.
To mimic Node.js it could beprocess.argv but since DukLuv is not trying to emulate Node.js that's not really a big point.
Placing it in uv would at least have the merit that it wouldn't pollute the global object for example, in case a better place is later found.
The way I do this in luvit is to pass the arg list to the script as part of the main bootstrap.
https://github.com/luvit/luvi/blob/master/src/main.c#L130-L135
https://github.com/luvit/luvi/blob/master/src/lua/init.lua#L91
I thought of misc.c as the place to implement because of this: https://github.com/creationix/dukluv/blob/master/src/duv.c#L104-L123; it already contains things like cpu_info() and other odds and ends.
Right, that would work. uv.argv() that returns an array of strings would work well. I don't have any idea how we would pull the argv our of libuv though. I guess we could dig through the private data.
It would be nice if libuv accepted this as an official API. I wonder what they think of adding it.
Yeah, one simple approach would be to construct the argv array in main() and just stick it in the global stash and let uv.argv() then read it from there. Or construct it in main() and pass on to duv_main() (which currently gets argv[1] i.e. script filename).
Looking through libuv, I don't think they store the full argv, but we could store a copy in our bindings to uv_setup_args somewhere.
I like the global stash idea. Do whatever you think is best. My only preference is to diverge from libuv as little as possible.
It'd be nice to read it back from libuv but that doesn't seem possible. I'll open a pull with a proposal, thanks!