Skip to content
Myr

options.variadic.test.js

This file

Path

tests/options.variadic.test.js

170 lines · 5.6 KiB · JavaScript

Last changed by

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

History of this file

Take it

The walk stopped after 200 commits: lines older than f47bdf5 point at it.

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 1–7why, one step back
1const commander = require('../');
23describe('variadic option with required value', () => {4 test('when variadic with value missing then error', () => {5 const program = new commander.Command();6 program7 .exitOverride()

configure output (#1387)John Geebefore adoptioned7f13e

line 8why, one step back
8 .configureOutput({ writeErr: jest.fn() })

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 9–42why, one step back
9 .option('-r,--required <value...>');
1011 expect(() => {12 program.parse(['--required'], { from: 'user' });13 }).toThrow();14 });1516 test('when variadic with one value then set in array', () => {17 const program = new commander.Command();18 program19 .option('-r,--required <value...>');2021 program.parse(['--required', 'one'], { from: 'user' });22 expect(program.opts().required).toEqual(['one']);23 });2425 test('when variadic with two values then set in array', () => {26 const program = new commander.Command();27 program28 .option('-r,--required <value...>');2930 program.parse(['--required', 'one', 'two'], { from: 'user' });31 expect(program.opts().required).toEqual(['one', 'two']);32 });3334 test('when variadic with repeated values then set in array', () => {35 const program = new commander.Command();36 program37 .option('-r,--required <value...>');3839 program.parse(['--required', 'one', '--required', 'two'], { from: 'user' });40 expect(program.opts().required).toEqual(['one', 'two']);41 });42

Add support for variadic to choices (#1454)John Geebefore adoption09dfb67

lines 43–60why, one step back
43 test('when variadic used with choices and one value then set in array', () => {
44 const program = new commander.Command();45 program46 .addOption(new commander.Option('-r,--required <value...>').choices(['one', 'two']));4748 program.parse(['--required', 'one'], { from: 'user' });49 expect(program.opts().required).toEqual(['one']);50 });5152 test('when variadic used with choices and two values then set in array', () => {53 const program = new commander.Command();54 program55 .addOption(new commander.Option('-r,--required <value...>').choices(['one', 'two']));5657 program.parse(['--required', 'one', 'two'], { from: 'user' });58 expect(program.opts().required).toEqual(['one', 'two']);59 });60

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 61–64why, one step back
61 test('when variadic with short combined argument then not variadic', () => {
62 const program = new commander.Command();63 program64 .option('-r,--required <value...>')

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 65why, one step back
65 .argument('[arg]');

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 66–74why, one step back
66
67 program.parse(['-rone', 'operand'], { from: 'user' });68 expect(program.opts().required).toEqual(['one']);69 });7071 test('when variadic with long combined argument then not variadic', () => {72 const program = new commander.Command();73 program74 .option('-r,--required <value...>')

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 75why, one step back
75 .argument('[arg]');

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 76–85why, one step back
76
77 program.parse(['--required=one', 'operand'], { from: 'user' });78 expect(program.opts().required).toEqual(['one']);79 });8081 test('when variadic with value followed by option then option not eaten', () => {82 const program = new commander.Command();83 program84 .option('-r,--required <value...>')85 .option('-f, --flag')

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 86why, one step back
86 .argument('[arg]');

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 87–170why, one step back
87
88 program.parse(['-r', 'one', '-f'], { from: 'user' });89 const opts = program.opts();90 expect(opts.required).toEqual(['one']);91 expect(opts.flag).toBe(true);92 });9394 test('when variadic with no value and default then set to default', () => {95 const program = new commander.Command();96 program97 .option('-r,--required <value...>', 'variadic description', 'default');9899 program.parse([], { from: 'user' });100 expect(program.opts().required).toEqual('default');101 });102103 test('when variadic with coercion then coercion sets value', () => {104 const program = new commander.Command();105 program106 .option('-r,--required <value...>', 'variadic description', parseFloat);107108 // variadic processing reads the multiple values, but up to custom coercion what it does.109 program.parse(['--required', '1e2', '1e3'], { from: 'user' });110 expect(program.opts().required).toEqual(1000);111 });112});113114// Not retesting everything, but do some tests on variadic with optional115describe('variadic option with optional value', () => {116 test('when variadic not specified then value undefined', () => {117 const program = new commander.Command();118 program119 .option('-o,--optional [value...]');120121 program.parse([], { from: 'user' });122 expect(program.opts().optional).toBeUndefined();123 });124125 test('when variadic used as boolean flag then value true', () => {126 const program = new commander.Command();127 program128 .option('-o,--optional [value...]');129130 program.parse(['--optional'], { from: 'user' });131 expect(program.opts().optional).toBe(true);132 });133134 test('when variadic with one value then set in array', () => {135 const program = new commander.Command();136 program137 .option('-o,--optional [value...]');138139 program.parse(['--optional', 'one'], { from: 'user' });140 expect(program.opts().optional).toEqual(['one']);141 });142143 test('when variadic with two values then set in array', () => {144 const program = new commander.Command();145 program146 .option('-o,--optional [value...]');147148 program.parse(['--optional', 'one', 'two'], { from: 'user' });149 expect(program.opts().optional).toEqual(['one', 'two']);150 });151});152153describe('variadic special cases', () => {154 test('when option flags has word character before dots then is variadic', () => {155 const program = new commander.Command();156 program157 .option('-c,--comma [value...]');158159 expect(program.options[0].variadic).toBeTruthy();160 });161162 test('when option flags has special characters before dots then not variadic', () => {163 // This might be used to describe coercion for comma separated values, and is not variadic.164 const program = new commander.Command();165 program166 .option('-c,--comma [value,...]');167168 expect(program.options[0].variadic).toBeFalsy();169 });170});