Update argparser2.lua

This commit is contained in:
LDDestroier 2022-01-08 13:31:22 -05:00 committed by GitHub
parent d3d8b7a549
commit 721a622b3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 18 deletions

View File

@ -7,13 +7,15 @@ local function checkOption(argName, argInfo, isShort)
return false return false
end end
function argParse(argInput, argInfo) local function argParse(argInput, argInfo)
local sDelim = "-" local sDelim = "-"
local lDelim = "--" local lDelim = "--"
optOutput = {} local optOutput = {}
argOutput = {} local argOutput = {}
argError = {} local argError = {}
local usedTokens = {}
local lOpt, sOpt, optNum local lOpt, sOpt, optNum
@ -33,11 +35,13 @@ function argParse(argInput, argInfo)
for ii = 1, argInfo[optNum][1] do for ii = 1, argInfo[optNum][1] do
if argInput[i + ii] then if argInput[i + ii] then
optOutput[optNum][#optOutput[optNum] + 1] = argInput[i + ii] optOutput[optNum][#optOutput[optNum] + 1] = argInput[i + ii]
usedTokens[i + ii] = true
else else
argError[#argError + 1] = "expected parameter " .. tostring(ii) .. " for argument " .. lDelim .. lOpt argError[#argError + 1] = "expected parameter " .. tostring(ii) .. " for argument " .. lDelim .. lOpt
break break
end end
end end
i = i + argInfo[optNum][1]
end end
else else
@ -60,11 +64,13 @@ function argParse(argInput, argInfo)
for ii = 1, argInfo[optNum][1] do for ii = 1, argInfo[optNum][1] do
if argInput[i + ii] then if argInput[i + ii] then
optOutput[optNum][#optOutput[optNum] + 1] = argInput[i + ii] optOutput[optNum][#optOutput[optNum] + 1] = argInput[i + ii]
usedTokens[i + ii] = true
else else
argError[#argError + 1] = "expected parameter " .. tostring(ii) .. " for argument " .. sDelim .. sOpt argError[#argError + 1] = "expected parameter " .. tostring(ii) .. " for argument " .. sDelim .. sOpt
break break
end end
end end
i = i + argInfo[optNum][1]
else else
argError[#argError + 1] = "options with parameters must be at the end of their group" argError[#argError + 1] = "options with parameters must be at the end of their group"
break break
@ -74,10 +80,9 @@ function argParse(argInput, argInfo)
break break
end end
end end
else elseif not usedTokens[i] then
argOutput[#argOutput + 1] = lOpt argOutput[#argOutput + 1] = lOpt
end end
end end
@ -85,19 +90,64 @@ function argParse(argInput, argInfo)
return argOutput, optOutput, argError return argOutput, optOutput, argError
end end
local tArg = {...} local function demoArgParse(...)
-- {amount of parameters for the option, short option, long option} --[[
local argInfo = { argInfo is structured as such:
[1] = {0, "h", "help"}, {
[2] = {0, "w", "what"}, amount of parameters for the option (usually 0 or 1),
[3] = {1, "n", "name"} short option,
} long option
}
--]]
arguments, options, errors = argParse(tArg, argInfo) local argInfo = {
[1] = {0, "h", "help"},
[2] = {0, "w", "what"},
[3] = {1, "n", "name"}
}
print(textutils.serialise(arguments)) --[[
print(textutils.serialise(options)) with this, you can do the following:
if #errors > 0 then "argparse.lua --help --what --name LDD"
printError(textutils.serialise(errors)) "argparse.lua -hwn LDD"
"argparse.lua --help -wn LDD"
return 1 is a table of arguments (not options)
return 2 is a table of options (not regular arguments)
return 3 is a table of errors (invalid inputs)
--]]
arguments, options, errors = argParse({...}, argInfo)
if #errors > 0 then
for i = 1, #errors do
printError(errors[i])
end
else
for i = 1, #arguments do
write(arguments[i])
if i == #arguments then
write("\n")
else
write(", ")
end
end
for i,v in pairs(options) do
write("--" .. argInfo[i][3] .. " ")
if argInfo[i][1] > 0 then
for ii = 1, #options[i] do
write(options[i][ii])
if ii == #options[i] then
write("\n")
else
write(", ")
end
end
end
end
end
end end
return argParse