Skip to content
Myr

command.parseOptions.test.js

This file

Path

tests/command.parseOptions.test.js

329 lines · 13.7 KiB · JavaScript

Last changed by

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

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

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 1–18why, one step back
1const childProcess = require('child_process');
2const commander = require('../');3const path = require('path');4const util = require('util');56const execFileAsync = util.promisify(childProcess.execFile);78// Combination of parse and parseOptions tests which are are more about details9// than high level behaviours which are tested elsewhere.1011// Tests created from reported bugs12describe('regression tests', () => {13 // https://github.com/tj/commander.js/issues/103214 function createProgram1032() {15 const program = new commander.Command();16 program17 .command('doit [id]')18 .option('--better', 'do it better')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 19why, one step back
19 .action((id, cmd) => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 20why, one step back
20 return program;

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 21why, one step back
21 }

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 22–37why, one step back
22
23 // https://github.com/tj/commander.js/issues/103224 test('when specify subcommand and argument then program.args not empty', () => {25 const program = createProgram1032();26 program.parse(['node', 'test.js', 'doit', 'myid']);27 expect(program.args.length).toBeGreaterThan(0);28 });2930 // https://github.com/tj/commander.js/issues/103231 test('when specify subcommand and option and argument then program.args not empty', () => {32 const program = createProgram1032();33 program.parse(['node', 'test.js', 'doit', '--better', 'myid']);34 expect(program.args.length).toBeGreaterThan(0);35 });3637 // https://github.com/tj/commander.js/issues/508

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 38why, one step back
38 test('when arguments to executable include option flags then argument order preserved', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 39why, one step back
39 const pm = path.join(__dirname, 'fixtures/pm');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 40–50why, one step back
40 const { stdout } = await execFileAsync('node', [
41 pm,42 'echo',43 '1',44 '2',45 '--dry-run',46 '3',47 '4',48 '5',49 '6',50 ]);

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 51–62why, one step back
51 expect(stdout).toBe("[ '1', '2', '--dry-run', '3', '4', '5', '6' ]\n");
52 });53});5455describe('parseOptions', () => {56 function createProgram() {57 const program = new commander.Command();58 program59 .option('--global-flag')60 .option('--global-value <value>')61 .command('sub [args...]')62 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 63why, one step back
63 program.action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 64why, one step back
64 return program;

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 65why, one step back
65 }

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 66–161why, one step back
66
67 // Subcommands are just another potential operand as far as parseOptions is concerned, so limited testing of subcommand as such.6869 test('when empty args then empty results', () => {70 const program = createProgram();71 const result = program.parseOptions([]);72 expect(result).toEqual({ operands: [], unknown: [] });73 });7475 test('when only operands then results has all operands', () => {76 const program = createProgram();77 const result = program.parseOptions('one two three'.split(' '));78 expect(result).toEqual({ operands: ['one', 'two', 'three'], unknown: [] });79 });8081 test('when subcommand and operand then results has subcommand and operand', () => {82 const program = createProgram();83 const result = program.parseOptions('sub one'.split(' '));84 expect(result).toEqual({ operands: ['sub', 'one'], unknown: [] });85 });8687 test('when program has flag then option removed', () => {88 const program = createProgram();89 const result = program.parseOptions('--global-flag'.split(' '));90 expect(result).toEqual({ operands: [], unknown: [] });91 });9293 test('when program has option with value then option removed', () => {94 const program = createProgram();95 const result = program.parseOptions('--global-value foo'.split(' '));96 expect(result).toEqual({ operands: [], unknown: [] });97 });9899 test('when program has flag before operand then option removed', () => {100 const program = createProgram();101 const result = program.parseOptions('--global-flag arg'.split(' '));102 expect(result).toEqual({ operands: ['arg'], unknown: [] });103 });104105 test('when program has flag after operand then option removed', () => {106 const program = createProgram();107 const result = program.parseOptions('arg --global-flag'.split(' '));108 expect(result).toEqual({ operands: ['arg'], unknown: [] });109 });110111 test('when program has flag after subcommand then option removed', () => {112 const program = createProgram();113 const result = program.parseOptions('sub --global-flag'.split(' '));114 expect(result).toEqual({ operands: ['sub'], unknown: [] });115 });116117 test('when program has unknown option then option returned in unknown', () => {118 const program = createProgram();119 const result = program.parseOptions('--unknown'.split(' '));120 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });121 });122123 test('when program has unknown option before operands then all unknown in same order', () => {124 const program = createProgram();125 const result = program.parseOptions('--unknown arg'.split(' '));126 expect(result).toEqual({ operands: [], unknown: ['--unknown', 'arg'] });127 });128129 test('when program has unknown option after operand then option returned in unknown', () => {130 const program = createProgram();131 const result = program.parseOptions('arg --unknown'.split(' '));132 expect(result).toEqual({ operands: ['arg'], unknown: ['--unknown'] });133 });134135 test('when program has flag after unknown option then flag removed', () => {136 const program = createProgram();137 const result = program.parseOptions('--unknown --global-flag'.split(' '));138 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });139 });140141 test('when subcommand has flag then flag returned as unknown', () => {142 const program = createProgram();143 const result = program.parseOptions('sub --sub-flag'.split(' '));144 expect(result).toEqual({ operands: ['sub'], unknown: ['--sub-flag'] });145 });146147 test('when program has literal before known flag then option returned as operand', () => {148 const program = createProgram();149 const result = program.parseOptions('-- --global-flag'.split(' '));150 expect(result).toEqual({ operands: ['--global-flag'], unknown: [] });151 });152153 test('when program has literal before unknown option then option returned as operand', () => {154 const program = createProgram();155 const result = program.parseOptions('-- --unknown uuu'.split(' '));156 expect(result).toEqual({ operands: ['--unknown', 'uuu'], unknown: [] });157 });158159 test('when program has literal after unknown option then literal preserved too', () => {160 const program = createProgram();161 const result = program.parseOptions('--unknown1 -- --unknown2'.split(' '));

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 162–165why, one step back
162 expect(result).toEqual({
163 operands: [],164 unknown: ['--unknown1', '--', '--unknown2'],165 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 166–172why, one step back
166 });
167});168169// parse now sets program.args to the result of parseOptions (operands + unknown). Some limited testing.170describe('parse and program.args', () => {171 test('when program has known flag and operand then option removed and operand returned', () => {172 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 173why, one step back
173 program.option('--global-flag');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 174–183why, one step back
174 program.parse('node test.js --global-flag arg'.split(' '));
175 expect(program.args).toEqual(['arg']);176 });177178 test('when program has mixed arguments then known options removed and rest returned in same order', () => {179 const program = new commander.Command();180 program181 .allowUnknownOption()182 .option('--global-flag')183 .option('--global-value <value>');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 184–188why, one step back
184 program.parse(
185 'node test.js aaa --global-flag bbb --unknown ccc --global-value value'.split(186 ' ',187 ),188 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 189–198why, one step back
189 expect(program.args).toEqual(['aaa', 'bbb', '--unknown', 'ccc']);
190 });191192 test('when subcommand has mixed arguments then program flags removed and rest returned in same order', () => {193 const program = new commander.Command();194 program195 .option('--global-flag')196 .option('--global-value <value>')197 .command('sub [args...]')198 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 199–203why, one step back
199 program.parse(
200 'node test.js --global-flag sub --sub-flag arg --global-value value'.split(201 ' ',202 ),203 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 204–248why, one step back
204 expect(program.args).toEqual(['sub', '--sub-flag', 'arg']);
205 });206});207208// Utility Conventions: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01209//210// 12.1 Utility Argument Syntax211// 2. Option-arguments are shown separated from their options by <blank> characters, except when the option-argument is212// enclosed in the '[' and ']' notation to indicate that it is optional. This reflects the situation in which an213// optional option-argument (if present) is included within the same argument string as the option; for a mandatory option-argument,214// it is the next argument. The Utility Syntax Guidelines in Utility Syntax Guidelines require that the option be a separate argument215// from its option-argument and that option-arguments not be optional, but there are some exceptions in POSIX.1-2017 to ensure216// continued operation of historical applications:217//218// a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example),219// a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall220// also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.221//222// b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application223// shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening224// <blank> characters. If the utility receives an argument containing only the option, it shall behave as specified in its description225// for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option.226//227// 12.2 Utility Syntax Guidelines, Guideline 5:228// One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when229// grouped behind one '-' delimiter.230231// Commander conformance:232// - allows separate argument for required option-argument233// - allows value in same argument for short flag with required option-argument234// - non-conforming: allows separate argument for optional option-argument if does not start with '-'235// - allows value in same argument for short flag with optional option-argument236// - allows short flags as per 12.2237//238// The focus in this file is testing the behaviours with known vs unknown options.239// See options.values.test.js for more general testing of known options.240241describe('Utility Conventions', () => {242 function createProgram() {243 const program = new commander.Command();244 program245 .option('-a,--aaa')246 .option('-b,--bbb')247 .option('-c,--ccc <value>')248 .option('-d,--ddd [value]');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 249why, one step back
249 program.action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 250why, one step back
250 return program;

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 251why, one step back
251 }

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 252–263why, one step back
252
253 test('when program has combo known boolean short flags then arg removed', () => {254 const program = createProgram();255 const result = program.parseOptions(['-ab']);256 expect(result).toEqual({ operands: [], unknown: [] });257 expect(program.opts()).toEqual({ aaa: true, bbb: true });258 });259260 test('when program has combo unknown short flags then arg preserved', () => {261 const program = createProgram();262 const result = program.parseOptions(['-pq']);263 expect(result).toEqual({ operands: [], unknown: ['-pq'] });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 264why, one step back
264 expect(program.opts()).toEqual({});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 265–305why, one step back
265 });
266267 test('when program has combo known short option and required value then arg removed', () => {268 const program = createProgram();269 const result = program.parseOptions(['-cvalue']);270 expect(result).toEqual({ operands: [], unknown: [] });271 expect(program.opts()).toEqual({ ccc: 'value' });272 });273274 test('when program has combo known short option and optional value then arg removed', () => {275 const program = createProgram();276 const result = program.parseOptions(['-dvalue']);277 expect(result).toEqual({ operands: [], unknown: [] });278 expect(program.opts()).toEqual({ ddd: 'value' });279 });280281 test('when program has known combo short boolean flags and required value then arg removed', () => {282 const program = createProgram();283 const result = program.parseOptions(['-abcvalue']);284 expect(result).toEqual({ operands: [], unknown: [] });285 expect(program.opts()).toEqual({ aaa: true, bbb: true, ccc: 'value' });286 });287288 test('when program has known combo short boolean flags and optional value then arg removed', () => {289 const program = createProgram();290 const result = program.parseOptions(['-abdvalue']);291 expect(result).toEqual({ operands: [], unknown: [] });292 expect(program.opts()).toEqual({ aaa: true, bbb: true, ddd: 'value' });293 });294295 test('when program has known long flag=value then arg removed', () => {296 const program = createProgram();297 const result = program.parseOptions(['--ccc=value']);298 expect(result).toEqual({ operands: [], unknown: [] });299 expect(program.opts()).toEqual({ ccc: 'value' });300 });301302 test('when program has unknown long flag=value then arg preserved', () => {303 const program = createProgram();304 const result = program.parseOptions(['--rrr=value']);305 expect(result).toEqual({ operands: [], unknown: ['--rrr=value'] });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 306why, one step back
306 expect(program.opts()).toEqual({});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 307–329why, one step back
307 });
308309 test('when program has combo optional and combineFlagAndOptionalValue() then treat as value', () => {310 const program = createProgram();311 program.combineFlagAndOptionalValue();312 program.parseOptions(['-db']);313 expect(program.opts()).toEqual({ ddd: 'b' });314 });315316 test('when program has combo optional and combineFlagAndOptionalValue(true) then treat as value', () => {317 const program = createProgram();318 program.combineFlagAndOptionalValue(true);319 program.parseOptions(['-db']);320 expect(program.opts()).toEqual({ ddd: 'b' });321 });322323 test('when program has combo optional and combineFlagAndOptionalValue(false) then treat as boolean', () => {324 const program = createProgram();325 program.combineFlagAndOptionalValue(false);326 program.parseOptions(['-db']);327 expect(program.opts()).toEqual({ ddd: true, bbb: true });328 });329});