Page 1 of 1
Command line parameters
Posted: Thu May 26, 2011 9:57 pm
by markhobley
How do I obtain the command line parameters from within main.lua? I tried the following, but this produces no data on the console:
-- This is main.lua
print( "Program name:", arg[0] )
print "Arguments:"
for l = 1, #arg do
print( l," ", arg[l] )
end
Re: Command line parameters
Posted: Thu May 26, 2011 9:59 pm
by bartbes
The easiest way is probably the table passed to love.load.
Re: Command line parameters
Posted: Thu May 26, 2011 11:53 pm
by Jasoco
I believe in default Löve, love.run calls love.load with the arg parameter. So do your check in love.load and make sure it says function love.load(arg).
For me the three arguments I get are the love project, "embeded boot.lua" and the love binary. You can use it to check the table for any argument you want.
Re: Command line parameters
Posted: Fri May 27, 2011 12:59 am
by markhobley
I revised the script as follows:
-- This is main.lua
function love.load ()
print("Program name", arg[0])
print("Arguments:")
for l = 1, #arg do
print(l," ",arg[l])
end
end
I got some funny results on a Microsoft Windows based system, but I will try this on Unix, when I get home at the weekend.
On Microsoft Windows, the parameters were shifted.
Program name: nil
Arguments:
1 --console
2 C:\Love\test
3 foo
4 bar
Also on Microsoft Windows, I could not drag the mouse over the console window to select the text for copy and paste.
Mark.
Re: Command line parameters
Posted: Fri May 27, 2011 2:59 am
by BlackBulletIV
Your script has a couple problems. love.load doesn't take the arg argument. It should be:
Secondly, Lua indicies start at 1, not 0. So adjust your indicies to account for this.
Finally, please use [ code ] (without spaces of course) for your code samples.
Re: Command line parameters
Posted: Fri May 27, 2011 9:44 am
by TsT
the arg indice are also :
-1 is the command like called (relative or absolute name)
0 the first argument, usualy the .lua file (or the .love file for love)
Code: Select all
The command :
echo "for k,v in pairs(arg) do print(k,v) end" | /usr/bin/lua - a b "c d"
Result :
1 a
2 b
3 c d
-1 /usr/bin/lua
0 -
Re: Command line parameters
Posted: Sat May 28, 2011 8:01 am
by BlackBulletIV
0 is the first argument? That seems a bit inconsistent.
Re: Command line parameters
Posted: Sat May 28, 2011 8:26 am
by Robin
BlackBulletIV wrote:0 is the first argument? That seems a bit inconsistent.
It's because you usually don't want to count the file as an argument, since it is supposed to be the actual program anyway. (And it really starts at -1, doesn't it?)
Re: Command line parameters
Posted: Sat May 28, 2011 9:36 am
by BlackBulletIV
Oooh, I see; that makes sense. That way, when you use pairs/ipairs it'll only give you the arguments after the binary and source file.
Re: Command line parameters
Posted: Sat May 28, 2011 2:59 pm
by Robin
Exactly.