Browse Source

Add `--` parsing to UNIX-style `core:flags`

This will allow a user to indicate all arguments after `--` are parsed
into the variadic array.
Feoramund 1 year ago
parent
commit
c3acdeb310
2 changed files with 35 additions and 0 deletions
  1. 8 0
      core/flags/internal_parsing.odin
  2. 27 0
      tests/core/flags/test_core_flags.odin

+ 8 - 0
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

+ 27 - 0
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) {