Skip to content

Commit

Permalink
Add support for optional types
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed Oct 13, 2023
1 parent e98d359 commit 4af9683
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
6 changes: 0 additions & 6 deletions example/simple.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var config = struct {
int: i32 = undefined,
bool: bool = false,
float: f64 = 0.34,
name: []const u8 = undefined,
}{};

var ip_option = cli.Option{
Expand Down Expand Up @@ -37,11 +36,6 @@ var float_option = cli.Option{
.value_ref = cli.mkRef(&config.float),
};

var name_option = cli.Option{
.long_name = "long_name",
.help = "long_name help",
.value_ref = cli.mkRef(&config.name),
};
var app = &cli.App{
.name = "simple",
.description = "This a simple CLI app\nEnjoy!",
Expand Down
38 changes: 21 additions & 17 deletions src/value_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ pub const ValueData = struct {
};

pub fn getValueData(comptime T: type) ValueData {
return switch (@typeInfo(T)) {
.Int => intData(T),
.Float => floatData(T),
const ValueType = switch (@typeInfo(T)) {
.Optional => |oinfo| oinfo.child,
else => T,
};
return switch (@typeInfo(ValueType)) {
.Int => intData(ValueType, T),
.Float => floatData(ValueType, T),
.Bool => boolData(T),
.Pointer => |pinfo| {
if (pinfo.size == .Slice and pinfo.child == u8) {
Expand All @@ -23,52 +27,52 @@ pub fn getValueData(comptime T: type) ValueData {
};
}

fn intData(comptime T: type) ValueData {
fn intData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(T),
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) anyerror!void {
const dt: *T = @ptrCast(@alignCast(dest));
dt.* = try std.fmt.parseInt(T, value, 10);
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = try std.fmt.parseInt(ValueType, value, 10);
}
}.parser,
.type_name = "integer",
};
}

fn floatData(comptime T: type) ValueData {
fn floatData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(T),
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) anyerror!void {
const dt: *T = @ptrCast(@alignCast(dest));
dt.* = try std.fmt.parseFloat(T, value);
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = try std.fmt.parseFloat(ValueType, value);
}
}.parser,
.type_name = "float",
};
}

fn boolData(comptime T: type) ValueData {
fn boolData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(T),
.value_size = @sizeOf(DestinationType),
.is_bool = true,
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) anyerror!void {
const dt: *T = @ptrCast(@alignCast(dest));
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = std.mem.eql(u8, value, "true");
}
}.parser,
.type_name = "bool",
};
}

fn stringData(comptime T: type) ValueData {
fn stringData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(T),
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) anyerror!void {
const dt: *T = @ptrCast(@alignCast(dest));
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = value;
}
}.parser,
Expand Down

0 comments on commit 4af9683

Please sign in to comment.