command.parseOptions.test.js
This file
Path
tests/command.parseOptions.test.js
335 lines · 13.9 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–6why, one step back1import * as childProcess from 'child_process';2import * as commander from '../index.js';3import * as path from 'path';4import * as util from 'util';5import { test, describe } from 'node:test';6import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 7–9why, one step back78const execFileAsync = util.promisify(childProcess.execFile);9Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585
line 10why, one step back10// Combination of parse and parseOptions tests which are more about detailsAdd copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 11–13why, one step back11// than high level behaviours which are tested elsewhere.1213// Tests created from reported bugsAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 14why, one step back14describe('parsing regression tests', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 15–20why, one step back15 // https://github.com/tj/commander.js/issues/103216 function createProgram1032() {17 const program = new commander.Command();18 program19 .command('doit [id]')20 .option('--better', 'do it better')Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 21why, one step back21 .action((id, cmd) => {});Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 22–28why, one step back22 return program;23 }2425 // https://github.com/tj/commander.js/issues/103226 test('when specify subcommand and argument then program.args not empty', () => {27 const program = createProgram1032();28 program.parse(['node', 'test.js', 'doit', 'myid']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 29why, one step back29 assert(program.args.length > 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 30–35why, one step back30 });3132 // https://github.com/tj/commander.js/issues/103233 test('when specify subcommand and option and argument then program.args not empty', () => {34 const program = createProgram1032();35 program.parse(['node', 'test.js', 'doit', '--better', 'myid']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 36why, one step back36 assert(program.args.length > 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 37–39why, one step back37 });3839 // https://github.com/tj/commander.js/issues/508Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 40why, one step back40 test('when arguments to executable include option flags then argument order preserved', async () => {Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 41why, one step back41 const pm = path.join(import.meta.dirname, 'fixtures/pm');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 42–52why, one step back42 const { stdout } = await execFileAsync('node', [43 pm,44 'echo',45 '1',46 '2',47 '--dry-run',48 '3',49 '4',50 '5',51 '6',52 ]);Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 53why, one step back53 assert.equal(stdout, '["1","2","--dry-run","3","4","5","6"]\n');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 54–56why, one step back54 });55});56Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 57why, one step back57describe('Command.parseOptions()', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 58–64why, one step back58 function createProgram() {59 const program = new commander.Command();60 program61 .option('--global-flag')62 .option('--global-value <value>')63 .command('sub [args...]')64 .option('--sub-flag');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 65why, one step back65 program.action(() => {});Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 66–73why, one step back66 return program;67 }6869 // Subcommands are just another potential operand as far as parseOptions is concerned, so limited testing of subcommand as such.7071 test('when empty args then empty results', () => {72 const program = createProgram();73 const result = program.parseOptions([]);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 74why, one step back74 assert.deepEqual(result, { operands: [], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 75–79why, one step back75 });7677 test('when only operands then results has all operands', () => {78 const program = createProgram();79 const result = program.parseOptions('one two three'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 80–83why, one step back80 assert.deepEqual(result, {81 operands: ['one', 'two', 'three'],82 unknown: [],83 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 84–88why, one step back84 });8586 test('when subcommand and operand then results has subcommand and operand', () => {87 const program = createProgram();88 const result = program.parseOptions('sub one'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 89why, one step back89 assert.deepEqual(result, { operands: ['sub', 'one'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 90–94why, one step back90 });9192 test('when program has flag then option removed', () => {93 const program = createProgram();94 const result = program.parseOptions('--global-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 95why, one step back95 assert.deepEqual(result, { operands: [], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 96–100why, one step back96 });9798 test('when program has option with value then option removed', () => {99 const program = createProgram();100 const result = program.parseOptions('--global-value foo'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 101why, one step back101 assert.deepEqual(result, { operands: [], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 102–106why, one step back102 });103104 test('when program has flag before operand then option removed', () => {105 const program = createProgram();106 const result = program.parseOptions('--global-flag arg'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 107why, one step back107 assert.deepEqual(result, { operands: ['arg'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 108–112why, one step back108 });109110 test('when program has flag after operand then option removed', () => {111 const program = createProgram();112 const result = program.parseOptions('arg --global-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 113why, one step back113 assert.deepEqual(result, { operands: ['arg'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 114–118why, one step back114 });115116 test('when program has flag after subcommand then option removed', () => {117 const program = createProgram();118 const result = program.parseOptions('sub --global-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 119why, one step back119 assert.deepEqual(result, { operands: ['sub'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 120–124why, one step back120 });121122 test('when program has unknown option then option returned in unknown', () => {123 const program = createProgram();124 const result = program.parseOptions('--unknown'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 125why, one step back125 assert.deepEqual(result, { operands: [], unknown: ['--unknown'] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 126–130why, one step back126 });127128 test('when program has unknown option before operands then all unknown in same order', () => {129 const program = createProgram();130 const result = program.parseOptions('--unknown arg'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 131why, one step back131 assert.deepEqual(result, { operands: [], unknown: ['--unknown', 'arg'] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 132–136why, one step back132 });133134 test('when program has unknown option after operand then option returned in unknown', () => {135 const program = createProgram();136 const result = program.parseOptions('arg --unknown'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 137why, one step back137 assert.deepEqual(result, { operands: ['arg'], unknown: ['--unknown'] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 138–142why, one step back138 });139140 test('when program has flag after unknown option then flag removed', () => {141 const program = createProgram();142 const result = program.parseOptions('--unknown --global-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 143why, one step back143 assert.deepEqual(result, { operands: [], unknown: ['--unknown'] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 144–148why, one step back144 });145146 test('when subcommand has flag then flag returned as unknown', () => {147 const program = createProgram();148 const result = program.parseOptions('sub --sub-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 149why, one step back149 assert.deepEqual(result, { operands: ['sub'], unknown: ['--sub-flag'] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 150–154why, one step back150 });151152 test('when program has literal before known flag then option returned as operand', () => {153 const program = createProgram();154 const result = program.parseOptions('-- --global-flag'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 155why, one step back155 assert.deepEqual(result, { operands: ['--global-flag'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 156–160why, one step back156 });157158 test('when program has literal before unknown option then option returned as operand', () => {159 const program = createProgram();160 const result = program.parseOptions('-- --unknown uuu'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 161why, one step back161 assert.deepEqual(result, { operands: ['--unknown', 'uuu'], unknown: [] });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 162–166why, one step back162 });163164 test('when program has literal after unknown option then literal preserved too', () => {165 const program = createProgram();166 const result = program.parseOptions('--unknown1 -- --unknown2'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 167why, one step back167 assert.deepEqual(result, {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 168–170why, one step back168 operands: [],169 unknown: ['--unknown1', '--', '--unknown2'],170 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 171–174why, one step back171 });172});173174// parse now sets program.args to the result of parseOptions (operands + unknown). Some limited testing.Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 175why, one step back175describe('Command.args after parsing', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 176–177why, one step back176 test('when program has known flag and operand then option removed and operand returned', () => {177 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 178why, one step back178 program.option('--global-flag').argument('[arg...]');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 179why, one step back179 program.parse('node test.js --global-flag arg'.split(' '));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 180why, one step back180 assert.deepEqual(program.args, ['arg']);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 181–187why, one step back181 });182183 test('when program has mixed arguments then known options removed and rest returned in same order', () => {184 const program = new commander.Command();185 program186 .allowUnknownOption()187 .option('--global-flag')Disallow excess arguments (#2223)John Geebefore adoption58c28eb
lines 188–189why, one step back188 .option('--global-value <value>')189 .argument('[arg...]');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 190–194why, one step back190 program.parse(191 'node test.js aaa --global-flag bbb --unknown ccc --global-value value'.split(192 ' ',193 ),194 );Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 195why, one step back195 assert.deepEqual(program.args, ['aaa', 'bbb', '--unknown', 'ccc']);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 196–204why, one step back196 });197198 test('when subcommand has mixed arguments then program flags removed and rest returned in same order', () => {199 const program = new commander.Command();200 program201 .option('--global-flag')202 .option('--global-value <value>')203 .command('sub [args...]')204 .option('--sub-flag');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 205–209why, one step back205 program.parse(206 'node test.js --global-flag sub --sub-flag arg --global-value value'.split(207 ' ',208 ),209 );Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 210why, one step back210 assert.deepEqual(program.args, ['sub', '--sub-flag', 'arg']);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 211–239why, one step back211 });212});213214// Utility Conventions: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01215//216// 12.1 Utility Argument Syntax217// 2. Option-arguments are shown separated from their options by <blank> characters, except when the option-argument is218// enclosed in the '[' and ']' notation to indicate that it is optional. This reflects the situation in which an219// optional option-argument (if present) is included within the same argument string as the option; for a mandatory option-argument,220// it is the next argument. The Utility Syntax Guidelines in Utility Syntax Guidelines require that the option be a separate argument221// from its option-argument and that option-arguments not be optional, but there are some exceptions in POSIX.1-2017 to ensure222// continued operation of historical applications:223//224// a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example),225// a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall226// also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.227//228// b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application229// shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening230// <blank> characters. If the utility receives an argument containing only the option, it shall behave as specified in its description231// for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option.232//233// 12.2 Utility Syntax Guidelines, Guideline 5:234// One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when235// grouped behind one '-' delimiter.236237// Commander conformance:238// - allows separate argument for required option-argument239// - allows value in same argument for short flag with required option-argumentMerge branch 'develop' into release/13.xJohn Geebefore adoption5a79585
line 240why, one step back240// - non-conforming: allows separate argument for optional option-argument if it does not start with '-'Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 241–246why, one step back241// - allows value in same argument for short flag with optional option-argument242// - allows short flags as per 12.2243//244// The focus in this file is testing the behaviours with known vs unknown options.245// See options.values.test.js for more general testing of known options.246Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 247why, one step back247describe('Posix Utility Conventions', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 248–254why, one step back248 function createProgram() {249 const program = new commander.Command();250 program251 .option('-a,--aaa')252 .option('-b,--bbb')253 .option('-c,--ccc <value>')254 .option('-d,--ddd [value]');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 255why, one step back255 program.action(() => {});Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 256–261why, one step back256 return program;257 }258259 test('when program has combo known boolean short flags then arg removed', () => {260 const program = createProgram();261 const result = program.parseOptions(['-ab']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 262–263why, one step back262 assert.deepEqual(result, { operands: [], unknown: [] });263 assert.deepEqual(program.opts(), { aaa: true, bbb: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 264–268why, one step back264 });265266 test('when program has combo unknown short flags then arg preserved', () => {267 const program = createProgram();268 const result = program.parseOptions(['-pq']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 269–270why, one step back269 assert.deepEqual(result, { operands: [], unknown: ['-pq'] });270 assert.deepEqual(program.opts(), {});Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 271–275why, one step back271 });272273 test('when program has combo known short option and required value then arg removed', () => {274 const program = createProgram();275 const result = program.parseOptions(['-cvalue']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 276–277why, one step back276 assert.deepEqual(result, { operands: [], unknown: [] });277 assert.deepEqual(program.opts(), { ccc: 'value' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 278–282why, one step back278 });279280 test('when program has combo known short option and optional value then arg removed', () => {281 const program = createProgram();282 const result = program.parseOptions(['-dvalue']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 283–284why, one step back283 assert.deepEqual(result, { operands: [], unknown: [] });284 assert.deepEqual(program.opts(), { ddd: 'value' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 285–289why, one step back285 });286287 test('when program has known combo short boolean flags and required value then arg removed', () => {288 const program = createProgram();289 const result = program.parseOptions(['-abcvalue']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 290–291why, one step back290 assert.deepEqual(result, { operands: [], unknown: [] });291 assert.deepEqual(program.opts(), { aaa: true, bbb: true, ccc: 'value' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 292–296why, one step back292 });293294 test('when program has known combo short boolean flags and optional value then arg removed', () => {295 const program = createProgram();296 const result = program.parseOptions(['-abdvalue']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 297–298why, one step back297 assert.deepEqual(result, { operands: [], unknown: [] });298 assert.deepEqual(program.opts(), { aaa: true, bbb: true, ddd: 'value' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 299–303why, one step back299 });300301 test('when program has known long flag=value then arg removed', () => {302 const program = createProgram();303 const result = program.parseOptions(['--ccc=value']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 304–305why, one step back304 assert.deepEqual(result, { operands: [], unknown: [] });305 assert.deepEqual(program.opts(), { ccc: 'value' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 306–310why, one step back306 });307308 test('when program has unknown long flag=value then arg preserved', () => {309 const program = createProgram();310 const result = program.parseOptions(['--rrr=value']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 311–312why, one step back311 assert.deepEqual(result, { operands: [], unknown: ['--rrr=value'] });312 assert.deepEqual(program.opts(), {});Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 313–318why, one step back313 });314315 test('when program has combo optional and combineFlagAndOptionalValue() then treat as value', () => {316 const program = createProgram();317 program.combineFlagAndOptionalValue();318 program.parseOptions(['-db']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 319why, one step back319 assert.deepEqual(program.opts(), { ddd: 'b' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 320–325why, one step back320 });321322 test('when program has combo optional and combineFlagAndOptionalValue(true) then treat as value', () => {323 const program = createProgram();324 program.combineFlagAndOptionalValue(true);325 program.parseOptions(['-db']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 326why, one step back326 assert.deepEqual(program.opts(), { ddd: 'b' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 327–332why, one step back327 });328329 test('when program has combo optional and combineFlagAndOptionalValue(false) then treat as boolean', () => {330 const program = createProgram();331 program.combineFlagAndOptionalValue(false);332 program.parseOptions(['-db']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 333why, one step back333 assert.deepEqual(program.opts(), { ddd: true, bbb: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 334–335why, one step back334 });335});