Skip to content

Lightpanda: the headless browser designed for AI and automation

Merge branch 'main' into shell-completion

Adrià Arrufatd2296393af76
diff --cc src/cli.zig
index f89369f7d,881644427..2bd03e566
--- a/src/cli.zig
+++ b/src/cli.zig
@@@ -205,37 -200,26 +205,57 @@@ pub fn tagNames(comptime E: type) []con
};
}
+pub const Completion = struct {
+ pub const Command = struct {
+ name: []const u8,
+ positional: ?Positional,
+ flags: []const Flag,
+ };
+
+ pub const Positional = struct {
+ name: []const u8,
+ values: Values,
+ multiple: bool,
+ };
+
+ pub const Flag = struct {
+ /// Long form, kebab-case, with the leading `--`.
+ name: []const u8,
+ short: ?u8 = null,
+ values: Values,
+ };
+
+ pub const Values = union(enum) {
+ /// A switch; takes no argument.
+ none,
+ any,
+ path,
+ one_of: []const []const u8,
+ /// A comma-separated list of these (packed-struct options).
+ list_of: []const []const u8,
+ };
+};
+
+ /// No command or choice has a `.`, `/` or `:`, so `markdown.com` is a url
+ /// however close it is to `markdown`.
+ pub fn isUrlLike(arg: []const u8) bool {
+ return std.mem.indexOfAny(u8, arg, ".:/") != null;
+ }
+
+ /// `prefix` was stripped from `value` before matching, like `--log-filter`'s sign.
+ pub fn invalidChoice(arg: []const u8, prefix: []const u8, value: []const u8, choices: []const []const u8) error{InvalidArgument} {
+ var value_buf: [128]u8 = undefined;
+ const typed = std.fmt.bufPrint(&value_buf, "{s}{s}", .{ prefix, value }) catch value;
+ if (string.closest(value, choices)) |near| {
+ var near_buf: [128]u8 = undefined;
+ const suggestion = std.fmt.bufPrint(&near_buf, "{s}{s}", .{ prefix, near }) catch near;
+ log.fatal(.app, "invalid option choice", .{ .arg = arg, .value = log.red(typed), .did_you_mean = log.green(suggestion) });
+ } else {
+ log.fatal(.app, "invalid option choice", .{ .arg = arg, .value = log.red(typed) });
+ }
+ return error.InvalidArgument;
+ }
+
pub fn Builder(comptime commands: anytype) type {
return struct {
const Self = @This();
@@@ -643,18 -569,6 +662,10 @@@
};
}
+ fn parseEnum(comptime E: type, arg: []const u8, str: []const u8) error{InvalidArgument}!E {
- return std.meta.stringToEnum(E, str) orelse {
- const value = log.red(str);
- if (string.closest(str, tagNames(E), 2)) |near| {
- log.fatal(.app, "invalid option choice", .{ .arg = arg, .value = value, .did_you_mean = log.green(near) });
- } else {
- log.fatal(.app, "invalid option choice", .{ .arg = arg, .value = value });
- }
- return error.InvalidArgument;
- };
++ return std.meta.stringToEnum(E, str) orelse invalidChoice(arg, "", str, tagNames(E));
+ }
+
fn parseValue(
allocator: Allocator,
args: *std.process.Args.Iterator,
@@@ -897,15 -829,9 +926,15 @@@
// Encountered an option we don't know of.
if (std.mem.startsWith(u8, option_name, "--")) {
- const names = comptime optionNames(options) ++ &[_][]const u8{"--help"};
+ // Short aliases are left out: a one-letter candidate sits
+ // within two edits of nearly any typo.
+ const names = comptime blk: {
+ var names: []const []const u8 = &.{"--help"};
+ for (commandFlags(command)) |flag| names = names ++ &[_][]const u8{flag.name};
+ break :blk names;
+ };
const arg = log.red(option_name);
- if (string.closest(option_name, names, 2)) |near| {
+ if (string.closest(option_name, names)) |near| {
log.fatal(.app, "unknown argument", .{ .mode = command.name, .arg = arg, .did_you_mean = log.green(near) });
} else {
log.fatal(.app, "unknown argument", .{ .mode = command.name, .arg = arg });