UNPKG

get-cli-arg

Version:

Get the value of a CLI argument

45 lines (44 loc) 1.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const process_1 = require("process"); const normalify_1 = __importDefault(require("normalify")); /** * Get the value of a CLI argument that is provided via: * --arg * --arg=value * --no-arg * --no-arg=value * @param name the name of the argument to find * @param args the list of the arguments to search, defaults to `process.argv` * @returns the result typecasted to whatever format the value represented */ function default_1(name, args = process_1.argv) { for (const arg of args) { // match const regexp = new RegExp(`^--(no-)?${name}(?:[=\\s](.*))?$`, 'i'); const match = arg.match(regexp); // not found in this argument, continue to next argument if (!match) continue; // extract const invert = Boolean(match[1]); const raw = match[2]; // process value let value; if (typeof raw === 'undefined') { value = true; } else if (raw === '') { value = false; } else { value = (0, normalify_1.default)(raw); } // return the value, and invert it if necessary return invert ? !value : value; } } exports.default = default_1;