-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed the `ofType()` function of `Value.Custom` to properly handle Enums by assigning a parsing function. - Created the new `asEnumTag()` Parsing Function Builder for Values. - Created the `log_enum.zig` example.
- Loading branch information
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const std = @import("std"); | ||
const log = std.log; | ||
const cova = @import("cova"); | ||
|
||
// We need to add any Enums we're using to our Value Config. | ||
const CommandT = cova.Command.Custom(.{ | ||
.val_config = .{ | ||
.custom_types = &.{ log.Level }, | ||
}, | ||
}); | ||
const setup_cmd = CommandT{ | ||
.name = "log_enum", | ||
.description = "A small demo of using the Log Level Enum as an Option.", | ||
.opts = &.{ | ||
.{ | ||
.name = "log_level", | ||
.description = "An Option using the `log.Level` Enum.", | ||
.long_name = "log-level", | ||
.mandatory = true, | ||
.val = CommandT.ValueT.ofType(log.Level, .{ | ||
.name = "log_level_val", | ||
.description = " This Value will handle then Enum." | ||
}) | ||
} | ||
}, | ||
}; | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const alloc = gpa.allocator(); | ||
defer if (gpa.deinit() != .ok and gpa.detectLeaks()) log.err("Memory leak detected!", .{}); | ||
const stdout = std.io.getStdOut().writer(); | ||
|
||
var main_cmd = try setup_cmd.init(alloc, .{}); | ||
defer main_cmd.deinit(); | ||
var args_iter = try cova.ArgIteratorGeneric.init(alloc); | ||
defer args_iter.deinit(); | ||
|
||
cova.parseArgs(&args_iter, CommandT, main_cmd, stdout, .{}) catch |err| switch (err) { | ||
error.UsageHelpCalled => {}, | ||
else => return err, | ||
}; | ||
|
||
const main_opts = try main_cmd.getOpts(.{}); | ||
const log_lvl_opt = main_opts.get("log_level").?; | ||
const log_lvl = log_lvl_opt.val.getAs(log.Level) catch { | ||
log.err("The provided Log Level was invalid.", .{}); | ||
return; | ||
}; | ||
log.info("Provided Log Level: {s}", .{ @tagName(log_lvl) }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters