util/argparse.lua
author Kim Alvefur <zash@zash.se>
Thu, 28 Mar 2024 15:39:59 +0100
changeset 13473 f9171624fd03
parent 13164 4ee9a912ceea
permissions -rw-r--r--
MUC: Fix legacy hats (thanks nicoco) Why do we not have tests for this?

local function parse(arg, config)
	local short_params = config and config.short_params or {};
	local value_params = config and config.value_params or {};
	local array_params = config and config.array_params or {};

	local parsed_opts = {};

	if #arg == 0 then
		return parsed_opts;
	end
	while true do
		local raw_param = arg[1];
		if not raw_param then
			break;
		end

		local prefix = raw_param:match("^%-%-?");
		if not prefix then
			break;
		elseif prefix == "--" and raw_param == "--" then
			table.remove(arg, 1);
			break;
		end
		local param = table.remove(arg, 1):sub(#prefix+1);
		if #param == 1 and short_params then
			param = short_params[param];
		end

		if not param then
			return nil, "param-not-found", raw_param;
		end

		local param_k, param_v;
		if value_params[param] or array_params[param] then
			param_k, param_v = param, table.remove(arg, 1);
			if not param_v then
				return nil, "missing-value", raw_param;
			end
		else
			param_k, param_v = param:match("^([^=]+)=(.+)$");
			if not param_k then
				if param:match("^no%-") then
					param_k, param_v = param:sub(4), false;
				else
					param_k, param_v = param, true;
				end
			end
			param_k = param_k:gsub("%-", "_");
		end
		if array_params[param] then
			if parsed_opts[param_k] then
				table.insert(parsed_opts[param_k], param_v);
			else
				parsed_opts[param_k] = { param_v };
			end
		else
			parsed_opts[param_k] = param_v;
		end
	end
	for i = 1, #arg do
		parsed_opts[i] = arg[i];
	end
	return parsed_opts;
end

return {
	parse = parse;
}