mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
flag: correct bool logic, add test (#22162)
This commit is contained in:
parent
d5c2ebce05
commit
0d35f0948c
2 changed files with 23 additions and 1 deletions
|
@ -832,6 +832,7 @@ fn keep_at_max(str string, max int) string {
|
|||
pub fn (fm FlagMapper) to_struct[T](defaults ?T) !T {
|
||||
// Generate T result
|
||||
mut result := defaults or { T{} }
|
||||
the_default := defaults or { T{} }
|
||||
|
||||
$if T is $struct {
|
||||
struct_name := T.name
|
||||
|
@ -907,7 +908,7 @@ pub fn (fm FlagMapper) to_struct[T](defaults ?T) !T {
|
|||
if arg := f.arg {
|
||||
return error('can not assign `${arg}` to bool field `${field.name}`')
|
||||
}
|
||||
result.$(field.name) = true
|
||||
result.$(field.name) = !the_default.$(field.name)
|
||||
} $else $if field.typ is string {
|
||||
trace_dbg_println('${@FN}: assigning (string) ${struct_name}.${field.name} = ${f.arg or {
|
||||
'ERROR'
|
||||
|
|
21
vlib/flag/flag_to_bool_test.v
Normal file
21
vlib/flag/flag_to_bool_test.v
Normal file
|
@ -0,0 +1,21 @@
|
|||
import flag
|
||||
|
||||
const gnu_args_bool_flags = ['--no-parallel', '--nocache', '--stay', '--nix']
|
||||
|
||||
struct BoolConfig {
|
||||
mix bool
|
||||
nix bool
|
||||
parallel bool = true @[long: 'no-parallel']
|
||||
cache bool @[long: nocache]
|
||||
no_stay bool @[long: 'stay']
|
||||
}
|
||||
|
||||
fn test_bool_flags() {
|
||||
bf, _ := flag.to_struct[BoolConfig](gnu_args_bool_flags, style: .long)!
|
||||
|
||||
assert bf.mix == false
|
||||
assert bf.nix == true
|
||||
assert bf.parallel == false
|
||||
assert bf.cache == true
|
||||
assert bf.no_stay == true
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue