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 + } +}