UNPKG

argparse-ts

Version:

Modern CLI arguments parser for node.js

186 lines 6.37 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)); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatArgHelp = formatArgHelp; exports.convertToTable = convertToTable; exports.tabTableRows = tabTableRows; /** * Generates a help message for an argument. * * @param argConfig - The argument configuration. * * @returns A 2D array of strings representing the help message. * * @category Utils */ function formatArgHelp(argConfig) { var _a, _b; var result = []; var argUsageExample = formatArgUsageExample(argConfig); if (argUsageExample.length > 30) { result.push([argUsageExample]); result.push(['', (_a = argConfig.description) !== null && _a !== void 0 ? _a : '<no description>']); } else { result.push([argUsageExample, (_b = argConfig.description) !== null && _b !== void 0 ? _b : '<no description>']); } result.push(['', "Type: ".concat(formatArgType(argConfig))]); if (argConfig.default !== undefined) { result.push(['', "Default value: ".concat(JSON.stringify(argConfig.default))]); } if (argConfig.choices !== undefined) { result.push(['', "Allowed values: ".concat(argConfig.choices.join(', '))]); } result.push(['']); return result; } /** * Converts a two-dimensional array of strings into a table. * * @param data The two-dimensional array of strings. * @param padding The amount of padding to add to each cell. * @returns The table as a string. * * @category Utils */ function convertToTable(data, padding) { var e_1, _a; if (padding === void 0) { padding = 0; } if (!data || data.length === 0) { return ''; } var maxCols = Math.max.apply(Math, __spreadArray([], __read(data.map(function (row) { return row.length; })), false)); var columnWidths = Array(maxCols).fill(0); var _loop_1 = function (col) { columnWidths[col] = Math.max.apply(Math, __spreadArray([], __read(data.map(function (row) { var _a; var len = ((_a = row[col]) === null || _a === void 0 ? void 0 : _a.length) || 0; return row.length === 1 ? 0 : len; })), false)); }; for (var col = 0; col < maxCols; col++) { _loop_1(col); } var result = []; try { for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) { var row = data_1_1.value; var rowString = row .map(function (cell, i) { return cell.padEnd(columnWidths[i] + padding, ' '); }) // Добавляем пробелы до максимальной длины .join(' '); // Разделитель result.push(rowString); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1); } finally { if (e_1) throw e_1.error; } } return result.join('\n'); } /** * Converts an argument type into a string representation. * * @param argConfig - The argument configuration. * * @returns A string representation of the argument type. * * @category Utils */ function formatArgType(argConfig) { var result = argConfig.multiple ? "Array<".concat(argConfig.type, ">") : argConfig.type; if (!argConfig.allowEmpty && argConfig.type !== 'boolean') { result += ' (not empty)'; } return result; } /** * Generates a usage example string for an argument. * * @param argConfig - The argument configuration. * * @returns A string representing the usage example for the argument. * * @category Utils */ function formatArgUsageExample(argConfig) { var result = ''; var argValueExample = formatArgValueExample(argConfig); if (argConfig.alias) { result += "".concat(argConfig.alias).concat(argValueExample, ", ").concat(argConfig.name).concat(argValueExample); } else { result += "".concat(argConfig.name).concat(argValueExample); } return result; } /** * Formats a table of strings into a tabbed format. * * @param rows - The 2D array of strings to format. * @param tabber - The tab character to use. Defaults to a standard tab. * * @returns The formatted strings. * * @category Utils */ function tabTableRows(rows, tabber) { if (tabber === void 0) { tabber = "\t"; } return rows.map(function (row) { return row.length ? __spreadArray(["".concat(tabber).concat(row[0])], __read(row.slice(1)), false) : []; }); } /** * Generates a value example string for an argument. * * @param argConfig - The argument configuration. * * @returns A string representing the value example for the argument. * * @category Utils */ function formatArgValueExample(argConfig) { if (argConfig.type === 'boolean' && !argConfig.multiple) { return ''; } if (argConfig.multiple) { return " <".concat(argConfig.type, "> <").concat(argConfig.type, "> ..."); } return " <".concat(argConfig.type, ">"); } //# sourceMappingURL=help.js.map