Skip to content

Commit

Permalink
Improve invalid keybind error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
otherJL0 committed Dec 31, 2024
1 parent ecfca17 commit 670ee41
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/cli/args.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const diags = @import("diagnostics.zig");
const internal_os = @import("../os/main.zig");
const Diagnostic = diags.Diagnostic;
const DiagnosticList = diags.DiagnosticList;
const binding = @import("../input/Binding.zig");

// TODO:
// - Only `--long=value` format is accepted. Do we want to allow
Expand Down Expand Up @@ -126,14 +127,15 @@ pub fn parse(

// The error set is dependent on comptime T, so we always add
// an extra error so we can have the "else" below.
const ErrSet = @TypeOf(err) || error{ Unknown, OutOfMemory };
const ErrSet = @TypeOf(err) || error{ Unknown, OutOfMemory } || binding.Error;
const message: [:0]const u8 = switch (@as(ErrSet, @errorCast(err))) {
// OOM is not recoverable since we need to allocate to
// track more error messages.
error.OutOfMemory => return err,
error.InvalidField => "unknown field",
error.ValueRequired => formatValueRequired(T, arena_alloc, key) catch "value required",
error.InvalidValue => formatInvalidValue(T, arena_alloc, key, value) catch "invalid value",
error.InvalidFormat, error.InvalidAction => try formatInvalidKeybind(arena_alloc, err == error.InvalidFormat, value.?),
else => try std.fmt.allocPrintZ(
arena_alloc,
"unknown error {}",
Expand Down Expand Up @@ -180,6 +182,28 @@ fn formatInvalidValue(
return buf.items[0 .. buf.items.len - 1 :0];
}

fn formatInvalidKeybind(
arena_alloc: std.mem.Allocator,
isInvalidFormat: bool,
value: []const u8,
) std.mem.Allocator.Error![:0]const u8 {
var buf = std.ArrayList(u8).init(arena_alloc);
errdefer buf.deinit();
const writer = buf.writer();

const splitIdx = std.mem.indexOf(u8, value, "=");
try writer.print("invalid value \"{s}\"", .{value});
const invalid = if (splitIdx) |idx|
// If there is an `=` delimiter,
if (isInvalidFormat) value[0..idx] else value[idx + 1 ..]
else
"Missing action";
const errorName = if (isInvalidFormat) "InvalidFormat" else "InvalidAction";
try writer.print(", {s}: {s}", .{ errorName, invalid });
try writer.writeByte(0);
return buf.items[0 .. buf.items.len - 1 :0];
}

fn formatValues(comptime T: type, key: []const u8, writer: anytype) std.mem.Allocator.Error!void {
const typeinfo = @typeInfo(T);
inline for (typeinfo.Struct.fields) |f| {
Expand Down

0 comments on commit 670ee41

Please sign in to comment.