blob: c5cad8c7a08e7c443321d71771becc9475feb7ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package flags
import "core:fmt"
import "core:reflect"
import "core:strconv"
_ :: fmt
_ :: reflect
_ :: strconv
// Validate that all the required arguments are set.
validate :: proc(data: ^$T, max_pos: int, set_args: []string) -> Error {
fields := reflect.struct_fields_zipped(T)
check_fields: for field in fields {
tag := reflect.struct_tag_lookup(field.tag, TAG_ARGS) or_continue
if _, ok := get_struct_subtag(tag, SUBTAG_REQUIRED); ok {
was_set := false
// Check if it was set by name.
check_set_args: for set_arg in set_args {
if get_field_name(field) == set_arg {
was_set = true
break check_set_args
}
}
// Check if it was set by position.
if pos, has_pos := get_struct_subtag(tag, SUBTAG_POS); has_pos {
value, value_ok := strconv.parse_int(pos)
if value < max_pos {
was_set = true
}
}
if !was_set {
return Validation_Error {
fmt.tprintf("required argument `%s` was not set", field.name),
}
}
}
}
return nil
}
|