Skip to content

Commit

Permalink
Updated Empty Value Behavior
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
00JCIV00 committed Dec 5, 2024
1 parent 8f12e48 commit a15be5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions examples/covademo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 18 additions & 1 deletion src/Command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a15be5b

Please sign in to comment.