ghostty/src/cli/list_actions.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

54 lines
1.5 KiB
Zig

const std = @import("std");
const args = @import("args.zig");
const Action = @import("action.zig").Action;
const Allocator = std.mem.Allocator;
const help_strings = @import("help_strings");
pub const Options = struct {
/// If `true`, print out documentation about the action associated with the
/// keybinds.
docs: bool = false,
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 `list-actions` command is used to list all the available keybind actions
/// for Ghostty.
///
/// The `--docs` argument will print out the documentation for each action.
pub fn run(alloc: 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();
const info = @typeInfo(help_strings.KeybindAction);
inline for (info.Struct.decls) |field| {
try stdout.print("{s}", .{field.name});
if (opts.docs) {
try stdout.print(":\n", .{});
var iter = std.mem.splitScalar(u8, std.mem.trimRight(u8, @field(help_strings.KeybindAction, field.name), &std.ascii.whitespace), '\n');
while (iter.next()) |line| {
try stdout.print(" {s}\n", .{line});
}
} else {
try stdout.print("\n", .{});
}
}
return 0;
}