aboutsummaryrefslogtreecommitdiff
path: root/core/flags/validation.odin
diff options
context:
space:
mode:
Diffstat (limited to 'core/flags/validation.odin')
-rw-r--r--core/flags/validation.odin45
1 files changed, 45 insertions, 0 deletions
diff --git a/core/flags/validation.odin b/core/flags/validation.odin
new file mode 100644
index 000000000..c5cad8c7a
--- /dev/null
+++ b/core/flags/validation.odin
@@ -0,0 +1,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
+}