Skip to content

Commit

Permalink
Allowed Custom Value.Typed Types in Value.Config.custom_types
Browse files Browse the repository at this point in the history
- Allowed Custom `Value.Typed` Types in `Value.Config.custom_types` to make custom Value Types more seamless and configurable.
- Fixed misssing `mem.Allocator` parameter in `Command.Config.usage_fn`.
- #30
  • Loading branch information
00JCIV00 committed Oct 20, 2023
1 parent c8c8551 commit f799141
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
15 changes: 14 additions & 1 deletion examples/covademo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub const CommandT = Command.Custom(.{
}.help
},
.usage_fn = struct{
fn usage(self: anytype, writer: anytype) !void {
fn usage(self: anytype, writer: anytype, alloc: mem.Allocator) !void {
_ = alloc;
// In a real implementation checks should be done to ensure `self` is a suitable Command Type and extract its sub Argument Types.
_ = self;
try writer.print("This is an overriding usage message!\n\n", .{});
Expand Down Expand Up @@ -191,6 +192,18 @@ pub const setup_cmd: CommandT = .{
}),
.description = "An integer option. (Can be given up to 10 times.)",
},
//.{
// .name = "uint_opt",
// .short_name = 'u',
// .long_name = "uint",
// .val = ValueT.ofType(u1024, .{
// .name = "uint_val",
// .description = "An unsigned integer value.",
// .set_behavior = .Multi,
// .max_args = 10,
// }),
// .description = "An unsigned integer option. (Can be given up to 10 times.)",
//},
.{
.name = "float_opt",
.short_name = 'f',
Expand Down
2 changes: 1 addition & 1 deletion src/Command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub const Config = struct {
/// 1. CommandT (This should be the `self` parameter. As such it needs to match the Command Type the function is being called on.)
/// 2. Writer (This is the Writer that will written to.)
/// 3. Allocator (This does not have to be used within in the function, but must be supported in case it's needed.)
usage_fn: ?*const fn(anytype, anytype)anyerror!void = null,
usage_fn: ?*const fn(anytype, anytype, mem.Allocator)anyerror!void = null,

/// Indent string used for Usage/Help formatting.
/// Note, this will be used as the default across all Argument Types,
Expand Down
17 changes: 12 additions & 5 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ pub const Config = struct {
/// This can be overwritten on individual Values using the `Value.Typed.arg_delims` field.
arg_delims: []const u8 = ",;",

/// Custom types for this project's Values.
/// Custom Types for this project's Values. If these Types are `Value.Typed` they'll be passed in directly.
/// Otherwise, each Type will be wrapped into a `Value.Typed`
/// This is useful for adding additional types that aren't covered by the base `Value.Generic` union.
/// Note, any non-numeric (Int, UInt, Float) types will require their own Parse Function to be implemented on the `Value.Typed.parse_fn` field.
/// Note, any non-numeric (Int, UInt, Float) or non-`Value.Typed` Types will require their own Parse Function to be implemented on the `Value.Typed.parse_fn` field.
custom_types: []const type = &.{},

/// Use Custom Bit Width Range for Ints and UInts.
Expand Down Expand Up @@ -353,10 +354,16 @@ pub fn Generic(comptime config: Config) type {
}

for (config.custom_types) |T| {
const AddT = addT: {
for (@typeInfo(T).Struct.fields, @typeInfo(Typed(bool, config){}).Struct.fields) |a_field, b_field| {
if (!mem.eql(u8, a_field.name, b_field.name)) break :addT Typed(T, config);
}
else break :addT T;
};
union_info.fields = union_info.fields ++ .{ UnionField {
.name = @typeName(T),
.type = Typed(T, config),
.alignment = @alignOf(Typed(T, config)),
.name = @typeName(AddT.ChildT),
.type = AddT,
.alignment = @alignOf(AddT),
} };
tag_info.fields = tag_info.fields ++ .{ .{
.name = @typeName(T),
Expand Down

0 comments on commit f799141

Please sign in to comment.