12 posts
Posted 21 April 2016 - 10:38 PM
I need help detecting arguments for a program I'm writing for a server.
I have looked at other programs(and the wiki)to see how but that doesn't work.
2427 posts
Location
UK
Posted 22 April 2016 - 09:56 AM
post your code so we know how far you have got, use
pastebin.com if it's easier
Also, this is not the best example but it works:
full code:
http://pastebin.com/LZ7Ks57Abelow is just the arg processing
Spoiler
local usage = {
"multiple different args are accepted",
"if multiple of the same arg are given then the last one will be used - this is redundent don't do it",
"we will tell you the arg number which caused any errors",
" -h | print this usage information",
" -f <filePath> | where filePath points to a file which contains the quadProgram",
" -n <interger/number> | numOfQuads",
" -d <float/number> | delayMult",
" -r <side/'ignore'> | redstoneSide or ignoreRedstone = true",
" -b <side> | baseSide",
" -t <bool> | anyKeyTerminate",
}
--# there is more code here, but not related to arg processing
local arg = {...}
--# set vaiables to default, we will overide these with the arg later
local quadProgram = defaults.quadProgram
local numOfQuads = defaults.numOfQuads
local delayMult = defaults.delayMult
local ignoreRedstone = defaults.ignoreRedstone
local anyKeyTerminate = defaults.anyKeyTerminate
local bSide = defaults.bSide
local rSide = defaults.rSide
if #arg > 0 then
--# lower case everything
for i = 1, #arg do
if not tonumber(arg[i]) then
arg[i] = string.lower(arg[i])
end
end
--# process arg
local i = 1
while i <= #arg do
local doubleInc = false
if arg[i] == "-h" then
printUsage()
elseif arg[i] == "-f" then --# quadProgram
do --# make my editor look clearer
if not (fs.exists(tostring(arg[i+1])) and (not fs.isDir(tostring(arg[i+1])))) then
myError("arg "..i.." Can't load file, either doesn't exist or is a directory", 2)
else
local f = fs.open(arg[i+1], "r")
local p = f.readAll()
f.close()
quadProgram = textutils.unserialize(p)
if quadProgram == nil then error("unable to understand file: "..tostring(arg[i+1]))
end
doubleInc = true
end --# make my editor look clearer
elseif arg[i] == "-n" then --# numOfQuads
do --# make my editor look clearer
local n = tonumber(arg[i+1])
if n == nil or n < 1 then
myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
else
local a,b = math.modf(n)
if b ~= 0 then
myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
end
end
if n > 64 then
print("arg "..i.." Warning: numOfQuads is larger than one stack(64). Did the max per base change or are you piping them in?")
end
numOfQuads = n
doubleInc = true
end --# make my editor look clearer
elseif arg[i] == "-d" then --# delayMult
do --# make my editor look clearer
local d = tonumber(arg[i+1])
if d == nil then
myError("arg "..i.." -d arg must be followed by a number", 2)
end
delayMult = d
doubleInc = true
end --# make my editor look clearer
elseif arg[i] == "-r" then --# redstoneSide
do --# make my editor look clearer
local r = tostring(arg[i+1])
if r == nil then
myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
end
if r == "ignore" then
ignoreRedstone = true
elseif r == "left" or r == "right" or r == "front" or r == "back" then
rSide = r
ignoreRedstone = false
else
myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
end
doubleInc = true
end --# make my editor look clearer
elseif arg[i] == "-b" then --# baseSide
do --# make my editor look clearer
local b = tostring(arg[i+1])
if b == nil then
myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
end
if b == "left" or b == "right" or b == "front" or b == "back" then
bSide = b
else
myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
end
doubleInc = true
end --# make my editor look clearer
elseif arg[i] == "-t" then --# anyKeyTerminate
do --# make my editor look clearer
local t = tostring(arg[i+1])
if t == nil then
myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
end
if t == "true" then
anyKeyTerminate = true
elseif t == "false" then
anyKeyTerminate = false
else
myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
end
doubleInc = true
end --# make my editor look clearer
else
myError("arg "..i.." unprocessed arg",2)
end
if doubleInc then
i = i +2
else
i = i +1
end
end
end
Edited on 22 April 2016 - 08:14 AM
1080 posts
Location
In the Matrix
Posted 22 April 2016 - 12:47 PM
A smaller easier to read example.
local tArgs = {...}
--#tArgs[1] is the first thing they type after the program name as a string.
--#myProgram hello
--#tArgs[1] is hello
--#It's a string tho, so myProgram 1 wouldn't make that 1 a number.
--#myProgram 1 hello
--#tArgs[1] = "1", tArgs[2] = "hello"
if (tArgs[1] == "hello" ) then
print("tArgs[1] was hello")
elseif (tonumber(tArgs[1]) ~= nil and tonumber(tArgs[1]) == 1) then
print("tArgs[1] was the number 1")
end
Edited on 22 April 2016 - 10:48 AM