UNPKG

argparse-ts

Version:

Modern CLI arguments parser for node.js

197 lines 6.71 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseArgsArray = parseArgsArray; exports.formatArgNameWithAlias = formatArgNameWithAlias; exports.buildArgExtraConfig = buildArgExtraConfig; /** * Parses an array of command-line arguments into positional and optional arguments. * * @param argv - The array of command-line arguments. * @returns A tuple containing an array of positional arguments and a record of optional arguments. * * @category Utils * * @example * ``` * const argv = ['make', '--flag', '-v', 'a', 'b', 'c']; * const [positional, optional] = parseArgsArray(argv); * console.log(positional); // ['make'] * console.log(optional); // { '--flag': [], '-v': ['a', 'b', 'c'] } * ``` */ function parseArgsArray(argv) { // Find the index of the first argument that starts with a dash. var foundIndex = argv.findIndex(function (x) { return isOptionalArgGiven(x); }); // If no such argument is found, set the index to the length of the array. var optionalBegin = foundIndex !== -1 ? foundIndex : argv.length; // The positional arguments are the arguments that come before the first option. var positional = __spreadArray([], __read(argv.slice(0, optionalBegin)), false); // Build the buffer of optional arguments. var optionalBuffer = __spreadArray([], __read(argv.slice(optionalBegin)), false).reverse(); // Store the parsed optional arguments in this record. var optional = {}; // The current argument name and values. var argName = undefined; var argValues = []; // Iterate over the buffer of optional arguments. while (optionalBuffer.length > 0) { var item = optionalBuffer.pop(); // If the item includes glued together aliases, expand it. if (item.match(/^-[a-zA-Z]{2,}$/)) { // Split the alias into individual characters and reverse the order. var items = item.slice(1).split('').reverse(); // Add the expanded alias arguments to the buffer. optionalBuffer.push.apply(optionalBuffer, __spreadArray([], __read(items.map(function (x) { return "-".concat(x); })), false)); continue; } // If the item is a new argument, store the previous argument if it exists. if (isOptionalArgGiven(item)) { if (argName !== undefined) { // Store the previous argument. optional[argName] = argValues; } // The current argument name is the item. argName = item; // Reset the current argument values. argValues = []; } else { // If the item is a value of the current argument, add it to the values. argValues.push(item); } } // Store the last argument. if (argName !== undefined) { optional[argName] = argValues; } return [positional, optional]; } /** * Formats the argument name with alias. * * @param argConfig - The argument configuration. * * @returns The formatted argument name. * * @category Utils */ function formatArgNameWithAlias(argConfig) { return "".concat(argConfig.name).concat(argConfig.alias ? " (".concat(argConfig.alias, ")") : ''); } /** * Builds the extra configuration for an argument. * * @param config - The argument configuration. * * @returns The extra configuration. * * @category Utils */ function buildArgExtraConfig(config) { var positional = isArgPositional(config); var multiple = isArgMultiple(config); var required = isArgRequired(config); var allowEmpty = isArgAllowEmpty(config); var valuesCount = typeof config.nargs === 'number' ? config.nargs : undefined; var minValuesCount = valuesCount !== null && valuesCount !== void 0 ? valuesCount : (allowEmpty ? 0 : 1); return { positional: positional, multiple: multiple, required: required, allowEmpty: allowEmpty, valuesCount: valuesCount, minValuesCount: minValuesCount }; } /** * Determines if the argument is positional. * * @param config - The argument configuration. * * @returns True if the argument is positional, otherwise false. * * @category Utils */ function isArgPositional(config) { return !config.name.startsWith('--'); } /** * Determines if the argument can accept multiple values. * * @param config - The argument configuration. * * @returns True if the argument can accept multiple values, otherwise false. * * @category Utils */ function isArgMultiple(config) { return config.nargs === '*' || config.nargs === '+' || typeof config.nargs === 'number'; } /** * Determines if the argument is required. * * @param config - The argument configuration. * * @returns True if the argument is required, otherwise false. * * @category Utils */ function isArgRequired(config) { if (config.required) { return true; } if (!isArgPositional(config) && config.nargs === undefined) { return false; } return config.nargs !== '*' && config.nargs !== '?' && config.default === undefined; } /** * Determines if the argument allows an empty value. * * @param config - The argument configuration. * * @returns True if the argument allows an empty value, otherwise false. * * @category Utils */ function isArgAllowEmpty(config) { return config.nargs === '*' || config.nargs === '?' || config.const !== undefined || (isArgPositional(config) && !isArgRequired(config)); } /** * Determines if the argument is optional. * * @param argName - The argument name. * * @returns True if the argument is optional, otherwise false. * * @category Utils */ function isOptionalArgGiven(argName) { return argName.startsWith('-') && isNaN(Number(argName)); } //# sourceMappingURL=utils.js.map