430
return usageTexts.reduce<string[]>((r, ut) => r.concat([` ${ut[0]}`, ` ${ut[1]}`]), []);
431
}
432
>
const descriptionColumns = columns - argLength - 1;
argv.ts
433
>
const result: string[] = [];
434
>
for (const ut of usageTexts) {
435
>
const usage = ut[0];
436
>
const wrappedDescription = wrapText(ut[1], descriptionColumns);
437
>
const keyPadding = indent(argLength - usage.length - 2/*left padding*/);
438
>
result.push(' ' + usage + keyPadding + wrappedDescription[0]);
439
>
for (let i = 1; i < wrappedDescription.length; i++) {
440
result.push(indent(argLength) + wrappedDescription[i]);
441
}
443
>
return result;
444
>
}
445
446
>
function indent(count: number): string {
argv.ts
447
>
return ' '.repeat(count);
448
>
}
449
450
>
function wrapText(text: string, columns: number): string[] {
argv.ts
451
>
const lines: string[] = [];
452
>
while (text.length) {
453
>
let index = text.length < columns ? text.length : text.lastIndexOf(' ', columns);
454
>
if (index === 0) {
455
index = columns;
456
}
457
>
const line = text.slice(0, index).trim();
argv.ts
458
>
text = text.slice(index).trimStart();
459
>
lines.push(line);
460
>
}
461
>
return lines;
462
>
}
463
464
export function buildHelpMessage(productName: string, executableName: string, version: string, options: OptionDescriptions<unknown> | Record<string, Option<'boolean'> | Option<'string'> | Option<'string[]'> | Subcommand<Record<string, unknown>>>, capabilities?: { noPipe?: boolean; noInputFiles?: boolean; isChat?: boolean }): string {