Some CLI arg fixes. (0.37.36)
This commit is contained in:
parent
6124d1e7fd
commit
9cd76937a3
3 changed files with 33 additions and 9 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
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 99)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,21 @@ pos = parsed["positionals"]
|
||||||
if (flags["f"] == 1 || flags["force"] == 1)
|
if (flags["f"] == 1 || flags["force"] == 1)
|
||||||
print("Force enabled")
|
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(join(["Force value=", flags["force"]], ""))
|
||||||
|
|
||||||
print("Flags map:")
|
print("Flags map:")
|
||||||
print(flags)
|
print(flags)
|
||||||
print("Positionals:")
|
print("Positionals:")
|
||||||
print(pos)
|
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]
|
||||||
|
*/
|
||||||
|
|
|
||||||
27
lib/cli.fun
27
lib/cli.fun
|
|
@ -1,3 +1,14 @@
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://fun-lang.xyz/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||||
|
* 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.
|
// Simple CLI helpers for Fun scripts.
|
||||||
//
|
//
|
||||||
// argv(): returns an array of the raw arguments passed to the script
|
// argv(): returns an array of the raw arguments passed to the script
|
||||||
|
|
@ -14,7 +25,8 @@ fun argv()
|
||||||
a = []
|
a = []
|
||||||
i = 0
|
i = 0
|
||||||
while (i < n)
|
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))
|
push(a, env(key))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return a
|
return a
|
||||||
|
|
@ -27,23 +39,24 @@ fun parse_args(args)
|
||||||
n = len(args)
|
n = len(args)
|
||||||
while (i < n)
|
while (i < n)
|
||||||
s = args[i]
|
s = args[i]
|
||||||
if (len(s) >= 2 && substr(s, 0, 2) == "--")
|
slen = len(s)
|
||||||
|
if (slen >= 2 && substr(s, 0, 2) == "--")
|
||||||
eq = find(s, "=")
|
eq = find(s, "=")
|
||||||
if (eq >= 0)
|
if (eq >= 0)
|
||||||
key = substr(s, 2, eq - 2) // between -- and =
|
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
|
flags[key] = val
|
||||||
else
|
else
|
||||||
key = substr(s, 2, len(s) - 2)
|
key = substr(s, 2, slen - 2)
|
||||||
flags[key] = 1
|
flags[key] = 1
|
||||||
else if (len(s) == 2 && substr(s, 0, 1) == "-")
|
else if (slen == 2 && substr(s, 0, 1) == "-")
|
||||||
// short -f boolean
|
// short -f boolean
|
||||||
key = substr(s, 1, 1)
|
key = substr(s, 1, 1)
|
||||||
flags[key] = 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
|
// compact short flags like -abc -> a=1, b=1, c=1
|
||||||
j = 1
|
j = 1
|
||||||
while (j < len(s))
|
while (j < slen)
|
||||||
k = substr(s, j, 1)
|
k = substr(s, j, 1)
|
||||||
flags[k] = 1
|
flags[k] = 1
|
||||||
j = j + 1
|
j = j + 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue