rat-times/scripts/cmd.gd

48 lines
1.5 KiB
GDScript3
Raw Normal View History

2023-03-03 21:44:32 +00:00
class_name CMD
2023-03-09 20:26:57 +00:00
var command_line_arguments: Dictionary = {}
func unsurround(value: String, quotes := PoolStringArray(['"', "'"])) -> String:
for quote_str in quotes:
if value.begins_with(quote_str) \
and value.ends_with(quote_str) \
and value[value.length() - 2] != "\\":
return value.trim_prefix(quote_str).trim_suffix(quote_str).strip_edges()
return value
2023-03-03 21:44:32 +00:00
## Returns a dictionary of all arguments passed after `--` on the command line
## arguments take one of 2 forms:
## - `--arg` which is a boolean (using `--no-arg` for `false` is possible)
## - `--arg=value`. If the value is quoted with `"` or `'`, this function will
## unsurround the string
## This function does no evaluation and does not attempt to guess the type of
## arguments. You will receive either bools, or strings.
2023-03-09 20:26:57 +00:00
func _read_arguments() -> Dictionary:
2023-03-03 21:44:32 +00:00
var arguments := {}
2023-03-09 20:26:57 +00:00
for arg in OS.get_cmdline_args():
var argument: String = arg.lstrip("--").to_lower()
2023-03-03 21:44:32 +00:00
if argument.find("=") > -1:
var arg_tuple := argument.split("=")
var key := arg_tuple[0]
2023-03-09 20:26:57 +00:00
var value := unsurround(arg_tuple[1])
2023-03-03 21:44:32 +00:00
arguments[key] = value
else:
var key := argument
var value := true
if argument.begins_with("no-"):
value = false
key = argument.lstrip("no-")
arguments[key] = value
2023-03-09 20:26:57 +00:00
return arguments
2023-03-03 21:44:32 +00:00
2023-03-09 20:26:57 +00:00
func get_argument(name: String, default = null):
2023-03-03 21:44:32 +00:00
if command_line_arguments.has(name):
return command_line_arguments[name]
return default
func has_argument(name: String) -> bool:
return command_line_arguments.has(name)