Skip to content

Commit e55a404

Browse files
committed
Simple arg parsing
1 parent 9b8c255 commit e55a404

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/core.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,9 @@ const environments* = {
340340
"gb": 0.02,
341341
"gb2": 0.02,
342342
"strange": 0.5,
343-
"wild_imagination": 0.3
343+
"wild_imagination": 0.3,
344344
}.to_table
345+
346+
template `?=`*[T](a: var T, b: T) =
347+
if not ?a:
348+
a = b

src/game.nim

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import
77
viewport, viewport_texture, performance, label, theme, dynamic_font,
88
resource_loader, main_loop, project_settings, input_map, input_event_action,
99
input_event_key, input_event, global_constants, scroll_container,
10-
voxel_server, world_environment
10+
voxel_server, world_environment,
1111
]
1212

1313
import ui/virtual_joystick
@@ -22,9 +22,8 @@ when defined(metrics):
2222
ZenContext.init_metrics "main", "worker"
2323

2424
# saved state when restarting worker thread
25-
const savable_flags = {
26-
ConsoleVisible, MouseCaptured, Flying, God, AltWalkSpeed, AltFlySpeed
27-
}
25+
const savable_flags =
26+
{ConsoleVisible, MouseCaptured, Flying, God, AltWalkSpeed, AltFlySpeed}
2827

2928
var environment_cache {.threadvar.}: Table[string, Environment]
3029

@@ -150,17 +149,27 @@ gdobj Game of Node:
150149

151150
randomize()
152151

152+
var args = get_cmdline_args().to_seq
153+
153154
var connect_address = ""
154155
var listen_address = ""
156+
if (let i = args.find("--connect"); i) > -1 and args.len > i + 1:
157+
connect_address = args[i + 1]
158+
args.delete(i .. i + 1)
159+
if (let i = args.find("--listen"); i) > -1:
160+
if args.len > i + 1:
161+
listen_address = args[i + 1]
162+
args.delete(i .. i + 1)
163+
else:
164+
listen_address = "0.0.0.0"
165+
args.delete(i)
155166

156-
if ?get_env("ENU_LISTEN_ADDRESS") and ?get_env("ENU_CONNECT_ADDRESS"):
157-
fail "Cannot set both ENU_LISTEN_ADDRESS and ENU_CONNECT_ADDRESS"
158-
elif ?get_env("ENU_LISTEN_ADDRESS"):
167+
if ?get_env("ENU_LISTEN_ADDRESS") and not ?listen_address:
159168
listen_address = get_env("ENU_LISTEN_ADDRESS")
160-
connect_address = ""
161-
elif ?get_env("ENU_CONNECT_ADDRESS"):
169+
if ?get_env("ENU_CONNECT_ADDRESS") and not ?connect_address:
162170
connect_address = get_env("ENU_CONNECT_ADDRESS")
163-
listen_address = ""
171+
if ?listen_address and ?connect_address:
172+
fail "Cannot set both ENU_LISTEN_ADDRESS and ENU_CONNECT_ADDRESS"
164173

165174
if ?saved_state.connect_address:
166175
connect_address = saved_state.connect_address

0 commit comments

Comments
 (0)