|
| 1 | +const commander = require('../'); |
| 2 | + |
| 3 | +test('when variety of options used with program then opts is same as optsWithGlobals', () => { |
| 4 | + const program = new commander.Command(); |
| 5 | + program |
| 6 | + .option('-b, --boolean') |
| 7 | + .option('-r, --require-value <value)') |
| 8 | + .option('-f, --float <value>', 'description', parseFloat) |
| 9 | + .option('-d, --default-value <value)', 'description', 'default value') |
| 10 | + .option('-n, --no-something'); |
| 11 | + |
| 12 | + program.parse(['-b', '-r', 'req', '-f', '1e2'], { from: 'user' }); |
| 13 | + expect(program.opts()).toEqual(program.optsWithGlobals()); |
| 14 | +}); |
| 15 | + |
| 16 | +test('when options in sub and program then optsWithGlobals includes both', () => { |
| 17 | + const program = new commander.Command(); |
| 18 | + let mergedOptions; |
| 19 | + program |
| 20 | + .option('-g, --global <value>'); |
| 21 | + program |
| 22 | + .command('sub') |
| 23 | + .option('-l, --local <value)') |
| 24 | + .action((options, cmd) => { |
| 25 | + mergedOptions = cmd.optsWithGlobals(); |
| 26 | + }); |
| 27 | + |
| 28 | + program.parse(['-g', 'GGG', 'sub', '-l', 'LLL'], { from: 'user' }); |
| 29 | + expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' }); |
| 30 | +}); |
| 31 | + |
| 32 | +test('when options in sub and subsub then optsWithGlobals includes both', () => { |
| 33 | + const program = new commander.Command(); |
| 34 | + let mergedOptions; |
| 35 | + program |
| 36 | + .command('sub') |
| 37 | + .option('-g, --global <value)') |
| 38 | + .command('subsub') |
| 39 | + .option('-l, --local <value)') |
| 40 | + .action((options, cmd) => { |
| 41 | + mergedOptions = cmd.optsWithGlobals(); |
| 42 | + }); |
| 43 | + |
| 44 | + program.parse(['sub', '-g', 'GGG', 'subsub', '-l', 'LLL'], { from: 'user' }); |
| 45 | + expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' }); |
| 46 | +}); |
| 47 | + |
| 48 | +test('when same named option in sub and program then optsWithGlobals includes global', () => { |
| 49 | + const program = new commander.Command(); |
| 50 | + let mergedOptions; |
| 51 | + program |
| 52 | + .option('-c, --common <value>') |
| 53 | + .enablePositionalOptions(); |
| 54 | + program |
| 55 | + .command('sub') |
| 56 | + .option('-c, --common <value)') |
| 57 | + .action((options, cmd) => { |
| 58 | + mergedOptions = cmd.optsWithGlobals(); |
| 59 | + }); |
| 60 | + |
| 61 | + program.parse(['-c', 'GGG', 'sub', '-c', 'LLL'], { from: 'user' }); |
| 62 | + expect(mergedOptions).toEqual({ common: 'GGG' }); |
| 63 | +}); |
0 commit comments