Skip to content

Commit 7fe7831

Browse files
authored
Add example of displaying custom usage in subcommand list (#1896)
* Add example of displaying custom usage in subcommand list * Make example more focused
1 parent 4ef19fa commit 7fe7831

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

examples/help-subcommands-usage.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// const commander = require('commander'); // (normal include)
2+
const commander = require('../'); // include commander in git clone of commander repo
3+
4+
// By default the subcommand list includes a fairly simple usage. If you have added a custom usage
5+
// to the subcommands you may wish to configure the help to show these instead.
6+
//
7+
// See also ./configure-help.js which shows configuring the subcommand list to have no usage at all
8+
// and just the subcommand name.
9+
10+
const program = new commander.Command()
11+
.configureHelp({ subcommandTerm: (cmd) => cmd.name() + ' ' + cmd.usage() });
12+
13+
program.command('make <FILENAME>')
14+
.usage('-root ROOTDIR [-abc] <FILENAME>')
15+
.requiredOption('--root <ROOTDIR>')
16+
.option('-a')
17+
.option('-b')
18+
.option('-c')
19+
.summary('example command with custom usage')
20+
.description('this full description is displayed in the full help and not in the list of subcommands');
21+
22+
program.command('serve <SERVICE>')
23+
.option('--port <PORTNUMBER>')
24+
.description('example command with default simple usage');
25+
26+
program.parse();
27+
28+
// Try the following:
29+
// node help-subcommands-usage help

0 commit comments

Comments
 (0)