Files
ghostty/src/cli/validate_config.zig
Mitchell Hashimoto c90ed29341 cli: skip argv0 and actions when parsing CLI flags
This fixes a regression from #2454. In that PR, we added an error when
positional arguments are detected. I believe that's correct, but we
were silently relying on the previous behavior in the CLI commands.

This commit changes the CLI commands to use a new argsIterator function
that creates an iterator that skips the first argument (argv0). This is
the same behavior that the config parsing does and now uses this shared
logic.

This also makes it so the argsIterator ignores actions (`+things`)
and we document that we expect those to be handled earlier.
2024-10-18 12:59:16 -07:00

72 lines
2.0 KiB
Zig

const std = @import("std");
const Allocator = std.mem.Allocator;
const args = @import("args.zig");
const Action = @import("action.zig").Action;
const Config = @import("../config.zig").Config;
const cli = @import("../cli.zig");
pub const Options = struct {
/// The path of the config file to validate. If this isn't specified,
/// then the default config file paths will be validated.
@"config-file": ?[:0]const u8 = null,
pub fn deinit(self: Options) void {
_ = self;
}
/// Enables "-h" and "--help" to work.
pub fn help(self: Options) !void {
_ = self;
return Action.help_error;
}
};
/// The `validate-config` command is used to validate a Ghostty config file.
///
/// When executed without any arguments, this will load the config from the default location.
///
/// The `--config-file` argument can be passed to validate a specific target config
/// file in a non-default location.
pub fn run(alloc: std.mem.Allocator) !u8 {
var opts: Options = .{};
defer opts.deinit();
{
var iter = try args.argsIterator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
}
const stdout = std.io.getStdOut().writer();
var cfg = try Config.default(alloc);
defer cfg.deinit();
// If a config path is passed, validate it, otherwise validate default configs
if (opts.@"config-file") |config_path| {
var buf: [std.fs.max_path_bytes]u8 = undefined;
const abs_path = try std.fs.cwd().realpath(config_path, &buf);
try cfg.loadFile(alloc, abs_path);
try cfg.loadRecursiveFiles(alloc);
} else {
cfg = try Config.load(alloc);
}
try cfg.finalize();
if (cfg._diagnostics.items().len > 0) {
var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit();
for (cfg._diagnostics.items()) |diag| {
try diag.write(buf.writer());
try stdout.print("{s}\n", .{buf.items});
buf.clearRetainingCapacity();
}
return 1;
}
return 0;
}