export function parseArgs(argv) {
    const [command, ...rest] = argv;
    const positionals = [];
    const flags = {};
    const passthrough = [];
    let pass = false;
    for (let index = 0; index < rest.length; index += 1) {
        const arg = rest[index];
        if (pass) {
            passthrough.push(arg);
            continue;
        }
        if (arg === "--") {
            pass = true;
            continue;
        }
        if (arg.startsWith("--")) {
            const hit = arg.match(/^--([^=]+)=(.*)$/);
            if (hit) {
                flags[toCamelCase(hit[1])] = hit[2];
                continue;
            }
            const name = arg.slice(2);
            const next = rest[index + 1];
            if (next && !next.startsWith("-")) {
                flags[toCamelCase(name)] = next;
                index += 1;
            }
            else {
                flags[toCamelCase(name)] = true;
            }
            continue;
        }
        if (arg.startsWith("-") && arg.length > 1) {
            const name = shortFlagName(arg);
            if (!name) {
                positionals.push(arg);
                continue;
            }
            const next = rest[index + 1];
            if (next && !next.startsWith("-")) {
                flags[name] = next;
                index += 1;
            }
            else {
                flags[name] = true;
            }
            continue;
        }
        positionals.push(arg);
    }
    return { command, positionals, flags, passthrough };
}
export function stringFlag(flags, name) {
    const value = flags[name];
    return typeof value === "string" && value.length > 0 ? value : undefined;
}
export function booleanFlag(flags, name) {
    return flags[name] === true || flags[name] === "true" || flags[name] === "yes";
}
function toCamelCase(value) {
    return value.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
}
function shortFlagName(value) {
    const chars = value.slice(1);
    if (chars.length !== 1)
        return;
    return {
        a: "agent",
        d: "directory",
        h: "help",
        m: "model",
        v: "version",
    }[chars];
}
//# sourceMappingURL=args.js.map