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
Command line parameters
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Command line parameters
The easiest way is probably the table passed to love.load.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Command line parameters
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.
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.
-
- Prole
- Posts: 3
- Joined: Wed Mar 02, 2011 6:02 pm
Re: Command line parameters
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.
-- 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.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Command line parameters
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.
Code: Select all
function love.load(arg)
Finally, please use [ code ] (without spaces of course) for your code samples.
Re: Command line parameters
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)
-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 -
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Command line parameters
0 is the first argument? That seems a bit inconsistent.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Command line parameters
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?)BlackBulletIV wrote:0 is the first argument? That seems a bit inconsistent.
Help us help you: attach a .love.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Command line parameters
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 13 guests