mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
cli args support optional types
This commit is contained in:
@ -150,7 +150,7 @@ pub fn create(alloc: Allocator, loop: libuv.Loop, config: *const Config) !*Windo
|
||||
errdefer pty.deinit();
|
||||
|
||||
// Create our child process
|
||||
const path = (try Command.expandPath(alloc, "sh")) orelse
|
||||
const path = (try Command.expandPath(alloc, config.command orelse "sh")) orelse
|
||||
return error.CommandNotFound;
|
||||
defer alloc.free(path);
|
||||
|
||||
|
@ -42,7 +42,13 @@ fn parseIntoField(comptime T: type, dst: *T, key: []const u8, value: ?[]const u8
|
||||
inline for (info.Struct.fields) |field| {
|
||||
if (mem.eql(u8, field.name, key)) {
|
||||
@field(dst, field.name) = field: {
|
||||
const Field = field.field_type;
|
||||
// For optional fields, we just treat it as the child type.
|
||||
// This lets optional fields default to null but get set by
|
||||
// the CLI.
|
||||
const Field = switch (@typeInfo(field.field_type)) {
|
||||
.Optional => |opt| opt.child,
|
||||
else => field.field_type,
|
||||
};
|
||||
const fieldInfo = @typeInfo(Field);
|
||||
|
||||
// If the type implements a parse function, call that.
|
||||
@ -137,6 +143,18 @@ test "parseIntoField: bool" {
|
||||
try testing.expectEqual(false, data.a);
|
||||
}
|
||||
|
||||
test "parseIntoField: optional field" {
|
||||
const testing = std.testing;
|
||||
|
||||
var data: struct {
|
||||
a: ?bool = null,
|
||||
} = .{};
|
||||
|
||||
// True
|
||||
try parseIntoField(@TypeOf(data), &data, "a", "1");
|
||||
try testing.expectEqual(true, data.a.?);
|
||||
}
|
||||
|
||||
test "parseIntoField: struct with parse func" {
|
||||
const testing = std.testing;
|
||||
|
||||
|
@ -6,6 +6,10 @@ pub const Config = struct {
|
||||
|
||||
/// Foreground color for the window.
|
||||
foreground: Color = .{ .r = 0xFF, .g = 0xA5, .b = 0 },
|
||||
|
||||
/// The command to run, usually a shell. If this is not an absolute path,
|
||||
/// it'll be looked up in the PATH.
|
||||
command: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
/// Color represents a color using RGB.
|
||||
|
Reference in New Issue
Block a user