UNPKG

@bleed-believer/path-alias

Version:
46 lines (45 loc) 1.3 kB
export class Argv { #raw; get raw() { return this.#raw.slice(); } #main; get main() { return this.#main.slice(); } #flags; get flags() { const flagsCopy = new Map(); for (const [key, value] of this.#flags.entries()){ flagsCopy.set(key, value.slice()); } return flagsCopy; } constructor(processInstance){ const argv = processInstance?.argv || process.argv; const args = argv.slice(2); this.#raw = []; this.#main = []; this.#flags = new Map(); let currentFlag = null; for (const arg of args){ this.#raw.push(arg); if (arg.startsWith('--')) { if (currentFlag) { this.#flags.set(currentFlag, []); } currentFlag = arg; } else if (currentFlag) { const values = this.#flags.get(currentFlag) || []; values.push(arg); this.#flags.set(currentFlag, values); currentFlag = null; } else { this.#main.push(arg); } } if (currentFlag && !this.#flags.has(currentFlag)) { this.#flags.set(currentFlag, []); } } }