Add getOptionValueSourceWithGlobals for completeness. (#1832)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent 0ae5b2f · Browse files · 4 files·+132 added−53 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+132 added−53 removed
- tests:+104 added−52 removed
- other:+28 added−1 removed
Provenance
- Commit
5a201ec- Tree
1a574a5- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
0ae5b2f
4 files changed+132 added−53 removed
modified:lib/command.js+19 added
813 unmodified lines814 return this._optionValueSources[key];815 }816 817 /**818+ * Get source of option value. See also .optsWithGlobals().819+ * Expected values are default | config | env | cli | implied820+ *821+ * @param {string} key822+ * @return {string}823+ */824+825+ getOptionValueSourceWithGlobals(key) {826+ // global overwrites local, like optsWithGlobals827+ let source;828+ getCommandAndParents(this).forEach((cmd) => {829+ if (cmd.getOptionValueSource(key) !== undefined) {830+ source = cmd.getOptionValueSource(key);831+ }832+ });833+ return source;834+ }835+836+ /**837 * Get user arguments from implied or explicit arguments.838 * Side-effects: set _scriptPath if args included script. Used for default program name, and subcommand searches.839 *840 * @api private1339 unmodified linesmodified:tests/options.optsWithGlobals.test.js+104 added-52 removed
1234567891034121314567891011121314161718192021222324252615161718282930192021222324252627282930323334353637383940414231323334353637383940414243444546444547484950515253545556575859606162636465666768484950515253545556575859697071727374616275767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115const 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');// Testing optsWithGlobals and getOptionValueSourceWithGlobals with focus on globals.
program.parse(['-b', '-r', 'req', '-f', '1e2'], { from: 'user' }); expect(program.opts()).toEqual(program.optsWithGlobals());});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');
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(['-b', '-r', 'req', '-f', '1e2'], { from: 'user' }); expect(program.opts()).toEqual(program.optsWithGlobals()); });
program.parse(['-g', 'GGG', 'sub', '-l', 'LLL'], { from: 'user' }); expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' });}); 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(); });
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(['-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' }); 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' }); });});
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(); });describe('getOptionValueSourceWithGlobals', () => { test('when option used with simple command then source is defined', () => { const program = new commander.Command(); program .option('-g, --global');
program.parse(['-c', 'GGG', 'sub', '-c', 'LLL'], { from: 'user' }); expect(mergedOptions).toEqual({ common: 'GGG' }); 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'); });});modified:typings/index.d.ts+6 added-1 removed
598 unmodified lines599 */600 setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this;601 602 /**603- * Retrieve option value source.603+ * Get source of option value.604 */605 getOptionValueSource(key: string): OptionValueSource | undefined;606 607 /**608+ * Get source of option value. See also .optsWithGlobals().609+ */610+ getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined;611+612+ /**613 * Alter parsing of short flags with optional values.614 *615 * @example616 * ```275 unmodified linesmodified:typings/index.test-d.ts+3 added
172 unmodified lines173 174 // getOptionValueSource175 expectType<commander.OptionValueSource | undefined>(program.getOptionValueSource('example'));176 177+// getOptionValueSourceWithGlobals178+expectType<commander.OptionValueSource | undefined>(program.getOptionValueSourceWithGlobals('example'));179+180 // combineFlagAndOptionalValue181 expectType<commander.Command>(program.combineFlagAndOptionalValue());182 expectType<commander.Command>(program.combineFlagAndOptionalValue(false));183 297 unmodified lines