Spaces:
Runtime error
Runtime error
File size: 2,134 Bytes
a6b96c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | "use strict";
/**
* Command Argument Projection Module (ADR-457 build-at-publish: the
* hand-written bin/lib/command-arg-projection.cjs collapsed to a TypeScript
* source of truth). Behaviour is preserved byte-for-behaviour from the prior
* hand-written .cjs; only types are added.
*
* Shared helpers for command-family adapters to project argv tokens into
* typed named values and multi-word segments.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseNamedArgs = parseNamedArgs;
exports.parseMultiwordArg = parseMultiwordArg;
/**
* Extract named --flag <value> pairs from an args array.
* Returns an object mapping flag names to their values (null if absent).
* Flags listed in `booleanFlags` are treated as booleans.
*/
function parseNamedArgs(args, valueFlags = [], booleanFlags = []) {
// Index each token's first position once (firstIndex.get(t) ?? -1 === args.indexOf(t),
// firstIndex.has(t) === args.includes(t)) so the flag loops below don't each re-scan
// argv — O(argv + flags) instead of O(flags * argv). Semantics are unchanged. (#312)
const firstIndex = new Map();
for (let i = 0; i < args.length; i++) {
if (!firstIndex.has(args[i]))
firstIndex.set(args[i], i);
}
const result = {};
for (const flag of valueFlags) {
const idx = firstIndex.has(`--${flag}`) ? firstIndex.get(`--${flag}`) : -1;
result[flag] =
idx !== -1 && args[idx + 1] !== undefined && !args[idx + 1].startsWith('--')
? args[idx + 1]
: null;
}
for (const flag of booleanFlags) {
result[flag] = firstIndex.has(`--${flag}`);
}
return result;
}
/**
* Collect all tokens after --flag until the next --flag or end of args.
*/
function parseMultiwordArg(args, flag) {
const idx = args.indexOf(`--${flag}`);
if (idx === -1)
return null;
const tokens = [];
for (let i = idx + 1; i < args.length; i++) {
if (args[i].startsWith('--'))
break;
tokens.push(args[i]);
}
return tokens.length > 0 ? tokens.join(' ') : null;
}
|