This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kingdaro's profile picture

[lua] Command Line Arguments

Started by Kingdaro, 13 March 2013 - 09:00 AM
Kingdaro #1
Posted 13 March 2013 - 10:00 AM
A while ago (well, twice really), I wanted to make sort of a minimal file browser/runner type thing, and I wanted to make it so that the program could automatically detect if a program needs arguments, in which it will prompt the user for said arguments.

The only way I know of to access command line arguments is to use the triple dot notation "…", but are there any other ways to do this, so I can support every method of argument catching?

As a side note, I don't think false positives (a function using … to accept variable arguments) would be much of a problem, if one of you decides to mention that.
eleure #2
Posted 13 March 2013 - 10:04 AM
there is also the arg table. unless i'm mistaken, that's all.
LBPHacker #3
Posted 13 March 2013 - 10:04 AM
If that's the question, I'm pretty sure there's no other way to access command line arguments.


EDIT:
there is also the arg table. unless i'm mistaken, that's all.
You mean
arg = {...}
That uses … too.
GopherAtl #4
Posted 13 March 2013 - 10:05 AM
… is the only way to access arguments, so go to it, and have fun.

eleure: you are mistaken. There is no built-in "arg" table.
Kingdaro #5
Posted 13 March 2013 - 10:11 AM
If that's the question, I'm pretty sure there's no other way to access command line arguments.

… is the only way to access arguments, so go to it, and have fun.

That's what I thought, I just wanted to make sure.

Thanks guys.
eleure #6
Posted 13 March 2013 - 10:12 AM
The given options (see below) are executed and then the Lua program in file script is loaded and executed. The given args are available to script as strings in a global table named arg.
from http://www.lua.org/manual/5.1/lua.html. computercraft must re-assign arg to {…}, so apparently my post only applies to normal lua programs

edit: just realized the above code isn't part of cc, but rather a convention. i'm honestly not sure of the purpose of arg = {…}
Lyqyd #7
Posted 13 March 2013 - 10:13 AM
I have this nagging feeling that there is a built-in table, though. Something like arg or args. I will double-check this this evening.
MysticT #8
Posted 13 March 2013 - 10:18 AM
The given options (see below) are executed and then the Lua program in file script is loaded and executed. The given args are available to script as strings in a global table named arg.
from http://www.lua.org/manual/5.1/lua.html. computercraft must re-assign arg to {…}, so apparently my post only applies to normal lua programs
Well, that's what the lua interpreter (wich is used to run lua programs) does. CC doesn't do this, so it's not an option here. It would be easy to implement it anyway.

I have this nagging feeling that there is a built-in table, though. Something like arg or args. I will double-check this this evening.
I think that's only in lua 5.2, and it works with functions too. Or at least I think I read that somewhere :P/>
eleure #9
Posted 13 March 2013 - 10:26 AM
The given options (see below) are executed and then the Lua program in file script is loaded and executed. The given args are available to script as strings in a global table named arg.
from http://www.lua.org/manual/5.1/lua.html. computercraft must re-assign arg to {…}, so apparently my post only applies to normal lua programs
Well, that's what the lua interpreter (wich is used to run lua programs) does. CC doesn't do this, so it's not an option here. It would be easy to implement it anyway.

I have this nagging feeling that there is a built-in table, though. Something like arg or args. I will double-check this this evening.
I think that's only in lua 5.2, and it works with functions too. Or at least I think I read that somewhere :P/>


#!/usr/bin/env lua5.1													  
print(arg[1])
just tested on my desktop. the following prints 'example':
~/tmp/arguments.lua example

lua certainly does have a global table named arg.

edit: lyqyd was referring to exactly the same feature as i am.
LBPHacker #10
Posted 13 March 2013 - 10:42 AM
-snip-

OK, you're the best, if that's what you want to hear.

Now go ahead and realize that we're talking about CC's Lua.

BTW Tested "arg", "args" and everything else - nil…
eleure #11
Posted 13 March 2013 - 10:45 AM
i realize you're talking about CC's lua. i have no idea why it's not present, but my point is that i wasn't mistaken about the feature itself.
i don't mind being told that i'm wrong when i am, but i do mind being told that i'm wrong when i'm not. i don't see a problem with that.
i would have tested on cc's lua, but i wasn't able at the moment.
ChunLing #12
Posted 13 March 2013 - 10:46 AM
The way that shell loads the program prevents it from having the table available, I'm not sure why. But this does work for ordinary functions with variable parameters.
MysticT #13
Posted 13 March 2013 - 12:05 PM
just tested on my desktop. the following prints 'example':
~/tmp/arguments.lua example

lua certainly does have a global table named arg.

edit: lyqyd was referring to exactly the same feature as i am.
I was actually telling you that you were right, but that it doesn't work in CC.

But this does work for ordinary functions with variable parameters.
Indeed it does. I thought that was only available on lua 5.2. Just tested and it works. It's weird that it doesn't work for command line arguments.
eleure #14
Posted 13 March 2013 - 12:42 PM
mea culpa. i thought you were misunderstanding the way interpreters work.
remiX #15
Posted 13 March 2013 - 06:11 PM
I have this nagging feeling that there is a built-in table, though. Something like arg or args. I will double-check this this evening.

I know you can use arg within a function without defining a table for it
local function foo( ... )
    for i = 1, #arg do
        print( arg[i] )
    end
end

foo( read(), read(), read() )