Skip to content

Commit 7831fb0

Browse files
authored
flag: improve the fix for #25166 tail/single bool flag error in flag_to.v (#25189)
1 parent 214628a commit 7831fb0

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

vlib/flag/flag_to.v

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,7 @@ pub fn (fm FlagMapper) to_struct[T](defaults ?T) !T {
934934
.f64()
935935
} $else $if field.typ is bool {
936936
if arg := f.arg {
937-
if arg != '' {
938-
return error('can not assign `${arg}` to bool field `${field.name}`')
939-
}
937+
return error('can not assign `${arg}` to bool field `${field.name}`')
940938
}
941939
result.$(field.name) = !the_default.$(field.name)
942940
} $else $if field.typ is string {
@@ -1037,7 +1035,7 @@ fn (mut fm FlagMapper) map_v(flag_ctx FlagContext, field StructField) !bool {
10371035
next := flag_ctx.next
10381036

10391037
if field.hints.has(.is_bool) {
1040-
if flag_name == field.match_name {
1038+
if flag_name == field.match_name || flag_name == field.short {
10411039
arg := if flag_raw.contains('=') { flag_raw.all_after('=') } else { '' }
10421040
if arg != '' {
10431041
return error('flag `${flag_raw}` can not be assigned to bool field "${field.name}"')

vlib/flag/flag_to_tail_bool_test.v

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
11
import flag
22

3-
const args_bool_short = ['some.exe', '-h']
4-
const args_bool_long = ['some.exe', '-help']
3+
const args_bool_short_tail = ['some.exe', '-h']
4+
const args_bool_long_tail = ['some.exe', '-help']
5+
6+
const args_bool_short_mixed = ['some.exe', '-h', '-long', 'val']
7+
const args_bool_long_mixed = ['some.exe', '-help', '-long', 'val']
58

69
struct CliOptions {
710
show_help bool @[long: 'help'; short: h]
811
}
912

10-
fn test_short_tail_bool() {
11-
cli_options, unmatched := flag.to_struct[CliOptions](args_bool_short,
13+
struct CliOptions2 {
14+
show_help bool @[long: 'help'; short: h]
15+
long string
16+
}
17+
18+
fn test_v_style_short_tail_bool() {
19+
cli_options, unmatched := flag.to_struct[CliOptions](args_bool_short_tail,
20+
skip: 1
21+
style: .v
22+
mode: .relaxed
23+
)!
24+
25+
if unmatched.len > 0 {
26+
assert false
27+
}
28+
if cli_options.show_help {
29+
assert true
30+
} else {
31+
assert false
32+
}
33+
}
34+
35+
fn test_v_style_long_tail_bool() {
36+
cli_options, unmatched := flag.to_struct[CliOptions](args_bool_long_tail,
37+
skip: 1
38+
style: .v
39+
mode: .relaxed
40+
)!
41+
42+
if unmatched.len > 0 {
43+
assert false
44+
}
45+
if cli_options.show_help {
46+
assert true
47+
} else {
48+
assert false
49+
}
50+
}
51+
52+
fn test_v_style_short_bool() {
53+
cli_options, unmatched := flag.to_struct[CliOptions2](args_bool_short_mixed,
1254
skip: 1
1355
style: .v
1456
mode: .relaxed
@@ -22,10 +64,11 @@ fn test_short_tail_bool() {
2264
} else {
2365
assert false
2466
}
67+
assert cli_options.long == 'val'
2568
}
2669

27-
fn test_long_tail_bool() {
28-
cli_options, unmatched := flag.to_struct[CliOptions](args_bool_long,
70+
fn test_v_style_long_bool() {
71+
cli_options, unmatched := flag.to_struct[CliOptions2](args_bool_long_mixed,
2972
skip: 1
3073
style: .v
3174
mode: .relaxed
@@ -39,4 +82,5 @@ fn test_long_tail_bool() {
3982
} else {
4083
assert false
4184
}
85+
assert cli_options.long == 'val'
4286
}

0 commit comments

Comments
 (0)