function getopt(arg, opts) result = {} processed = {} for i = 1, arg.n do if (string.sub(arg[i], 1, 1) == "-") then for j = 2, string.len(arg[i]) do opt = string.sub(arg[i], j, j) find = string.find(opts, opt) if find ~= nil then if string.sub(opts, find+1, find+1) == ":" then -- opt takes an arg if string.len(arg[i]) > 2 then -- opt takes an arg, arg directly follows opt result[opt] = string.sub(arg[i], 3) processed[i] = 1 break elseif arg.n >= (i+1) then -- opt takes an arg, arg must be next arg in list result[opt] = arg[i+1] processed[i] = 1 processed[i+1] = 1 else -- opt takes an arg, but was not given one print("Option requires an argument: -" .. opt) result[opt] = 1 processed[i] = 1 end else -- opt does not take an arg result[opt] = 1 processed[i] = 1 end else -- unknown opt print("Unrecognized option -" .. opt) processed[i] = 1 end end end end -- remove whatever options we've processed for i = arg.n,1,-1 do if processed[i] then table.remove(arg,i) end end return result end