Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pp:ifndef INCL_GETOPT
- --pp:define INCL_GETOPT
- local getopt = {}
- function getopt.help(name, desc, options)
- local helpStr = name .. " -- " .. desc .. "\n"
- .. "USAGE: " .. name .. " [options] [args]\n"
- for k, v in pairs(options) do
- helpStr = helpStr .. "\n--" .. k .. " (-" .. v[2] .. ")"
- if v[3] ~= nil then
- helpStr = helpStr .. " [" .. v[3] .. "]"
- end
- helpStr = helpStr .. ": " .. v[1]
- end
- textutils.pagedPrint(helpStr)
- end
- -- table or nil getopt.init(string programDescription, table optionsTable, table args)
- -- NOTE: In optionsTable, you can't have --help or -h because getopt creates them for you.
- function getopt.init(name, desc, options, args)
- local _resTbl = {}
- local _isArg = false
- local _optCnt = 1
- for i, v in ipairs(args) do
- if v == "-h" or v == "--help" then
- _resTbl = {}
- getopt.help(name, desc, options)
- return nil
- end
- if v:sub(1, 1) == "-" then
- for j, x in pairs(options) do
- if v == "--" .. j or v == "-" .. x[2] then
- if x[3] ~= nil then
- _resTbl[j] = args[i + 1]
- _isArg = true
- else
- _resTbl[j] = true
- end
- end
- end
- elseif not _isArg then
- _resTbl["opt-" .. _optCnt] = v
- _optCnt = _optCnt + 1
- else
- _isArg = false
- end
- end
- return _resTbl
- end
- --pp:ignoreend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement