-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhelp-centered.mjs
42 lines (31 loc) · 1.05 KB
/
help-centered.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { 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