UNPKG

argparse-ts

Version:

Modern CLI arguments parser for node.js

129 lines 4.05 kB
/** * Generates a help message for an argument. * * @param argConfig - The argument configuration. * * @returns A 2D array of strings representing the help message. * * @category Utils */ export function formatArgHelp(argConfig) { var _a, _b; const result = []; const 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: ${formatArgType(argConfig)}`]); if (argConfig.default !== undefined) { result.push(['', `Default value: ${JSON.stringify(argConfig.default)}`]); } if (argConfig.choices !== undefined) { result.push(['', `Allowed values: ${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 */ export function convertToTable(data, padding = 0) { if (!data || data.length === 0) { return ''; } const maxCols = Math.max(...data.map(row => row.length)); const columnWidths = Array(maxCols).fill(0); for (let col = 0; col < maxCols; col++) { columnWidths[col] = Math.max(...data.map(row => { var _a; const len = ((_a = row[col]) === null || _a === void 0 ? void 0 : _a.length) || 0; return row.length === 1 ? 0 : len; })); } const result = []; for (const row of data) { const rowString = row .map((cell, i) => cell.padEnd(columnWidths[i] + padding, ' ')) // Добавляем пробелы до максимальной длины .join(' '); // Разделитель result.push(rowString); } 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) { let result = argConfig.multiple ? `Array<${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) { let result = ''; const argValueExample = formatArgValueExample(argConfig); if (argConfig.alias) { result += `${argConfig.alias}${argValueExample}, ${argConfig.name}${argValueExample}`; } else { result += `${argConfig.name}${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 */ export function tabTableRows(rows, tabber = "\t") { return rows.map((row) => row.length ? [`${tabber}${row[0]}`, ...row.slice(1)] : []); } /** * 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 ` <${argConfig.type}> <${argConfig.type}> ...`; } return ` <${argConfig.type}>`; } //# sourceMappingURL=help.js.map