-
-
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.
Add support for getting merged options including globals (#1671)
* Proof of concept * Use separate method for optsWithGlobals * Add documentation * Add tests * Simplify description * Add example * Remove unused param
- Loading branch information
1 parent
f902f6d
commit 772eb53
Showing
6 changed files
with
125 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// const { Command } = require('commander'); // (normal include) | ||
const { Command } = require('../'); // include commander in git clone of commander repo | ||
|
||
// Show use of .optsWithGlobals(), and compare with .opts(). | ||
|
||
const program = new Command(); | ||
|
||
program | ||
.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 optsWithGlobals.js --global sub --local |
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,63 @@ | ||
const commander = require('../'); | ||
|
||
test('when variety of options used with program then opts is same as optsWithGlobals', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('-b, --boolean') | ||
.option('-r, --require-value <value)') | ||
.option('-f, --float <value>', 'description', parseFloat) | ||
.option('-d, --default-value <value)', 'description', 'default value') | ||
.option('-n, --no-something'); | ||
|
||
program.parse(['-b', '-r', 'req', '-f', '1e2'], { from: 'user' }); | ||
expect(program.opts()).toEqual(program.optsWithGlobals()); | ||
}); | ||
|
||
test('when options in sub and program then optsWithGlobals includes both', () => { | ||
const program = new commander.Command(); | ||
let mergedOptions; | ||
program | ||
.option('-g, --global <value>'); | ||
program | ||
.command('sub') | ||
.option('-l, --local <value)') | ||
.action((options, cmd) => { | ||
mergedOptions = cmd.optsWithGlobals(); | ||
}); | ||
|
||
program.parse(['-g', 'GGG', 'sub', '-l', 'LLL'], { from: 'user' }); | ||
expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' }); | ||
}); | ||
|
||
test('when options in sub and subsub then optsWithGlobals includes both', () => { | ||
const program = new commander.Command(); | ||
let mergedOptions; | ||
program | ||
.command('sub') | ||
.option('-g, --global <value)') | ||
.command('subsub') | ||
.option('-l, --local <value)') | ||
.action((options, cmd) => { | ||
mergedOptions = cmd.optsWithGlobals(); | ||
}); | ||
|
||
program.parse(['sub', '-g', 'GGG', 'subsub', '-l', 'LLL'], { from: 'user' }); | ||
expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' }); | ||
}); | ||
|
||
test('when same named option in sub and program then optsWithGlobals includes global', () => { | ||
const program = new commander.Command(); | ||
let mergedOptions; | ||
program | ||
.option('-c, --common <value>') | ||
.enablePositionalOptions(); | ||
program | ||
.command('sub') | ||
.option('-c, --common <value)') | ||
.action((options, cmd) => { | ||
mergedOptions = cmd.optsWithGlobals(); | ||
}); | ||
|
||
program.parse(['-c', 'GGG', 'sub', '-c', 'LLL'], { from: 'user' }); | ||
expect(mergedOptions).toEqual({ common: 'GGG' }); | ||
}); |
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