I am writing a CLI argument parser, hopefully in comptime and am wondering how I can create a struct based on another struct at comptime.
The standard library already has the perfect struct for what I want, I just want to be able to give it a type at compile time to be able to parse the values to that type later (and generate structs with the correct types).
pub const StructField = struct {
name: [:0]const u8,
type: type,
/// The type of the default value is the type of this struct field, which
/// is the value of the `type` field in this struct. However there is no
/// way to refer to that type here, so we use `*const anyopaque`.
/// See also: `defaultValue`.
default_value_ptr: ?*const anyopaque,
is_comptime: bool,
alignment: comptime_int,
/// Loads the field's default value from `default_value_ptr`.
/// Returns `null` if the field has no default value.
pub inline fn defaultValue(comptime sf: StructField) ?sf.type {
const dp: *const sf.type = @ptrCast(@alignCast(sf.default_value_ptr orelse return null));
return dp.*;
}
};
I'd like to get something similar to thisStructField but allowing me to set a type T to use as the type to parse the CLI argument to later