Command line parameters

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
markhobley
Prole
Posts: 3
Joined: Wed Mar 02, 2011 6:02 pm

Command line parameters

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Command line parameters

Post by bartbes »

The easiest way is probably the table passed to love.load.
User avatar
Jasoco
Inner party member
Posts: 3655
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Command line parameters

Post 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.
markhobley
Prole
Posts: 3
Joined: Wed Mar 02, 2011 6:02 pm

Re: Command line parameters

Post 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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Command line parameters

Post by BlackBulletIV »

Your script has a couple problems. love.load doesn't take the arg argument. It should be:

Code: Select all

function love.load(arg)
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.
User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: Command line parameters

Post 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	-
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Command line parameters

Post by BlackBulletIV »

0 is the first argument? That seems a bit inconsistent. :huh:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Command line parameters

Post by Robin »

BlackBulletIV wrote:0 is the first argument? That seems a bit inconsistent. :huh:
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?)
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Command line parameters

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Command line parameters

Post by Robin »

Exactly.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 17 guests