cli.gd 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class_name _ModLoaderCLI
  2. extends RefCounted
  3. # This Class provides util functions for working with cli arguments.
  4. # Currently all of the included functions are internal and should only be used by the mod loader itself.
  5. const LOG_NAME := "ModLoader:CLI"
  6. # Check if the provided command line argument was present when launching the game
  7. static func is_running_with_command_line_arg(argument: String) -> bool:
  8. for arg in OS.get_cmdline_args():
  9. if argument == arg.split("=")[0]:
  10. return true
  11. return false
  12. # Get the command line argument value if present when launching the game
  13. static func get_cmd_line_arg_value(argument: String) -> String:
  14. var args := _get_fixed_cmdline_args()
  15. for arg_index in args.size():
  16. var arg := args[arg_index] as String
  17. var key := arg.split("=")[0]
  18. if key == argument:
  19. # format: `--arg=value` or `--arg="value"`
  20. if "=" in arg:
  21. var value := arg.trim_prefix(argument + "=")
  22. value = value.trim_prefix('"').trim_suffix('"')
  23. value = value.trim_prefix("'").trim_suffix("'")
  24. return value
  25. # format: `--arg value` or `--arg "value"`
  26. elif arg_index +1 < args.size() and not args[arg_index +1].begins_with("--"):
  27. return args[arg_index + 1]
  28. return ""
  29. static func _get_fixed_cmdline_args() -> PackedStringArray:
  30. return fix_godot_cmdline_args_string_space_splitting(OS.get_cmdline_args())
  31. # Reverses a bug in Godot, which splits input strings at spaces even if they are quoted
  32. # e.g. `--arg="some value" --arg-two 'more value'` becomes `[ --arg="some, value", --arg-two, 'more, value' ]`
  33. static func fix_godot_cmdline_args_string_space_splitting(args: PackedStringArray) -> PackedStringArray:
  34. if not OS.has_feature("editor"): # only happens in editor builds
  35. return args
  36. if OS.has_feature("windows"): # windows is unaffected
  37. return args
  38. var fixed_args := PackedStringArray([])
  39. var fixed_arg := ""
  40. # if we encounter an argument that contains `=` followed by a quote,
  41. # or an argument that starts with a quote, take all following args and
  42. # concatenate them into one, until we find the closing quote
  43. for arg in args:
  44. var arg_string := arg as String
  45. if '="' in arg_string or '="' in fixed_arg or \
  46. arg_string.begins_with('"') or fixed_arg.begins_with('"'):
  47. if not fixed_arg == "":
  48. fixed_arg += " "
  49. fixed_arg += arg_string
  50. if arg_string.ends_with('"'):
  51. fixed_args.append(fixed_arg.trim_prefix(" "))
  52. fixed_arg = ""
  53. continue
  54. # same thing for single quotes
  55. elif "='" in arg_string or "='" in fixed_arg \
  56. or arg_string.begins_with("'") or fixed_arg.begins_with("'"):
  57. if not fixed_arg == "":
  58. fixed_arg += " "
  59. fixed_arg += arg_string
  60. if arg_string.ends_with("'"):
  61. fixed_args.append(fixed_arg.trim_prefix(" "))
  62. fixed_arg = ""
  63. continue
  64. else:
  65. fixed_args.append(arg_string)
  66. return fixed_args