Added some CLI args support. (0.37.35)
This commit is contained in:
parent
e144b5119d
commit
6124d1e7fd
6 changed files with 173 additions and 2 deletions
32
examples/cli_argv_dump.fun
Executable file
32
examples/cli_argv_dump.fun
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
// Demonstrates accessing raw script arguments via lib/cli.fun
|
||||
// Usage:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build_debug/fun ./examples/cli_argv_dump.fun -f --force=42 foo bar
|
||||
|
||||
#include <cli.fun>
|
||||
|
||||
args = argv()
|
||||
|
||||
print(join(["FUN_ARGC=", env("FUN_ARGC")], ""))
|
||||
print(join(["FUN_ARGS='", env("FUN_ARGS"), "'"], ""))
|
||||
print("Raw argv array:")
|
||||
print(args)
|
||||
|
||||
/* Expected output:
|
||||
FUN_ARGC=3
|
||||
FUN_ARGS='-f --force --force=42'
|
||||
Raw argv array:
|
||||
[-f, --force, --force=42
|
||||
*/
|
||||
37
examples/cli_parse_example.fun
Executable file
37
examples/cli_parse_example.fun
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
// Demonstrates parsing flags like -f, --force, --force=42 and positionals
|
||||
// Usage examples:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build_debug/fun ./examples/cli_parse_example.fun -f foo
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build_debug/fun ./examples/cli_parse_example.fun --force bar
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build_debug/fun ./examples/cli_parse_example.fun --force=42 alpha beta
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build_debug/fun ./examples/cli_parse_example.fun -abc x y
|
||||
|
||||
#include <cli.fun>
|
||||
|
||||
args = argv()
|
||||
parsed = parse_args(args)
|
||||
flags = parsed["flags"]
|
||||
pos = parsed["positionals"]
|
||||
|
||||
if (flags["f"] == 1 || flags["force"] == 1)
|
||||
print("Force enabled")
|
||||
|
||||
if (has_key(flags, "force") && typeof(flags["force"]) == "string")
|
||||
print(join(["Force value=", flags["force"]], ""))
|
||||
|
||||
print("Flags map:")
|
||||
print(flags)
|
||||
print("Positionals:")
|
||||
print(pos)
|
||||
0
examples/env_all.fun
Normal file → Executable file
0
examples/env_all.fun
Normal file → Executable file
Loading…
Add table
Add a link
Reference in a new issue