← Files RivetARCHIVED FILE

node_modules/rivet-design/dist/cli/flags.js

2.18 KB · Nov 7, 2026 · 13:16 UTC

↓ Download file

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.integerFlag = exports.booleanFlag = exports.stringFlag = exports.parseFlags = void 0;
const parseFlags = (args, spec) => {
    const values = {};
    const positionals = [];
    const errors = [];
    for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (!arg.startsWith('--')) {
            positionals.push(arg);
            continue;
        }
        const equalsIndex = arg.indexOf('=');
        const name = equalsIndex === -1 ? arg.slice(2) : arg.slice(2, equalsIndex);
        const inlineValue = equalsIndex === -1 ? undefined : arg.slice(equalsIndex + 1);
        const definition = spec[name];
        if (!definition) {
            errors.push(`Unknown flag --${name}`);
            continue;
        }
        if (!definition.takesValue) {
            if (inlineValue !== undefined) {
                errors.push(`Flag --${name} does not take a value`);
                continue;
            }
            values[name] = true;
            continue;
        }
        if (inlineValue !== undefined) {
            values[name] = inlineValue;
            continue;
        }
        const next = args[i + 1];
        if (next === undefined || next.startsWith('--')) {
            errors.push(`Flag --${name} requires a value`);
            continue;
        }
        values[name] = next;
        i++;
    }
    return { values, positionals, errors };
};
exports.parseFlags = parseFlags;
const stringFlag = (parsed, name) => {
    const value = parsed.values[name];
    return typeof value === 'string' ? value : undefined;
};
exports.stringFlag = stringFlag;
const booleanFlag = (parsed, name) => parsed.values[name] === true;
exports.booleanFlag = booleanFlag;
const integerFlag = (parsed, name) => {
    const raw = (0, exports.stringFlag)(parsed, name);
    if (raw === undefined) {
        return undefined;
    }
    const value = Number.parseInt(raw, 10);
    if (!Number.isInteger(value) || value <= 0) {
        parsed.errors.push(`Flag --${name} must be a positive integer`);
        return undefined;
    }
    return value;
};
exports.integerFlag = integerFlag;
//# sourceMappingURL=flags.js.map

SHA-256: cce0355faf223b45af3d8132cf08cb1ab5f09cef1d99970b6e5b9e0bd4084a5a