From 76575e834b9fd523179071340f794a8a1dcb3638 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sat, 15 Jun 2024 00:18:49 -0400 Subject: [PATCH] Raise error on spaced UNIX-style flag with no value --- core/flags/parsing.odin | 9 ++++++++- tests/core/flags/test_core_flags.odin | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/flags/parsing.odin b/core/flags/parsing.odin index d8aea513f..f09c4c690 100644 --- a/core/flags/parsing.odin +++ b/core/flags/parsing.odin @@ -1,6 +1,7 @@ package flags import "core:container/bit_array" +import "core:fmt" Parsing_Style :: enum { // Odin-style: `-flag`, `-flag:option`, `-map:key=value` @@ -71,9 +72,15 @@ parse :: proc( return } - for /**/; future_args > 0; future_args -= 1 { + for starting_future_args := future_args; future_args > 0; future_args -= 1 { i += 1 if i == len(args) { + if future_args == starting_future_args { + return Parse_Error { + .No_Value, + fmt.tprintf("Expected a value for `%s` but none was given.", current_flag), + } + } break } #no_bounds_check arg = args[i] diff --git a/tests/core/flags/test_core_flags.odin b/tests/core/flags/test_core_flags.odin index cbe5e93a5..71fc34701 100644 --- a/tests/core/flags/test_core_flags.odin +++ b/tests/core/flags/test_core_flags.odin @@ -1055,6 +1055,23 @@ test_unix_double_dash_variadic :: proc(t: ^testing.T) { testing.expect_value(t, s.varg[2], "5") } +@(test) +test_unix_no_value :: proc(t: ^testing.T) { + S :: struct { + i: int, + } + s: S + + args := [?]string { "--i" } + + result := flags.parse(&s, args[:], .Unix) + err, ok := result.(flags.Parse_Error) + testing.expectf(t, ok, "unexpected result: %v", result) + if ok { + testing.expect_value(t, err.reason, flags.Parse_Error_Reason.No_Value) + } +} + // This test ensures there are no bad frees with cstrings. @(test) test_if_dynamic_cstrings_get_freed :: proc(t: ^testing.T) {