Skip to content
Myr

man-style-help.mjs

This file

Path

examples/man-style-help.mjs

41 lines · 1.0 KiB · JavaScript

Last changed by

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

History of this file

Take it

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

lines 1–41why, one step back
1import { Command } from 'commander';
23// Layout the help like a man page, with the description starting on the next line.45function formatItem(term, termWidth, description, helper) {6 const termIndent = 2;7 const descIndent = 6;8 const helpWidth = this.helpWidth || 80;910 // No need to pad term as on its own line.11 const lines = [' '.repeat(termIndent) + term];1213 if (description) {14 const boxText = helper.boxWrap(description, helpWidth - 6);15 const descIndentText = ' '.repeat(descIndent);16 lines.push(17 descIndentText + boxText.split('\n').join('\n' + descIndentText),18 );19 }2021 lines.push('');22 return lines.join('\n');23}2425const program = new Command();2627program.configureHelp({ formatItem });2829program30 .option('-s', 'short flag')31 .option('-f, --flag', 'short and long flag')32 .option('--long <number>', 'l '.repeat(100));3334program35 .command('sub1', 'sssss '.repeat(33))36 .command('sub2', 'subcommand 2 description');3738program.parse();3940// Try the following:41// node man-style-help.mjs --help