From d5d392a2e592b29d508b01d0cde25af6c85625a1 Mon Sep 17 00:00:00 2001 From: larpon <768942+larpon@users.noreply.github.com> Date: Tue, 26 Aug 2025 12:22:12 +0200 Subject: [PATCH] flag: fix tail/single bool flag error condition logic in `flag_to.v` (fix #25166) (#25172) --- vlib/flag/flag_to.v | 4 ++- vlib/flag/flag_to_tail_bool_test.v | 42 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 vlib/flag/flag_to_tail_bool_test.v diff --git a/vlib/flag/flag_to.v b/vlib/flag/flag_to.v index 8f46fd20c2..876d65d995 100644 --- a/vlib/flag/flag_to.v +++ b/vlib/flag/flag_to.v @@ -934,7 +934,9 @@ pub fn (fm FlagMapper) to_struct[T](defaults ?T) !T { .f64() } $else $if field.typ is bool { if arg := f.arg { - return error('can not assign `${arg}` to bool field `${field.name}`') + if arg != '' { + return error('can not assign `${arg}` to bool field `${field.name}`') + } } result.$(field.name) = !the_default.$(field.name) } $else $if field.typ is string { diff --git a/vlib/flag/flag_to_tail_bool_test.v b/vlib/flag/flag_to_tail_bool_test.v new file mode 100644 index 0000000000..a67e923f44 --- /dev/null +++ b/vlib/flag/flag_to_tail_bool_test.v @@ -0,0 +1,42 @@ +import flag + +const args_bool_short = ['some.exe', '-h'] +const args_bool_long = ['some.exe', '-help'] + +struct CliOptions { + show_help bool @[long: 'help'; short: h] +} + +fn test_short_tail_bool() { + cli_options, unmatched := flag.to_struct[CliOptions](args_bool_short, + skip: 1 + style: .v + mode: .relaxed + )! + + if unmatched.len > 0 { + assert false + } + if cli_options.show_help { + assert true + } else { + assert false + } +} + +fn test_long_tail_bool() { + cli_options, unmatched := flag.to_struct[CliOptions](args_bool_long, + skip: 1 + style: .v + mode: .relaxed + )! + + if unmatched.len > 0 { + assert false + } + if cli_options.show_help { + assert true + } else { + assert false + } +}