1

I've used Lua with Corona SDK for a long time, but I only just downloaded the standalone Lua interpreter (invoked from the command line). When I use

lua main.lua

From the Mac terminal, for some reason, any functions that use (...) no longer have access to arg as their ... arguments; rather, arg now points to the command line arguments.

My question: is there a way to invoke Lua from the command line and still have functions like

local function myFunction(...)
  print(arg[1])
end

With them pointing to the their own ... arguments, not the command line ones?

2
  • possible duplicate of Getting arg to work in a varag function in Lua 5.2 (integrated in Delphi) Commented Nov 7, 2013 at 20:20
  • I'm reasonably sure that Corona is still Lua 5.1 based. I don't know if they feel any pressure to change, especially given that LuaJIT is still primarily 5.1 with just a few tweaks from 5.2 optionally available. Commented Nov 7, 2013 at 21:22

1 Answer 1

1

What about saving those command line arguments on some variable or table just at the entry point? Example:

local function myFunction(...)
    print(cmd_arg)
end
-- Entry point:
local cmd_arg = arg[1]
myFunction()

or collecting all of command line arguments into table:

local function myFunction(...)
    print(cmd_arg[1])
end

-- Entry point:
local cmd_args = {}
for _, cmd_arg in arg do
    table.insert(cmd_args, cmd_arg)
end
myFunction()

EDIT: the solution is already mentioned here: https://stackoverflow.com/a/9787126/1150918

arg appears to be deprecated since 5.1.

And Michal Kottman's solution was:

function debug(name, ...)
    local arg = table.pack(...)
    print(name)
    for i=1,arg.n do
        print(i, arg[i])
    end
end
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for not being clearer - the above is what I don't want. I want each function to have arg point to its personal ... arguments, not the command line ones
@CalebP, oh, no, sorry. It's just deprecated: stackoverflow.com/questions/9786051/…
I must point out that calling a function debug is generally a bad idea in Lua, since it's the name of one of the standard libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.