From 5c4e0eec6a21783d7bf9b248aee34ac6476b10f7 Mon Sep 17 00:00:00 2001 From: Xananax Date: Sat, 4 Mar 2023 01:44:32 +0400 Subject: [PATCH] start command line setup --- config_manager.tres | 4 +- main.gd | 25 +++++++++++ project.godot | 2 +- scripts/cmd.gd | 43 +++++++++++++++++++ .../config_manager.gd | 0 Main.gd => ui/Main.gd | 0 Main.tscn => ui/Main.tscn | 16 +++---- 7 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 main.gd create mode 100644 scripts/cmd.gd rename config_manager.gd => scripts/config_manager.gd (100%) rename Main.gd => ui/Main.gd (100%) rename Main.tscn => ui/Main.tscn (96%) diff --git a/config_manager.tres b/config_manager.tres index 84b06f1..cf305a9 100644 --- a/config_manager.tres +++ b/config_manager.tres @@ -1,6 +1,6 @@ -[gd_resource type="Resource" script_class="ConfigManager" load_steps=2 format=3 uid="uid://e3yofdasfcli"] +[gd_resource type="Resource" script_class="ConfigManager" load_steps=2 format=3] -[ext_resource type="Script" path="res://config_manager.gd" id="1_xfu8y"] +[ext_resource type="Script" path="res://scripts/config_manager.gd" id="1_xfu8y"] [resource] script = ExtResource("1_xfu8y") diff --git a/main.gd b/main.gd new file mode 100644 index 0000000..f6feaff --- /dev/null +++ b/main.gd @@ -0,0 +1,25 @@ +#!/usr/bin/env -S godot --headless -s +extends SceneTree + +var cmd := CMD.new() + +func _init(): + for command in ["list", "stop", "start", "current"]: + if cmd.has_argument(command): + call(command) + + +func list() -> void: + pass + + +func stop() -> void: + pass + + +func start() -> void: + pass + + +func current() -> void: + pass diff --git a/project.godot b/project.godot index 5cad5e3..e60df6f 100644 --- a/project.godot +++ b/project.godot @@ -12,7 +12,7 @@ config_version=5 config/name="Rat Times" config/description="Track your time(s)s" -run/main_scene="res://Main.tscn" +run/main_scene="res://ui/Main.tscn" config/use_custom_user_dir=true config/custom_user_dir_name="rat_times" config/features=PackedStringArray("4.0") diff --git a/scripts/cmd.gd b/scripts/cmd.gd new file mode 100644 index 0000000..f4810c6 --- /dev/null +++ b/scripts/cmd.gd @@ -0,0 +1,43 @@ +class_name CMD + +## 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. +var command_line_arguments: Dictionary = (func get_command_line_arguments() -> Dictionary: + var unsurround := func unsurround(value: String, quotes := PackedStringArray(['"', "'"])) -> 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 + var arguments := {} + for argument in OS.get_cmdline_user_args(): + argument = argument.lstrip("--").to_lower() + if argument.find("=") > -1: + var arg_tuple := argument.split("=") + var key := arg_tuple[0] + var value:String = unsurround.call(arg_tuple[1]) + arguments[key] = value + else: + var key := argument + var value := true + if argument.begins_with("no-"): + value = false + key = argument.lstrip("no-") + arguments[key] = value + return arguments).call() + + +func get_argument(name: String, default: Variant = null) -> Variant: + 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) diff --git a/config_manager.gd b/scripts/config_manager.gd similarity index 100% rename from config_manager.gd rename to scripts/config_manager.gd diff --git a/Main.gd b/ui/Main.gd similarity index 100% rename from Main.gd rename to ui/Main.gd diff --git a/Main.tscn b/ui/Main.tscn similarity index 96% rename from Main.tscn rename to ui/Main.tscn index 5d11ecf..d196419 100644 --- a/Main.tscn +++ b/ui/Main.tscn @@ -1,9 +1,9 @@ [gd_scene load_steps=7 format=3 uid="uid://bmlciwscreowf"] -[ext_resource type="Theme" uid="uid://bd8ancgbfsvmd" path="res://assets/default_theme.theme" id="1_1mila"] -[ext_resource type="Script" path="res://Main.gd" id="1_vrowr"] -[ext_resource type="AudioStream" uid="uid://cdsbhoidgyx70" path="res://assets/pop.ogg" id="3_o37py"] -[ext_resource type="Script" path="res://ui/time_entries_items_tree.gd" id="3_wwscb"] +[ext_resource type="Theme" uid="uid://bd8ancgbfsvmd" path="res://assets/default_theme.theme" id="1_2s8h2"] +[ext_resource type="Script" path="res://ui/Main.gd" id="2_sl5q6"] +[ext_resource type="Script" path="res://ui/time_entries_items_tree.gd" id="3_oxqux"] +[ext_resource type="AudioStream" uid="uid://cdsbhoidgyx70" path="res://assets/pop.ogg" id="4_6ajaq"] [sub_resource type="InputEventKey" id="InputEventKey_guuii"] device = -1 @@ -20,7 +20,7 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 -theme = ExtResource("1_1mila") +theme = ExtResource("1_2s8h2") theme_type_variation = &"background" [node name="Main" type="MarginContainer" parent="."] @@ -29,7 +29,7 @@ theme_override_constants/margin_left = 5 theme_override_constants/margin_top = 5 theme_override_constants/margin_right = 5 theme_override_constants/margin_bottom = 5 -script = ExtResource("1_vrowr") +script = ExtResource("2_sl5q6") [node name="VBoxContainer" type="VBoxContainer" parent="Main"] layout_mode = 2 @@ -132,7 +132,7 @@ layout_mode = 2 size_flags_horizontal = 3 size_flags_vertical = 3 columns = 2 -script = ExtResource("3_wwscb") +script = ExtResource("3_oxqux") [node name="SettingsWindow" type="Window" parent="Main"] unique_name_in_owner = true @@ -235,4 +235,4 @@ This game uses Godot Engine, available under the following license: [node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."] unique_name_in_owner = true -stream = ExtResource("3_o37py") +stream = ExtResource("4_6ajaq")