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 fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243import { Command, Help } from 'commander';
// Right-justify the terms in the help output.// Setup a subclass so we can do simple tweak of formatItem.
class MyHelp extends Help { formatItem(term, termWidth, description, helper) { // Pre-pad the term at start instead of end. const paddedTerm = term.padStart( termWidth + term.length - helper.displayWidth(term), );
return super.formatItem(paddedTerm, termWidth, description, helper); }}
class MyCommand extends Command { createCommand(name) { return new MyCommand(name); } createHelp() { return Object.assign(new MyHelp(), this.configureHelp()); }}
const program = new MyCommand();
program.configureHelp({ MyCommand });
program .option('-s', 'short flag') .option('-f, --flag', 'short and long flag') .option('--long <number>', 'long flag');
program.command('compile').alias('c').description('compile something');
program.command('run', 'run something').command('print', 'print something');
program.parse();
// Try the following:// node help-centered.mjs --help