rat-times/scripts/cmd.gd

71 lines
2.4 KiB
GDScript3
Raw Permalink Normal View History

2023-03-03 21:44:32 +00:00
class_name CMD
2023-03-09 20:26:57 +00:00
2023-04-22 01:08:05 +00:00
var _parsed := false
## @type Dictionary[String, String|bool]
var command_line_arguments: Dictionary = {} setget set_command_line_arguments, get_command_line_arguments
## Removes the first element find from the `quotes` array from the start and end of a string
## Also removes any whitespace resulting from removing the quoting elements
static func unsurround(value: String, quotes := PoolStringArray(['"', "'"])) -> String:
2023-03-09 20:26:57 +00:00
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-04-22 01:08:05 +00:00
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-04-22 01:08:05 +00:00
static func parse_cmd_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-04-22 01:08:05 +00:00
func set_command_line_arguments(_arguments: Dictionary) -> void:
printerr("get_command_line_arguments is a read only value")
func get_command_line_arguments() -> Dictionary:
if not _parsed:
_parsed = true
command_line_arguments = parse_cmd_arguments()
return command_line_arguments
2023-03-03 21:44:32 +00:00
2023-04-22 01:08:05 +00:00
## Returns a single argument passed after `--` on the command line
## if the argument does not exist, `default` is returned instead
## _parse_cmd_arguments() has to be called first
2023-03-09 20:26:57 +00:00
func get_argument(name: String, default = null):
2023-04-22 01:08:05 +00:00
if get_command_line_arguments().has(name):
return get_command_line_arguments()[name]
2023-03-03 21:44:32 +00:00
return default
2023-04-22 01:08:05 +00:00
## Verifies an argument exists on the command line
## _parse_cmd_arguments() has to be called first
2023-03-03 21:44:32 +00:00
func has_argument(name: String) -> bool:
2023-04-22 01:08:05 +00:00
return get_command_line_arguments().has(name)