From c3acdeb310c1426d009004956c1b3e59e4920808 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Fri, 14 Jun 2024 23:59:01 -0400 Subject: [PATCH] Add `--` parsing to UNIX-style `core:flags` This will allow a user to indicate all arguments after `--` are parsed into the variadic array. --- core/flags/internal_parsing.odin | 8 ++++++++ tests/core/flags/test_core_flags.odin | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/core/flags/internal_parsing.odin b/core/flags/internal_parsing.odin index 349afdd29..7a769b17c 100644 --- a/core/flags/internal_parsing.odin +++ b/core/flags/internal_parsing.odin @@ -90,6 +90,14 @@ parse_one_unix_arg :: proc(model: ^$T, parser: ^Parser, arg: string) -> ( if strings.has_prefix(arg, "-") { // Allow `--` to function as `-`. arg = arg[1:] + + if len(arg) == 0 { + // `--`, and only `--`. + // Everything from now on will be treated as an argument. + future_args = max(int) + current_flag = INTERNAL_VARIADIC_FLAG + return + } } flag: string diff --git a/tests/core/flags/test_core_flags.odin b/tests/core/flags/test_core_flags.odin index a9efa5e14..cbe5e93a5 100644 --- a/tests/core/flags/test_core_flags.odin +++ b/tests/core/flags/test_core_flags.odin @@ -1028,6 +1028,33 @@ test_unix_positional_with_variadic :: proc(t: ^testing.T) { testing.expect_value(t, len(s.v), 2) } +@(test) +test_unix_double_dash_variadic :: proc(t: ^testing.T) { + S :: struct { + varg: [dynamic]string, + i: int, + } + s: S + + args := [?]string { "-i", "3", "--", "hellope", "-i", "5" } + + result := flags.parse(&s, args[:], .Unix) + defer { + delete(s.varg) + } + testing.expect_value(t, result, nil) + testing.expect_value(t, len(s.varg), 3) + testing.expect_value(t, s.i, 3) + + if len(s.varg) != 3 { + return + } + + testing.expect_value(t, s.varg[0], "hellope") + testing.expect_value(t, s.varg[1], "-i") + testing.expect_value(t, s.varg[2], "5") +} + // This test ensures there are no bad frees with cstrings. @(test) test_if_dynamic_cstrings_get_freed :: proc(t: ^testing.T) {