#is this function lowkey okay?

1 messages · Page 1 of 1 (latest)

rapid nymph
#
pub fn parseCommand(comptime E: type, io: std.Io, args: *std.process.Args.Iterator, usage_message: []const u8) ?E {
    const arg = args.next() orelse {
        std.Io.File.stderr().writeStreamingAll(io, usage_message) catch {};
        return null;
    };
    return std.meta.stringToEnum(E, arg) orelse {
        std.Io.File.stderr().writeStreamingAll(io, usage_message) catch {};
        return null;
    };
}

I'm mostly concerned about the orelse expressions, doing catch {} seems odd, and returning null is also odd.

#

Could do a return type of !?E and just return the writeStreamingAll errors, but not sure.

pseudo ledge
#

The orelses and returning null seem fine to me

#

Catching and ignoring the error of writeStreamingAll to stderr is a matter of if you want to treat failing to write the usage message as a failure of the program