-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support showing global options in help (#1828)
Add help configuration to show the global options.
- Loading branch information
1 parent
198c6e4
commit 0ae5b2f
Showing
10 changed files
with
228 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env node | ||
|
||
// This example shows global options on the program which affect all the subcommands. | ||
// See how to work with global options in the subcommand and display them in the help. | ||
// | ||
// (A different pattern for a "global" option is to add it to the subcommands, rather | ||
// than to the program. See global-options-added.js.) | ||
|
||
// const { Command } = require('commander'); // (normal include) | ||
const { Command } = require('../'); // include commander in git clone of commander repo | ||
|
||
const program = new Command(); | ||
|
||
program | ||
.configureHelp({ showGlobalOptions: true }) | ||
.option('-g, --global'); | ||
|
||
program | ||
.command('sub') | ||
.option('-l, --local') | ||
.action((options, cmd) => { | ||
console.log({ | ||
opts: cmd.opts(), | ||
optsWithGlobals: cmd.optsWithGlobals() | ||
}); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// node global-options-nested.js --global sub --local | ||
// node global-options-nested.js sub --help |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const commander = require('../'); | ||
|
||
test('when default configuration then global options hidden', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('--global'); | ||
const sub = program.command('sub'); | ||
expect(sub.helpInformation()).not.toContain('global'); | ||
}); | ||
|
||
test('when showGlobalOptions:true then program options shown', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('--global') | ||
.configureHelp({ showGlobalOptions: true }); | ||
const sub = program.command('sub'); | ||
expect(sub.helpInformation()).toContain('global'); | ||
}); | ||
|
||
test('when showGlobalOptions:true and no global options then global options header not shown', () => { | ||
const program = new commander.Command(); | ||
program | ||
.configureHelp({ showGlobalOptions: true }); | ||
const sub = program.command('sub'); | ||
expect(sub.helpInformation()).not.toContain('Global'); | ||
}); | ||
|
||
test('when showGlobalOptions:true and nested commands then combined nested options shown program last', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('--global') | ||
.configureHelp({ showGlobalOptions: true }); | ||
const sub1 = program.command('sub1') | ||
.option('--sub1'); | ||
const sub2 = sub1.command('sub2'); | ||
expect(sub2.helpInformation()).toContain(`Global Options: | ||
--sub1 | ||
--global | ||
`); | ||
}); | ||
|
||
test('when showGlobalOptions:true and sortOptions: true then global options sorted', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('-3') | ||
.option('-4') | ||
.option('-2') | ||
.configureHelp({ showGlobalOptions: true, sortOptions: true }); | ||
const sub1 = program.command('sub1') | ||
.option('-6') | ||
.option('-1') | ||
.option('-5'); | ||
const sub2 = sub1.command('sub2'); | ||
expect(sub2.helpInformation()).toContain(`Global Options: | ||
-1 | ||
-2 | ||
-3 | ||
-4 | ||
-5 | ||
-6 | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const commander = require('../'); | ||
|
||
test('when default configuration then return empty array', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('--global'); | ||
const sub = program.command('sub'); | ||
const helper = sub.createHelp(); | ||
expect(helper.visibleGlobalOptions(program)).toEqual([]); | ||
}); | ||
|
||
test('when showGlobalOptions:true then return program options', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('--global') | ||
.configureHelp({ showGlobalOptions: true }); | ||
const sub = program.command('sub'); | ||
const helper = sub.createHelp(); | ||
const visibleOptionNames = helper.visibleGlobalOptions(sub).map(option => option.name()); | ||
expect(visibleOptionNames).toEqual(['global']); | ||
}); | ||
|
||
test('when showGlobalOptions:true and program has version then return version', () => { | ||
const program = new commander.Command(); | ||
program | ||
.configureHelp({ showGlobalOptions: true }) | ||
.version('1.2.3'); | ||
const sub = program.command('sub'); | ||
const helper = sub.createHelp(); | ||
const visibleOptionNames = helper.visibleGlobalOptions(sub).map(option => option.name()); | ||
expect(visibleOptionNames).toEqual(['version']); | ||
}); | ||
|
||
test('when showGlobalOptions:true and nested commands then return combined global options', () => { | ||
const program = new commander.Command(); | ||
program | ||
.configureHelp({ showGlobalOptions: true }) | ||
.option('--global'); | ||
const sub1 = program.command('sub1') | ||
.option('--sub1'); | ||
const sub2 = sub1.command('sub2'); | ||
const helper = sub2.createHelp(); | ||
const visibleOptionNames = helper.visibleGlobalOptions(sub2).map(option => option.name()); | ||
expect(visibleOptionNames).toEqual(['sub1', 'global']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters