http://www.computerc...post__p__239950
old post
So I'm working on an update to my quadPipe program and am having a problem interpreting the command line arguments, I have a for loop and am looping through everything in (admittedly) a very bad way. I'm considering replacing the for loop with a while loop, but want to pick your brains first.
If a rewrite is needed then consider this a "how not to write a for loop".
Full Code
So I'm working on an update to my quadPipe program and am having a problem interpreting the command line arguments, I have a for loop and am looping through everything in (admittedly) a very bad way. I'm considering replacing the for loop with a while loop, but want to pick your brains first.
If a rewrite is needed then consider this a "how not to write a for loop".
Full Code
Relevant Code
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
--lower case everything
for i = 1, #arg do
if tostring(arg[i]) then
arg[i] = string.lower(arg[i])
end
end
--process arg
for i = 1, #arg do
if arg[i] == "-h" then
printUsage()
elseif arg[i] == "-f" then --quadProgram
do --make my editor look clearer
myError("arg "..i.." Can't load file", 2)
i = i + 1 --this is definatly really bad practise
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
i = i + 1 --this is definatly really bad practise
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
i = i + 1 --this is definatly really bad practise
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
i = i + 1 --this is definatly really bad practise
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 = r
else
myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
end
i = i + 1 --this is definatly really bad practise
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
nyError("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
i = i + 1 --this is definatly really bad practise
end --make my editor look clearer
else
myError("arg "..i.." unprocessed arg",2)
end
endEdited by Lupus590, 09 December 2015 - 03:07 PM.












