Skip to content
1        10        20        30        40        50        60        70        80        901//! Command-line parsing for `any`.2const std = @import("std");34pub const usage =5    \\any installs the silen-infra skill into the current repository.6    \\7    \\usage:8    \\  any                    Create or append .opencode/skills/silen-infra/SKILL.md9    \\  any skill              Same as `any`10    \\  any skill --print      Print the embedded skill to stdout11    \\  any skill --path <p>   Write to a specific file instead12    \\  any skill --force      Replace an existing different file13    \\  any skill --check      Exit 1 unless the skill is installed14    \\  any --version          Print the version15    \\  any --help             Print this help16    \\17;1819pub const Install = struct {20    path: ?[]const u8 = null,21    force: bool = false,22    check: bool = false,23};2425pub const Action = union(enum) {26    help,27    version,28    print,29    install: Install,30};3132pub const ParseError = error{Usage};3334threadlocal var message: [512]u8 = @splat(0);3536/// The message from the most recent parse failure on this thread.37pub fn lastErrorMessage() []const u8 {38    return std.mem.sliceTo(&message, 0);39}4041fn fail(comptime format: []const u8, args: anytype) ParseError {42    const text = std.fmt.bufPrint(&message, format, args) catch "invalid arguments";43    message[@min(text.len, message.len - 1)] = 0;44    return error.Usage;45}4647fn is(arg: []const u8, short: []const u8, long: []const u8) bool {48    return std.mem.eql(u8, arg, short) or std.mem.eql(u8, arg, long);49}5051/// Parses arguments after the program name.52pub fn parse(args: []const [:0]const u8) ParseError!Action {53    var install: Install = .{};54    var action: ?Action = null;5556    var index: usize = 0;57    while (index < args.len) : (index += 1) {58        const arg = args[index];5960        if (is(arg, "-h", "--help")) return .help;61        if (is(arg, "-V", "--version")) return .version;62        if (is(arg, "-n", "--print")) {63            action = .print;64            continue;65        }66        if (is(arg, "-f", "--force")) {67            install.force = true;68            continue;69        }70        if (is(arg, "-c", "--check")) {71            install.check = true;72            continue;73        }74        if (is(arg, "-p", "--path")) {75            index += 1;76            if (index >= args.len) return fail("--path needs a value", .{});77            install.path = args[index];78            continue;79        }80        if (std.mem.eql(u8, arg, "skill")) continue;81        if (std.mem.eql(u8, arg, "--")) {82            if (index + 1 < args.len) return fail("unexpected argument `{s}`", .{args[index + 1]});83            continue;84        }85        return fail("unexpected argument `{s}`", .{arg});86    }8788    if (action) |chosen| return chosen;89    return .{ .install = install };90}9192fn expectInstall(action: Action) Install {93    return switch (action) {94        .install => |install| install,95        else => std.debug.panic("expected an install action", .{}),96    };97}9899test "no arguments installs" {100    const install = expectInstall(try parse(&.{}));101    try std.testing.expect(install.path == null);102    try std.testing.expect(!install.force);103}104105test "skill subcommand and flags" {106    const install = expectInstall(try parse(&.{ "skill", "--force", "--path", "docs/SKILL.md" }));107    try std.testing.expect(install.force);108    try std.testing.expectEqualStrings("docs/SKILL.md", install.path.?);109}110111test "print and check" {112    try std.testing.expect(std.meta.activeTag(try parse(&.{ "skill", "--print" })) == .print);113114    const install = expectInstall(try parse(&.{ "skill", "--check" }));115    try std.testing.expect(install.check);116}117118test "unknown argument is a usage error" {119    try std.testing.expectError(error.Usage, parse(&.{"install"}));120    try std.testing.expect(std.mem.indexOf(u8, lastErrorMessage(), "unexpected argument") != null);121}122123test "path needs a value" {124    try std.testing.expectError(error.Usage, parse(&.{"--path"}));125}