-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getOptionValueSourceWithGlobals #1832
Merged
shadowspawn
merged 1 commit into
tj:develop
from
shadowspawn:feature/combo-with-long-name
Dec 19, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,63 +1,115 @@ | ||
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()); | ||
}); | ||
// Testing optsWithGlobals and getOptionValueSourceWithGlobals with focus on globals. | ||
|
||
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' }); | ||
}); | ||
describe('optsWithGlobals', () => { | ||
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(); | ||
}); | ||
|
||
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' }); | ||
program.parse(['-c', 'GGG', 'sub', '-c', 'LLL'], { from: 'user' }); | ||
expect(mergedOptions).toEqual({ common: 'GGG' }); | ||
}); | ||
}); | ||
|
||
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' }); | ||
describe('getOptionValueSourceWithGlobals', () => { | ||
test('when option used with simple command then source is defined', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('-g, --global'); | ||
|
||
program.parse(['-g'], { from: 'user' }); | ||
expect(program.getOptionValueSourceWithGlobals('global')).toEqual('cli'); | ||
}); | ||
|
||
test('when option used with program then source is defined', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('-g, --global'); | ||
const sub = program.command('sub') | ||
.option('-l, --local') | ||
.action(() => {}); | ||
|
||
program.parse(['sub', '-g'], { from: 'user' }); | ||
expect(sub.getOptionValueSourceWithGlobals('global')).toEqual('cli'); | ||
}); | ||
|
||
test('when option used with subcommand then source is defined', () => { | ||
const program = new commander.Command(); | ||
program | ||
.option('-g, --global'); | ||
const sub = program.command('sub') | ||
.option('-l, --local') | ||
.action(() => {}); | ||
|
||
program.parse(['sub', '-l'], { from: 'user' }); | ||
expect(sub.getOptionValueSourceWithGlobals('local')).toEqual('cli'); | ||
}); | ||
|
||
test('when same named option in sub and program then source is defined by global', () => { | ||
const program = new commander.Command(); | ||
program | ||
.enablePositionalOptions() | ||
.option('-c, --common <value>', 'description', 'default value'); | ||
const sub = program.command('sub') | ||
.option('-c, --common <value>') | ||
.action(() => {}); | ||
|
||
program.parse(['sub', '--common', 'value'], { from: 'user' }); | ||
expect(sub.getOptionValueSourceWithGlobals('common')).toEqual('default'); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference :
You can also write the following using
findLast
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use Node.js 18 or later.
My apologies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started out planning to target Commander v10.x just so I could use
??
, but switched to old-school Node 12 compatible code for now. Eventually!