flag: add missing short flag match in flag.to_struct, add test (#22696)

This commit is contained in:
larpon 2024-10-30 16:31:31 +01:00 committed by GitHub
parent 3af720921c
commit dad8e3c766
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View File

@ -1609,6 +1609,22 @@ fn (mut fm FlagMapper) map_posix_short(flag_ctx FlagContext, field StructField)
fm.handled_pos << pos + 1 // next
}
return true
} else if !flag_ctx.next.starts_with(used_delimiter) {
if field.short == flag_name {
trace_println('${@FN}: found match for (${field.type_name}) ${fm.dbg_match(flag_ctx,
field, next, '')}')
fm.field_map_flag[field.name] = FlagData{
raw: flag_raw
field_name: field.name
delimiter: used_delimiter
name: flag_name
arg: ?string(next)
pos: pos
}
fm.handled_pos << pos
fm.handled_pos << pos + 1 // next
return true
}
}
}
if (fm.config.style == .short || field.hints.has(.short_only)) && first_letter == field.short {

View File

@ -0,0 +1,25 @@
import flag
const edge_case = ['appimage', '-v', '3', '-o', '/tmp/lol.appimage', '/home/user/Projects/game/']
pub struct AppImageOptions {
pub:
verbosity int @[short: v; xdoc: 'Verbosity level 1-3']
dump_usage bool @[long: help; short: h; xdoc: 'Show this help message and exit']
show_version bool @[long: version; xdoc: 'Output version information and exit']
pub mut:
input string @[tail]
output string @[short: o; xdoc: 'Path to output (dir/file)']
assets []string @[short: a; xdoc: 'Asset dir(s) to include in build']
}
fn test_edge_case() {
aio, no_matches := flag.to_struct[AppImageOptions](edge_case, skip: 1)!
assert aio.verbosity == 3
assert aio.output == '/tmp/lol.appimage'
assert aio.input == '/home/user/Projects/game/'
assert aio.dump_usage == false
assert aio.show_version == false
assert aio.assets == []
assert no_matches == []
}