2

I am writing a Lua 5.3 program and it requires arguments to be passed to it. I use the arg table to get the first argument: arg[1], but the 1st argument, according to the script, is nil even though I have passed an argument to the file.

Here's the code that I've written:

local strin = arg[1]:sub(2,arg[1]:len()-1)   -- to remove the quote marks
local i = 0
for W in strin:gmatch(".") do
    i = i + 1
    if W == " " or W == "\t" then strin = strin:sub(i+1) else break end
end
print(strin)

I pass the argument to the file like this:

C:\Users\WhiteKid\Documents\Scripts>RemoveWhiteSpace.lua "     hello world!"

It thinks arg[1] is a nil value when it is not. Is there a different way of getting the arguments passed to a lua script in Lua 5.3?

6
  • What happens with the minimal script for k, v in pairs( arg ) do print( k, v ) end? Commented Jun 30, 2017 at 23:44
  • I got two keys assigned to two different value. Commented Jul 1, 2017 at 1:44
  • -1 = path to lua53.exe 0 = path to my script file Commented Jul 1, 2017 at 1:45
  • When passing arguments to it? Because that would mean something else is broken. Commented Jul 1, 2017 at 2:50
  • Yes. I feel like it might be something with my version of lua. I have Lua 5.3 Commented Jul 1, 2017 at 3:55

1 Answer 1

2

Since you are calling the .lua script directly (C:\Users\WhiteKid\Documents\Scripts>RemoveWhiteSpace.lua " hello world!"), you seem to have an association with a lua interpreter. You need to make sure you pass %1 or %* to the interpreter you are calling in that association. Alternatively, try calling the Lua interpreter and passing the script name and the parameters and it should work as you expect.

Also, you should check if arg[1] is present and check if the quotes are also there (as they may be removed before the parameters get to the script, so you should not always expect them).

Sign up to request clarification or add additional context in comments.

Comments

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.