all: update to zig 0.11.0-pre syntax

For loop syntax changed. Also `orelse` apparently no longer works with
non-optional types, which is reasonable but annoying. The path of
least resistance is to make the flag default type optional to mirror
options/arguments.
This commit is contained in:
torque 2023-03-20 23:02:13 -07:00
parent 0afad6b585
commit 9480d23f2d
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
3 changed files with 18 additions and 13 deletions

View File

@ -21,7 +21,7 @@ pub fn UpdateDefaults(comptime input: type, comptime defaults: anytype) type {
}; };
var fields: [fieldcount]StructField = undefined; var fields: [fieldcount]StructField = undefined;
for (inputInfo.Struct.fields) |field, idx| { for (inputInfo.Struct.fields, 0..) |field, idx| {
fields[idx] = .{ fields[idx] = .{
.name = field.name, .name = field.name,
.field_type = field.field_type, .field_type = field.field_type,
@ -78,11 +78,11 @@ pub const MutableTuple = struct {
pub fn TupleType(comptime self: @This()) type { pub fn TupleType(comptime self: @This()) type {
comptime { comptime {
var fields: [self.types.len]StructField = undefined; var fields: [self.types.len]StructField = undefined;
for (self.types) |Type, idx| { for (self.types, 0..) |Type, idx| {
var num_buf: [128]u8 = undefined; var num_buf: [128]u8 = undefined;
fields[idx] = .{ fields[idx] = .{
.name = std.fmt.bufPrint(&num_buf, "{d}", .{idx}) catch unreachable, .name = std.fmt.bufPrint(&num_buf, "{d}", .{idx}) catch unreachable,
.field_type = Type, .type = Type,
.default_value = null, .default_value = null,
// TODO: is this the right thing to do? // TODO: is this the right thing to do?
.is_comptime = false, .is_comptime = false,

View File

@ -128,7 +128,7 @@ pub fn CommandParser(
return OptionError.UnknownOption; return OptionError.UnknownOption;
} else { } else {
// we have a short flag, which may be multiple fused flags // we have a short flag, which may be multiple fused flags
shortloop: for (arg[1..]) |shorty, idx| { shortloop: for (arg[1..], 0..) |shorty, idx| {
specloop: inline for (spec) |param| { specloop: inline for (spec) |param| {
switch (@TypeOf(param).brand) { switch (@TypeOf(param).brand) {
.Option => { .Option => {
@ -251,13 +251,13 @@ pub fn CommandParser(
} }
} }
fn scryTruthiness(alloc: std.mem.Allocator, input: []const u8) !bool { fn scryTruthiness(input: []const u8) bool {
// empty string is falsy. // empty string is falsy.
if (input.len == 0) return false; if (input.len == 0) return false;
if (input.len <= 5) { if (input.len <= 5) {
const comp = try std.ascii.allocLowerString(alloc, input); var lowerBuf: [5]u8 = undefined;
defer alloc.free(comp); const comp = std.ascii.lowerString(&lowerBuf, input);
inline for ([_][]const u8{ "false", "no", "0" }) |candidate| { inline for ([_][]const u8{ "false", "no", "0" }) |candidate| {
if (std.mem.eql(u8, comp, candidate)) { if (std.mem.eql(u8, comp, candidate)) {
@ -302,7 +302,7 @@ pub fn CommandParser(
.Flag => { .Flag => {
if (param.envVar) |want| { if (param.envVar) |want| {
if (env.get(want)) |value| { if (env.get(want)) |value| {
@field(result, param.name) = try scryTruthiness(alloc, value); @field(result, param.name) = scryTruthiness(value);
} }
} }
}, },
@ -369,7 +369,7 @@ pub fn CommandResult(comptime spec: anytype, comptime UserContext: type) type {
fields[idx] = .{ fields[idx] = .{
.name = param.name, .name = param.name,
.field_type = FieldType, .type = FieldType,
.default_value = @ptrCast(?*const anyopaque, &param.default), .default_value = @ptrCast(?*const anyopaque, &param.default),
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(FieldType), .alignment = @alignOf(FieldType),
@ -417,7 +417,7 @@ fn RequiredTracker(comptime spec: anytype) type {
.Argument, .Option => if (param.required()) { .Argument, .Option => if (param.required()) {
fields[idx] = .{ fields[idx] = .{
.name = param.name, .name = param.name,
.field_type = bool, .type = bool,
.default_value = &false, .default_value = &false,
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(bool), .alignment = @alignOf(bool),

View File

@ -32,7 +32,7 @@ pub fn Option(comptime args: ParameterArgs) type {
comptime var result = struct { comptime var result = struct {
pub const brand: Brand = .Option; pub const brand: Brand = .Option;
pub const mayBeOptional: bool = switch (@typeInfo(args.Output)) { const mayBeOptional: bool = switch (@typeInfo(args.Output)) {
.Optional => true, .Optional => true,
else => false, else => false,
}; };
@ -96,13 +96,18 @@ pub fn Flag(comptime UserContext: type) type {
pub const ContextType: type = UserContext; pub const ContextType: type = UserContext;
name: []const u8, name: []const u8,
default: bool = false, default: ?bool = false,
truthy: ShortLong = .{}, truthy: ShortLong = .{},
falsy: ShortLong = .{}, falsy: ShortLong = .{},
help: ?[]const u8 = null, help: ?[]const u8 = null,
envVar: ?[]const u8 = null, envVar: ?[]const u8 = null,
hideResult: bool = false, hideResult: bool = false,
eager: ?*const fn (UserContext, CommandData) anyerror!void = null, eager: ?*const fn (UserContext, CommandData) anyerror!void = null,
pub fn required(self: @This()) bool {
if (self.default) return true;
return false;
}
}; };
} }
@ -149,7 +154,7 @@ pub fn Argument(comptime args: ParameterArgs) type {
return struct { return struct {
pub const brand: Brand = .Argument; pub const brand: Brand = .Argument;
pub const mayBeOptional: bool = switch (@typeInfo(args.Output)) { const mayBeOptional: bool = switch (@typeInfo(args.Output)) {
.Optional => true, .Optional => true,
else => false, else => false,
}; };