extends Node

var parsed_args_cached: bool = false
var cached_parsed_args: Dictionary = {}


func print_help() -> void:
	pass


func get_cmdline_args() -> Dictionary:
	"""
	Returns:
		The command line arguments in the form {key: value} where both `key`
		and `value` are strings. Caches the result on the first call and
		returns the cached result for subsequent calls
	"""
	if parsed_args_cached:
		return cached_parsed_args
	else:
		var parsed_args: Dictionary = {}
		for arg in OS.get_cmdline_args():
			if arg.begins_with("--"):
				var key_value: PoolStringArray = arg.split("=", true, 1)
				var value: String = "" if len(key_value) < 2 else key_value[1]
				parsed_args[key_value[0].lstrip("--")] = value
		cached_parsed_args = parsed_args
		parsed_args_cached = true
		return parsed_args


func get_rpc_address() -> PoolStringArray:
	"""
	Returns:
		The address and port of the RPC interface if given as a command line
		argument, otherwise empty
	"""
	var args: Dictionary = get_cmdline_args()
	if args.has("rpc-address") and len(args["rpc-address"]) > 0:
		var address: PoolStringArray = args["rpc-address"].rsplit(":", true, 1)
		# Replace "localhost" because the `HTTPRequest` node does not resolve
		# that its self
		if address[0] == "localhost":
			address[0] = "127.0.0.1"
		return address
	return PoolStringArray()
