Skip to content
Myr

help-centered.mjs

This file

Path

examples/help-centered.mjs

42 lines · 1.1 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–42why, one step back
1import { Command, Help } from 'commander';
23// Right-justify the terms in the help output.4// Setup a subclass so we can do simple tweak of formatItem.56class MyHelp extends Help {7 formatItem(term, termWidth, description, helper) {8 // Pre-pad the term at start instead of end.9 const paddedTerm = term.padStart(10 termWidth + term.length - helper.displayWidth(term),11 );1213 return super.formatItem(paddedTerm, termWidth, description, helper);14 }15}1617class MyCommand extends Command {18 createCommand(name) {19 return new MyCommand(name);20 }21 createHelp() {22 return Object.assign(new MyHelp(), this.configureHelp());23 }24}2526const program = new MyCommand();2728program.configureHelp({ MyCommand });2930program31 .option('-s', 'short flag')32 .option('-f, --flag', 'short and long flag')33 .option('--long <number>', 'long flag');3435program.command('compile').alias('c').description('compile something');3637program.command('run', 'run something').command('print', 'print something');3839program.parse();4041// Try the following:42// node help-centered.mjs --help