From 9cd76937a3a86988861b987dccdc568309489a64 Mon Sep 17 00:00:00 2001 From: hanez Date: Tue, 30 Dec 2025 17:51:25 +0100 Subject: [PATCH] Some CLI arg fixes. (0.37.36) --- CMakeLists.txt | 2 +- examples/cli_parse_example.fun | 13 ++++++++++++- lib/cli.fun | 27 ++++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b08938..c5d90af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.37.35 LANGUAGES C) +project(fun VERSION 0.37.36 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/examples/cli_parse_example.fun b/examples/cli_parse_example.fun index f178ca3..1e5c399 100755 --- a/examples/cli_parse_example.fun +++ b/examples/cli_parse_example.fun @@ -28,10 +28,21 @@ pos = parsed["positionals"] if (flags["f"] == 1 || flags["force"] == 1) print("Force enabled") -if (has_key(flags, "force") && typeof(flags["force"]) == "string") +// No hasKey() builtin available here; check via keys()+contains() +flag_keys = keys(flags) +if (contains(flag_keys, "force") && typeof(flags["force"]) == "string") print(join(["Force value=", flags["force"]], "")) print("Flags map:") print(flags) print("Positionals:") print(pos) + +/* Expected output (FUN_LIB_DIR="$(pwd)/lib" ./build/fun ./examples/cli_parse_example.fun --force bar xx x x --for=xxx --h=1): +1 +Force enabled +Flags map: +{"force": 1, "for": xxx, "h": 1} +Positionals: +[bar, xx, x, x] +*/ diff --git a/lib/cli.fun b/lib/cli.fun index 70d2f0d..bf9ea6c 100644 --- a/lib/cli.fun +++ b/lib/cli.fun @@ -1,3 +1,14 @@ +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2025 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2025-12-30 + */ + // Simple CLI helpers for Fun scripts. // // argv(): returns an array of the raw arguments passed to the script @@ -14,7 +25,8 @@ fun argv() a = [] i = 0 while (i < n) - key = join(["FUN_ARGV_", to_string(i)], "") + // Build key without join() to avoid any potential call resolution issues + key = "FUN_ARGV_" + to_string(i) push(a, env(key)) i = i + 1 return a @@ -27,23 +39,24 @@ fun parse_args(args) n = len(args) while (i < n) s = args[i] - if (len(s) >= 2 && substr(s, 0, 2) == "--") + slen = len(s) + if (slen >= 2 && substr(s, 0, 2) == "--") eq = find(s, "=") if (eq >= 0) key = substr(s, 2, eq - 2) // between -- and = - val = substr(s, eq + 1, len(s) - (eq + 1)) + val = substr(s, eq + 1, slen - (eq + 1)) flags[key] = val else - key = substr(s, 2, len(s) - 2) + key = substr(s, 2, slen - 2) flags[key] = 1 - else if (len(s) == 2 && substr(s, 0, 1) == "-") + else if (slen == 2 && substr(s, 0, 1) == "-") // short -f boolean key = substr(s, 1, 1) flags[key] = 1 - else if (len(s) > 2 && substr(s, 0, 1) == "-") + else if (slen > 2 && substr(s, 0, 1) == "-") // compact short flags like -abc -> a=1, b=1, c=1 j = 1 - while (j < len(s)) + while (j < slen) k = substr(s, j, 1) flags[k] = 1 j = j + 1