From a15be5b597470cef652407808b6be793fb15dca3 Mon Sep 17 00:00:00 2001 From: 00JCIV00 Date: Thu, 5 Dec 2024 16:10:09 -0500 Subject: [PATCH] Updated Empty Value Behavior - Changed the `setEmpty()` method of Values to no longer erases a Value's default value. - Updated the GetConfig in Command to allow library users to specify a status filter for the underlying Value. - Updated `getVals()` to include these new features. --- examples/covademo.zig | 4 ++-- src/Command.zig | 19 ++++++++++++++++++- src/Value.zig | 1 - 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/examples/covademo.zig b/examples/covademo.zig index ee20937..39545a1 100644 --- a/examples/covademo.zig +++ b/examples/covademo.zig @@ -545,7 +545,7 @@ pub const setup_cmd: CommandT = .{ .description = "A boolean value for the command.", .set_behavior = .Multi, .max_entries = 10, - .parse_fn = Value.ParsingFns.Builder.altBool(&.{ "true", "t", "yes", "y" }, &.{ "false", "f", "no", "n", "0" }, .Error), + .parse_fn = Value.ParsingFns.Builder.altBool(&.{ "true", "t", "yes", "y", "1" }, &.{ "false", "f", "no", "n", "0" }, .Error), }), ValueT.ofType(u64, .{ .name = "cmd_u64", @@ -629,7 +629,7 @@ pub fn main() !void { ); var main_opts = try main_cmd.getOpts(.{}); if (main_opts.get("string_opt")) |str_opt| strOpt: { - if (str_opt.val.isEmpty()) { + if (str_opt.val.isEmpty() and str_opt.val.isSet()) { log.debug("This Option was set, but intentionally left empty.", .{}); break :strOpt; } diff --git a/src/Command.zig b/src/Command.zig index 0f6dbf4..910d767 100644 --- a/src/Command.zig +++ b/src/Command.zig @@ -419,6 +419,14 @@ pub fn Custom(comptime config: Config) type { pub const GetConfig = struct{ /// An optional Argument Group to filter the returned Options or Values. arg_group: ?[]const u8 = null, + /// A filter for the status of an Option or Value + status: StatusFilter = .set_or_default, + + pub const StatusFilter = enum { + set, + set_or_default, + all, + }; }; /// Argument Groups Types for `checkGroup()`. @@ -492,7 +500,11 @@ pub fn Custom(comptime config: Config) type { const opt_group = opt.opt_group orelse continue; if (!mem.eql(u8, conf_group, opt_group)) continue; } - if (!opt.val.isSet() and !opt.val.hasDefault()) continue; + switch (get_config.status) { + .set => if (!opt.val.isSet()) continue, + .set_or_default => if (!opt.val.isSet() and !opt.val.hasDefault()) continue, + else => {}, + } try map.put(opt.name, opt); } return map; @@ -632,6 +644,11 @@ pub fn Custom(comptime config: Config) type { const val_group = val.valGroup() orelse continue; if (!mem.eql(u8, conf_group, val_group)) continue; } + switch (get_config.status) { + .set => if (!val.isSet()) continue, + .set_or_default => if (!val.isSet() and !val.hasDefault()) continue, + else => {}, + } try map.put(val.name(), val); } return map; diff --git a/src/Value.zig b/src/Value.zig index 89fb8c7..29f8e1b 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -350,7 +350,6 @@ pub fn Typed(comptime SetT: type, comptime config: Config) type { /// This is intended to be used with Options. pub fn setEmpty(self: *const @This()) !void { if (!self.is_empty) return error.NotEmpty; - if (@constCast(self).default_val) |_| @constCast(self).default_val = null; @constCast(self).is_set = true; }