11cb0ef41Sopenharmony_ciimport '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 31cb0ef41Sopenharmony_ciimport { test } from 'node:test'; 41cb0ef41Sopenharmony_ciimport { parseArgs } from 'node:util'; 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_citest('when short option used as flag then stored as flag', () => { 71cb0ef41Sopenharmony_ci const args = ['-f']; 81cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, f: true }, positionals: [] }; 91cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args }); 101cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 111cb0ef41Sopenharmony_ci}); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_citest('when short option used as flag before positional then stored as flag and positional (and not value)', () => { 141cb0ef41Sopenharmony_ci const args = ['-f', 'bar']; 151cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, f: true }, positionals: [ 'bar' ] }; 161cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args }); 171cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 181cb0ef41Sopenharmony_ci}); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_citest('when short option `type: "string"` used with value then stored as value', () => { 211cb0ef41Sopenharmony_ci const args = ['-f', 'bar']; 221cb0ef41Sopenharmony_ci const options = { f: { type: 'string' } }; 231cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, f: 'bar' }, positionals: [] }; 241cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 251cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 261cb0ef41Sopenharmony_ci}); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_citest('when short option listed in short used as flag then long option stored as flag', () => { 291cb0ef41Sopenharmony_ci const args = ['-f']; 301cb0ef41Sopenharmony_ci const options = { foo: { short: 'f', type: 'boolean' } }; 311cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, positionals: [] }; 321cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 331cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 341cb0ef41Sopenharmony_ci}); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_citest('when short option listed in short and long listed in `type: "string"` and ' + 371cb0ef41Sopenharmony_ci 'used with value then long option stored as value', () => { 381cb0ef41Sopenharmony_ci const args = ['-f', 'bar']; 391cb0ef41Sopenharmony_ci const options = { foo: { short: 'f', type: 'string' } }; 401cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: 'bar' }, positionals: [] }; 411cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 421cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 431cb0ef41Sopenharmony_ci}); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_citest('when short option `type: "string"` used without value then stored as flag', () => { 461cb0ef41Sopenharmony_ci const args = ['-f']; 471cb0ef41Sopenharmony_ci const options = { f: { type: 'string' } }; 481cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, f: true }, positionals: [] }; 491cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 501cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 511cb0ef41Sopenharmony_ci}); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_citest('short option group behaves like multiple short options', () => { 541cb0ef41Sopenharmony_ci const args = ['-rf']; 551cb0ef41Sopenharmony_ci const options = { }; 561cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, r: true, f: true }, positionals: [] }; 571cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 581cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 591cb0ef41Sopenharmony_ci}); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_citest('short option group does not consume subsequent positional', () => { 621cb0ef41Sopenharmony_ci const args = ['-rf', 'foo']; 631cb0ef41Sopenharmony_ci const options = { }; 641cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, r: true, f: true }, positionals: ['foo'] }; 651cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 661cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 671cb0ef41Sopenharmony_ci}); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci// See: Guideline 5 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html 701cb0ef41Sopenharmony_citest('if terminal of short-option group configured `type: "string"`, subsequent positional is stored', () => { 711cb0ef41Sopenharmony_ci const args = ['-rvf', 'foo']; 721cb0ef41Sopenharmony_ci const options = { f: { type: 'string' } }; 731cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, r: true, v: true, f: 'foo' }, positionals: [] }; 741cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 751cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 761cb0ef41Sopenharmony_ci}); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_citest('handles short-option groups in conjunction with long-options', () => { 791cb0ef41Sopenharmony_ci const args = ['-rf', '--foo', 'foo']; 801cb0ef41Sopenharmony_ci const options = { foo: { type: 'string' } }; 811cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, r: true, f: true, foo: 'foo' }, positionals: [] }; 821cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 831cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 841cb0ef41Sopenharmony_ci}); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_citest('handles short-option groups with "short" alias configured', () => { 871cb0ef41Sopenharmony_ci const args = ['-rf']; 881cb0ef41Sopenharmony_ci const options = { remove: { short: 'r', type: 'boolean' } }; 891cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, remove: true, f: true }, positionals: [] }; 901cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 911cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 921cb0ef41Sopenharmony_ci}); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_citest('handles short-option followed by its value', () => { 951cb0ef41Sopenharmony_ci const args = ['-fFILE']; 961cb0ef41Sopenharmony_ci const options = { foo: { short: 'f', type: 'string' } }; 971cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: 'FILE' }, positionals: [] }; 981cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 991cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 1001cb0ef41Sopenharmony_ci}); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_citest('Everything after a bare `--` is considered a positional argument', () => { 1031cb0ef41Sopenharmony_ci const args = ['--', 'barepositionals', 'mopositionals']; 1041cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null }, positionals: ['barepositionals', 'mopositionals'] }; 1051cb0ef41Sopenharmony_ci const result = parseArgs({ allowPositionals: true, args }); 1061cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('testing bare positionals')); 1071cb0ef41Sopenharmony_ci}); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_citest('args are true', () => { 1101cb0ef41Sopenharmony_ci const args = ['--foo', '--bar']; 1111cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true, bar: true }, positionals: [] }; 1121cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args }); 1131cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('args are true')); 1141cb0ef41Sopenharmony_ci}); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_citest('arg is true and positional is identified', () => { 1171cb0ef41Sopenharmony_ci const args = ['--foo=a', '--foo', 'b']; 1181cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, positionals: ['b'] }; 1191cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args }); 1201cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('arg is true and positional is identified')); 1211cb0ef41Sopenharmony_ci}); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_citest('args equals are passed `type: "string"`', () => { 1241cb0ef41Sopenharmony_ci const args = ['--so=wat']; 1251cb0ef41Sopenharmony_ci const options = { so: { type: 'string' } }; 1261cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, so: 'wat' }, positionals: [] }; 1271cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1281cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('arg value is passed')); 1291cb0ef41Sopenharmony_ci}); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_citest('when args include single dash then result stores dash as positional', () => { 1321cb0ef41Sopenharmony_ci const args = ['-']; 1331cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null }, positionals: ['-'] }; 1341cb0ef41Sopenharmony_ci const result = parseArgs({ allowPositionals: true, args }); 1351cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 1361cb0ef41Sopenharmony_ci}); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_citest('zero config args equals are parsed as if `type: "string"`', () => { 1391cb0ef41Sopenharmony_ci const args = ['--so=wat']; 1401cb0ef41Sopenharmony_ci const options = { }; 1411cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, so: 'wat' }, positionals: [] }; 1421cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 1431cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('arg value is passed')); 1441cb0ef41Sopenharmony_ci}); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_citest('same arg is passed twice `type: "string"` and last value is recorded', () => { 1471cb0ef41Sopenharmony_ci const args = ['--foo=a', '--foo', 'b']; 1481cb0ef41Sopenharmony_ci const options = { foo: { type: 'string' } }; 1491cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: 'b' }, positionals: [] }; 1501cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1511cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('last arg value is passed')); 1521cb0ef41Sopenharmony_ci}); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_citest('args equals pass string including more equals', () => { 1551cb0ef41Sopenharmony_ci const args = ['--so=wat=bing']; 1561cb0ef41Sopenharmony_ci const options = { so: { type: 'string' } }; 1571cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, so: 'wat=bing' }, positionals: [] }; 1581cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1591cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('arg value is passed')); 1601cb0ef41Sopenharmony_ci}); 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_citest('first arg passed for `type: "string"` and "multiple" is in array', () => { 1631cb0ef41Sopenharmony_ci const args = ['--foo=a']; 1641cb0ef41Sopenharmony_ci const options = { foo: { type: 'string', multiple: true } }; 1651cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: ['a'] }, positionals: [] }; 1661cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1671cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('first multiple in array')); 1681cb0ef41Sopenharmony_ci}); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_citest('args are passed `type: "string"` and "multiple"', () => { 1711cb0ef41Sopenharmony_ci const args = ['--foo=a', '--foo', 'b']; 1721cb0ef41Sopenharmony_ci const options = { 1731cb0ef41Sopenharmony_ci foo: { 1741cb0ef41Sopenharmony_ci type: 'string', 1751cb0ef41Sopenharmony_ci multiple: true, 1761cb0ef41Sopenharmony_ci }, 1771cb0ef41Sopenharmony_ci }; 1781cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: ['a', 'b'] }, positionals: [] }; 1791cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1801cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('both arg values are passed')); 1811cb0ef41Sopenharmony_ci}); 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_citest('when expecting `multiple:true` boolean option and option used multiple times then result includes array of ' + 1841cb0ef41Sopenharmony_ci 'booleans matching usage', () => { 1851cb0ef41Sopenharmony_ci const args = ['--foo', '--foo']; 1861cb0ef41Sopenharmony_ci const options = { 1871cb0ef41Sopenharmony_ci foo: { 1881cb0ef41Sopenharmony_ci type: 'boolean', 1891cb0ef41Sopenharmony_ci multiple: true, 1901cb0ef41Sopenharmony_ci }, 1911cb0ef41Sopenharmony_ci }; 1921cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: [true, true] }, positionals: [] }; 1931cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 1941cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 1951cb0ef41Sopenharmony_ci}); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_citest('order of option and positional does not matter (per README)', () => { 1981cb0ef41Sopenharmony_ci const args1 = ['--foo=bar', 'baz']; 1991cb0ef41Sopenharmony_ci const args2 = ['baz', '--foo=bar']; 2001cb0ef41Sopenharmony_ci const options = { foo: { type: 'string' } }; 2011cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: 'bar' }, positionals: ['baz'] }; 2021cb0ef41Sopenharmony_ci assert.deepStrictEqual( 2031cb0ef41Sopenharmony_ci parseArgs({ allowPositionals: true, args: args1, options }), 2041cb0ef41Sopenharmony_ci expected, 2051cb0ef41Sopenharmony_ci Error('option then positional') 2061cb0ef41Sopenharmony_ci ); 2071cb0ef41Sopenharmony_ci assert.deepStrictEqual( 2081cb0ef41Sopenharmony_ci parseArgs({ allowPositionals: true, args: args2, options }), 2091cb0ef41Sopenharmony_ci expected, 2101cb0ef41Sopenharmony_ci Error('positional then option') 2111cb0ef41Sopenharmony_ci ); 2121cb0ef41Sopenharmony_ci}); 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_citest('correct default args when use node -p', () => { 2151cb0ef41Sopenharmony_ci const holdArgv = process.argv; 2161cb0ef41Sopenharmony_ci process.argv = [process.argv0, '--foo']; 2171cb0ef41Sopenharmony_ci const holdExecArgv = process.execArgv; 2181cb0ef41Sopenharmony_ci process.execArgv = ['-p', '0']; 2191cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false }); 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 2221cb0ef41Sopenharmony_ci positionals: [] }; 2231cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 2241cb0ef41Sopenharmony_ci process.argv = holdArgv; 2251cb0ef41Sopenharmony_ci process.execArgv = holdExecArgv; 2261cb0ef41Sopenharmony_ci}); 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_citest('correct default args when use node --print', () => { 2291cb0ef41Sopenharmony_ci const holdArgv = process.argv; 2301cb0ef41Sopenharmony_ci process.argv = [process.argv0, '--foo']; 2311cb0ef41Sopenharmony_ci const holdExecArgv = process.execArgv; 2321cb0ef41Sopenharmony_ci process.execArgv = ['--print', '0']; 2331cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false }); 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 2361cb0ef41Sopenharmony_ci positionals: [] }; 2371cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 2381cb0ef41Sopenharmony_ci process.argv = holdArgv; 2391cb0ef41Sopenharmony_ci process.execArgv = holdExecArgv; 2401cb0ef41Sopenharmony_ci}); 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_citest('correct default args when use node -e', () => { 2431cb0ef41Sopenharmony_ci const holdArgv = process.argv; 2441cb0ef41Sopenharmony_ci process.argv = [process.argv0, '--foo']; 2451cb0ef41Sopenharmony_ci const holdExecArgv = process.execArgv; 2461cb0ef41Sopenharmony_ci process.execArgv = ['-e', '0']; 2471cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false }); 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 2501cb0ef41Sopenharmony_ci positionals: [] }; 2511cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 2521cb0ef41Sopenharmony_ci process.argv = holdArgv; 2531cb0ef41Sopenharmony_ci process.execArgv = holdExecArgv; 2541cb0ef41Sopenharmony_ci}); 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_citest('correct default args when use node --eval', () => { 2571cb0ef41Sopenharmony_ci const holdArgv = process.argv; 2581cb0ef41Sopenharmony_ci process.argv = [process.argv0, '--foo']; 2591cb0ef41Sopenharmony_ci const holdExecArgv = process.execArgv; 2601cb0ef41Sopenharmony_ci process.execArgv = ['--eval', '0']; 2611cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false }); 2621cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 2631cb0ef41Sopenharmony_ci positionals: [] }; 2641cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 2651cb0ef41Sopenharmony_ci process.argv = holdArgv; 2661cb0ef41Sopenharmony_ci process.execArgv = holdExecArgv; 2671cb0ef41Sopenharmony_ci}); 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_citest('correct default args when normal arguments', () => { 2701cb0ef41Sopenharmony_ci const holdArgv = process.argv; 2711cb0ef41Sopenharmony_ci process.argv = [process.argv0, 'script.js', '--foo']; 2721cb0ef41Sopenharmony_ci const holdExecArgv = process.execArgv; 2731cb0ef41Sopenharmony_ci process.execArgv = []; 2741cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false }); 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 2771cb0ef41Sopenharmony_ci positionals: [] }; 2781cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 2791cb0ef41Sopenharmony_ci process.argv = holdArgv; 2801cb0ef41Sopenharmony_ci process.execArgv = holdExecArgv; 2811cb0ef41Sopenharmony_ci}); 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_citest('excess leading dashes on options are retained', () => { 2841cb0ef41Sopenharmony_ci // Enforce a design decision for an edge case. 2851cb0ef41Sopenharmony_ci const args = ['---triple']; 2861cb0ef41Sopenharmony_ci const options = { }; 2871cb0ef41Sopenharmony_ci const expected = { 2881cb0ef41Sopenharmony_ci values: { '__proto__': null, '-triple': true }, 2891cb0ef41Sopenharmony_ci positionals: [] 2901cb0ef41Sopenharmony_ci }; 2911cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 2921cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected, Error('excess option dashes are retained')); 2931cb0ef41Sopenharmony_ci}); 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_citest('positional arguments are allowed by default in strict:false', () => { 2961cb0ef41Sopenharmony_ci const args = ['foo']; 2971cb0ef41Sopenharmony_ci const options = { }; 2981cb0ef41Sopenharmony_ci const expected = { 2991cb0ef41Sopenharmony_ci values: { __proto__: null }, 3001cb0ef41Sopenharmony_ci positionals: ['foo'] 3011cb0ef41Sopenharmony_ci }; 3021cb0ef41Sopenharmony_ci const result = parseArgs({ strict: false, args, options }); 3031cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 3041cb0ef41Sopenharmony_ci}); 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_citest('positional arguments may be explicitly disallowed in strict:false', () => { 3071cb0ef41Sopenharmony_ci const args = ['foo']; 3081cb0ef41Sopenharmony_ci const options = { }; 3091cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ strict: false, allowPositionals: false, args, options }); }, { 3101cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL' 3111cb0ef41Sopenharmony_ci }); 3121cb0ef41Sopenharmony_ci}); 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci// Test bad inputs 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_citest('invalid argument passed for options', () => { 3171cb0ef41Sopenharmony_ci const args = ['--so=wat']; 3181cb0ef41Sopenharmony_ci const options = 'bad value'; 3191cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3201cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 3211cb0ef41Sopenharmony_ci }); 3221cb0ef41Sopenharmony_ci}); 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_citest('type property missing for option then throw', () => { 3251cb0ef41Sopenharmony_ci const knownOptions = { foo: { } }; 3261cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ options: knownOptions }); }, { 3271cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 3281cb0ef41Sopenharmony_ci }); 3291cb0ef41Sopenharmony_ci}); 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_citest('boolean passed to "type" option', () => { 3321cb0ef41Sopenharmony_ci const args = ['--so=wat']; 3331cb0ef41Sopenharmony_ci const options = { foo: { type: true } }; 3341cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3351cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 3361cb0ef41Sopenharmony_ci }); 3371cb0ef41Sopenharmony_ci}); 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_citest('invalid union value passed to "type" option', () => { 3401cb0ef41Sopenharmony_ci const args = ['--so=wat']; 3411cb0ef41Sopenharmony_ci const options = { foo: { type: 'str' } }; 3421cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3431cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 3441cb0ef41Sopenharmony_ci }); 3451cb0ef41Sopenharmony_ci}); 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci// Test strict mode 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_citest('unknown long option --bar', () => { 3501cb0ef41Sopenharmony_ci const args = ['--foo', '--bar']; 3511cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 3521cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3531cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' 3541cb0ef41Sopenharmony_ci }); 3551cb0ef41Sopenharmony_ci}); 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_citest('unknown short option -b', () => { 3581cb0ef41Sopenharmony_ci const args = ['--foo', '-b']; 3591cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 3601cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3611cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' 3621cb0ef41Sopenharmony_ci }); 3631cb0ef41Sopenharmony_ci}); 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_citest('unknown option -r in short option group -bar', () => { 3661cb0ef41Sopenharmony_ci const args = ['-bar']; 3671cb0ef41Sopenharmony_ci const options = { b: { type: 'boolean' }, a: { type: 'boolean' } }; 3681cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3691cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' 3701cb0ef41Sopenharmony_ci }); 3711cb0ef41Sopenharmony_ci}); 3721cb0ef41Sopenharmony_ci 3731cb0ef41Sopenharmony_citest('unknown option with explicit value', () => { 3741cb0ef41Sopenharmony_ci const args = ['--foo', '--bar=baz']; 3751cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 3761cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3771cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' 3781cb0ef41Sopenharmony_ci }); 3791cb0ef41Sopenharmony_ci}); 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_citest('unexpected positional', () => { 3821cb0ef41Sopenharmony_ci const args = ['foo']; 3831cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 3841cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3851cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL' 3861cb0ef41Sopenharmony_ci }); 3871cb0ef41Sopenharmony_ci}); 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_citest('unexpected positional after --', () => { 3901cb0ef41Sopenharmony_ci const args = ['--', 'foo']; 3911cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 3921cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 3931cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL' 3941cb0ef41Sopenharmony_ci }); 3951cb0ef41Sopenharmony_ci}); 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_citest('-- by itself is not a positional', () => { 3981cb0ef41Sopenharmony_ci const args = ['--foo', '--']; 3991cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 4001cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 4011cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, foo: true }, 4021cb0ef41Sopenharmony_ci positionals: [] }; 4031cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 4041cb0ef41Sopenharmony_ci}); 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_citest('string option used as boolean', () => { 4071cb0ef41Sopenharmony_ci const args = ['--foo']; 4081cb0ef41Sopenharmony_ci const options = { foo: { type: 'string' } }; 4091cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 4101cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 4111cb0ef41Sopenharmony_ci }); 4121cb0ef41Sopenharmony_ci}); 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_citest('boolean option used with value', () => { 4151cb0ef41Sopenharmony_ci const args = ['--foo=bar']; 4161cb0ef41Sopenharmony_ci const options = { foo: { type: 'boolean' } }; 4171cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 4181cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 4191cb0ef41Sopenharmony_ci }); 4201cb0ef41Sopenharmony_ci}); 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_citest('invalid short option length', () => { 4231cb0ef41Sopenharmony_ci const args = []; 4241cb0ef41Sopenharmony_ci const options = { foo: { short: 'fo', type: 'boolean' } }; 4251cb0ef41Sopenharmony_ci assert.throws(() => { parseArgs({ args, options }); }, { 4261cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE' 4271cb0ef41Sopenharmony_ci }); 4281cb0ef41Sopenharmony_ci}); 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_citest('null prototype: when no options then values.toString is undefined', () => { 4311cb0ef41Sopenharmony_ci const result = parseArgs({ args: [] }); 4321cb0ef41Sopenharmony_ci assert.strictEqual(result.values.toString, undefined); 4331cb0ef41Sopenharmony_ci}); 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_citest('null prototype: when --toString then values.toString is true', () => { 4361cb0ef41Sopenharmony_ci const args = ['--toString']; 4371cb0ef41Sopenharmony_ci const options = { toString: { type: 'boolean' } }; 4381cb0ef41Sopenharmony_ci const expectedResult = { values: { __proto__: null, toString: true }, positionals: [] }; 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 4411cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expectedResult); 4421cb0ef41Sopenharmony_ci}); 4431cb0ef41Sopenharmony_ci 4441cb0ef41Sopenharmony_ciconst candidateGreedyOptions = [ 4451cb0ef41Sopenharmony_ci '', 4461cb0ef41Sopenharmony_ci '-', 4471cb0ef41Sopenharmony_ci '--', 4481cb0ef41Sopenharmony_ci 'abc', 4491cb0ef41Sopenharmony_ci '123', 4501cb0ef41Sopenharmony_ci '-s', 4511cb0ef41Sopenharmony_ci '--foo', 4521cb0ef41Sopenharmony_ci]; 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_cicandidateGreedyOptions.forEach((value) => { 4551cb0ef41Sopenharmony_ci test(`greedy: when short option with value '${value}' then eaten`, () => { 4561cb0ef41Sopenharmony_ci const args = ['-w', value]; 4571cb0ef41Sopenharmony_ci const options = { with: { type: 'string', short: 'w' } }; 4581cb0ef41Sopenharmony_ci const expectedResult = { values: { __proto__: null, with: value }, positionals: [] }; 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci const result = parseArgs({ args, options, strict: false }); 4611cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expectedResult); 4621cb0ef41Sopenharmony_ci }); 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci test(`greedy: when long option with value '${value}' then eaten`, () => { 4651cb0ef41Sopenharmony_ci const args = ['--with', value]; 4661cb0ef41Sopenharmony_ci const options = { with: { type: 'string', short: 'w' } }; 4671cb0ef41Sopenharmony_ci const expectedResult = { values: { __proto__: null, with: value }, positionals: [] }; 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_ci const result = parseArgs({ args, options, strict: false }); 4701cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expectedResult); 4711cb0ef41Sopenharmony_ci }); 4721cb0ef41Sopenharmony_ci}); 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_citest('strict: when candidate option value is plain text then does not throw', () => { 4751cb0ef41Sopenharmony_ci const args = ['--with', 'abc']; 4761cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 4771cb0ef41Sopenharmony_ci const expectedResult = { values: { __proto__: null, with: 'abc' }, positionals: [] }; 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci const result = parseArgs({ args, options, strict: true }); 4801cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expectedResult); 4811cb0ef41Sopenharmony_ci}); 4821cb0ef41Sopenharmony_ci 4831cb0ef41Sopenharmony_citest("strict: when candidate option value is '-' then does not throw", () => { 4841cb0ef41Sopenharmony_ci const args = ['--with', '-']; 4851cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 4861cb0ef41Sopenharmony_ci const expectedResult = { values: { __proto__: null, with: '-' }, positionals: [] }; 4871cb0ef41Sopenharmony_ci 4881cb0ef41Sopenharmony_ci const result = parseArgs({ args, options, strict: true }); 4891cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expectedResult); 4901cb0ef41Sopenharmony_ci}); 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_citest("strict: when candidate option value is '--' then throws", () => { 4931cb0ef41Sopenharmony_ci const args = ['--with', '--']; 4941cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 4951cb0ef41Sopenharmony_ci 4961cb0ef41Sopenharmony_ci assert.throws(() => { 4971cb0ef41Sopenharmony_ci parseArgs({ args, options }); 4981cb0ef41Sopenharmony_ci }, { 4991cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 5001cb0ef41Sopenharmony_ci }); 5011cb0ef41Sopenharmony_ci}); 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_citest('strict: when candidate option value is short option then throws', () => { 5041cb0ef41Sopenharmony_ci const args = ['--with', '-a']; 5051cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 5061cb0ef41Sopenharmony_ci 5071cb0ef41Sopenharmony_ci assert.throws(() => { 5081cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5091cb0ef41Sopenharmony_ci }, { 5101cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 5111cb0ef41Sopenharmony_ci }); 5121cb0ef41Sopenharmony_ci}); 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_citest('strict: when candidate option value is short option digit then throws', () => { 5151cb0ef41Sopenharmony_ci const args = ['--with', '-1']; 5161cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci assert.throws(() => { 5191cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5201cb0ef41Sopenharmony_ci }, { 5211cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 5221cb0ef41Sopenharmony_ci }); 5231cb0ef41Sopenharmony_ci}); 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_citest('strict: when candidate option value is long option then throws', () => { 5261cb0ef41Sopenharmony_ci const args = ['--with', '--foo']; 5271cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ci assert.throws(() => { 5301cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5311cb0ef41Sopenharmony_ci }, { 5321cb0ef41Sopenharmony_ci code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' 5331cb0ef41Sopenharmony_ci }); 5341cb0ef41Sopenharmony_ci}); 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_citest('strict: when short option and suspect value then throws with short option in error message', () => { 5371cb0ef41Sopenharmony_ci const args = ['-w', '--foo']; 5381cb0ef41Sopenharmony_ci const options = { with: { type: 'string', short: 'w' } }; 5391cb0ef41Sopenharmony_ci 5401cb0ef41Sopenharmony_ci assert.throws(() => { 5411cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5421cb0ef41Sopenharmony_ci }, /for '-w'/ 5431cb0ef41Sopenharmony_ci ); 5441cb0ef41Sopenharmony_ci}); 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_citest('strict: when long option and suspect value then throws with long option in error message', () => { 5471cb0ef41Sopenharmony_ci const args = ['--with', '--foo']; 5481cb0ef41Sopenharmony_ci const options = { with: { type: 'string' } }; 5491cb0ef41Sopenharmony_ci 5501cb0ef41Sopenharmony_ci assert.throws(() => { 5511cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5521cb0ef41Sopenharmony_ci }, /for '--with'/ 5531cb0ef41Sopenharmony_ci ); 5541cb0ef41Sopenharmony_ci}); 5551cb0ef41Sopenharmony_ci 5561cb0ef41Sopenharmony_citest('strict: when short option and suspect value then throws with whole expected message', () => { 5571cb0ef41Sopenharmony_ci const args = ['-w', '--foo']; 5581cb0ef41Sopenharmony_ci const options = { with: { type: 'string', short: 'w' } }; 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ci try { 5611cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5621cb0ef41Sopenharmony_ci } catch (err) { 5631cb0ef41Sopenharmony_ci console.info(err.message); 5641cb0ef41Sopenharmony_ci } 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci assert.throws(() => { 5671cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5681cb0ef41Sopenharmony_ci }, /To specify an option argument starting with a dash use '--with=-XYZ' or '-w-XYZ'/ 5691cb0ef41Sopenharmony_ci ); 5701cb0ef41Sopenharmony_ci}); 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_citest('strict: when long option and suspect value then throws with whole expected message', () => { 5731cb0ef41Sopenharmony_ci const args = ['--with', '--foo']; 5741cb0ef41Sopenharmony_ci const options = { with: { type: 'string', short: 'w' } }; 5751cb0ef41Sopenharmony_ci 5761cb0ef41Sopenharmony_ci assert.throws(() => { 5771cb0ef41Sopenharmony_ci parseArgs({ args, options }); 5781cb0ef41Sopenharmony_ci }, /To specify an option argument starting with a dash use '--with=-XYZ'/ 5791cb0ef41Sopenharmony_ci ); 5801cb0ef41Sopenharmony_ci}); 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_citest('tokens: positional', () => { 5831cb0ef41Sopenharmony_ci const args = ['one']; 5841cb0ef41Sopenharmony_ci const expectedTokens = [ 5851cb0ef41Sopenharmony_ci { kind: 'positional', index: 0, value: 'one' }, 5861cb0ef41Sopenharmony_ci ]; 5871cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 5881cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 5891cb0ef41Sopenharmony_ci}); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_citest('tokens: -- followed by option-like', () => { 5921cb0ef41Sopenharmony_ci const args = ['--', '--foo']; 5931cb0ef41Sopenharmony_ci const expectedTokens = [ 5941cb0ef41Sopenharmony_ci { kind: 'option-terminator', index: 0 }, 5951cb0ef41Sopenharmony_ci { kind: 'positional', index: 1, value: '--foo' }, 5961cb0ef41Sopenharmony_ci ]; 5971cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 5981cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 5991cb0ef41Sopenharmony_ci}); 6001cb0ef41Sopenharmony_ci 6011cb0ef41Sopenharmony_citest('tokens: strict:true boolean short', () => { 6021cb0ef41Sopenharmony_ci const args = ['-f']; 6031cb0ef41Sopenharmony_ci const options = { 6041cb0ef41Sopenharmony_ci file: { short: 'f', type: 'boolean' } 6051cb0ef41Sopenharmony_ci }; 6061cb0ef41Sopenharmony_ci const expectedTokens = [ 6071cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '-f', 6081cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6091cb0ef41Sopenharmony_ci ]; 6101cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, args, options, tokens: true }); 6111cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6121cb0ef41Sopenharmony_ci}); 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_citest('tokens: strict:true boolean long', () => { 6151cb0ef41Sopenharmony_ci const args = ['--file']; 6161cb0ef41Sopenharmony_ci const options = { 6171cb0ef41Sopenharmony_ci file: { short: 'f', type: 'boolean' } 6181cb0ef41Sopenharmony_ci }; 6191cb0ef41Sopenharmony_ci const expectedTokens = [ 6201cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 6211cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6221cb0ef41Sopenharmony_ci ]; 6231cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, args, options, tokens: true }); 6241cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6251cb0ef41Sopenharmony_ci}); 6261cb0ef41Sopenharmony_ci 6271cb0ef41Sopenharmony_citest('tokens: strict:false boolean short', () => { 6281cb0ef41Sopenharmony_ci const args = ['-f']; 6291cb0ef41Sopenharmony_ci const expectedTokens = [ 6301cb0ef41Sopenharmony_ci { kind: 'option', name: 'f', rawName: '-f', 6311cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6321cb0ef41Sopenharmony_ci ]; 6331cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 6341cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6351cb0ef41Sopenharmony_ci}); 6361cb0ef41Sopenharmony_ci 6371cb0ef41Sopenharmony_citest('tokens: strict:false boolean long', () => { 6381cb0ef41Sopenharmony_ci const args = ['--file']; 6391cb0ef41Sopenharmony_ci const expectedTokens = [ 6401cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 6411cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6421cb0ef41Sopenharmony_ci ]; 6431cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 6441cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6451cb0ef41Sopenharmony_ci}); 6461cb0ef41Sopenharmony_ci 6471cb0ef41Sopenharmony_citest('tokens: strict:false boolean option group', () => { 6481cb0ef41Sopenharmony_ci const args = ['-ab']; 6491cb0ef41Sopenharmony_ci const expectedTokens = [ 6501cb0ef41Sopenharmony_ci { kind: 'option', name: 'a', rawName: '-a', 6511cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6521cb0ef41Sopenharmony_ci { kind: 'option', name: 'b', rawName: '-b', 6531cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6541cb0ef41Sopenharmony_ci ]; 6551cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 6561cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6571cb0ef41Sopenharmony_ci}); 6581cb0ef41Sopenharmony_ci 6591cb0ef41Sopenharmony_citest('tokens: strict:false boolean option group with repeated option', () => { 6601cb0ef41Sopenharmony_ci // Also positional to check index correct after grouop 6611cb0ef41Sopenharmony_ci const args = ['-aa', 'pos']; 6621cb0ef41Sopenharmony_ci const expectedTokens = [ 6631cb0ef41Sopenharmony_ci { kind: 'option', name: 'a', rawName: '-a', 6641cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6651cb0ef41Sopenharmony_ci { kind: 'option', name: 'a', rawName: '-a', 6661cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 6671cb0ef41Sopenharmony_ci { kind: 'positional', index: 1, value: 'pos' }, 6681cb0ef41Sopenharmony_ci ]; 6691cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, allowPositionals: true, args, tokens: true }); 6701cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6711cb0ef41Sopenharmony_ci}); 6721cb0ef41Sopenharmony_ci 6731cb0ef41Sopenharmony_citest('tokens: strict:true string short with value after space', () => { 6741cb0ef41Sopenharmony_ci // Also positional to check index correct after out-of-line. 6751cb0ef41Sopenharmony_ci const args = ['-f', 'bar', 'ppp']; 6761cb0ef41Sopenharmony_ci const options = { 6771cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 6781cb0ef41Sopenharmony_ci }; 6791cb0ef41Sopenharmony_ci const expectedTokens = [ 6801cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '-f', 6811cb0ef41Sopenharmony_ci index: 0, value: 'bar', inlineValue: false }, 6821cb0ef41Sopenharmony_ci { kind: 'positional', index: 2, value: 'ppp' }, 6831cb0ef41Sopenharmony_ci ]; 6841cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, allowPositionals: true, args, options, tokens: true }); 6851cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6861cb0ef41Sopenharmony_ci}); 6871cb0ef41Sopenharmony_ci 6881cb0ef41Sopenharmony_citest('tokens: strict:true string short with value inline', () => { 6891cb0ef41Sopenharmony_ci const args = ['-fBAR']; 6901cb0ef41Sopenharmony_ci const options = { 6911cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 6921cb0ef41Sopenharmony_ci }; 6931cb0ef41Sopenharmony_ci const expectedTokens = [ 6941cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '-f', 6951cb0ef41Sopenharmony_ci index: 0, value: 'BAR', inlineValue: true }, 6961cb0ef41Sopenharmony_ci ]; 6971cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, args, options, tokens: true }); 6981cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 6991cb0ef41Sopenharmony_ci}); 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_citest('tokens: strict:false string short missing value', () => { 7021cb0ef41Sopenharmony_ci const args = ['-f']; 7031cb0ef41Sopenharmony_ci const options = { 7041cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 7051cb0ef41Sopenharmony_ci }; 7061cb0ef41Sopenharmony_ci const expectedTokens = [ 7071cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '-f', 7081cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 7091cb0ef41Sopenharmony_ci ]; 7101cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, options, tokens: true }); 7111cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7121cb0ef41Sopenharmony_ci}); 7131cb0ef41Sopenharmony_ci 7141cb0ef41Sopenharmony_citest('tokens: strict:true string long with value after space', () => { 7151cb0ef41Sopenharmony_ci // Also positional to check index correct after out-of-line. 7161cb0ef41Sopenharmony_ci const args = ['--file', 'bar', 'ppp']; 7171cb0ef41Sopenharmony_ci const options = { 7181cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 7191cb0ef41Sopenharmony_ci }; 7201cb0ef41Sopenharmony_ci const expectedTokens = [ 7211cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 7221cb0ef41Sopenharmony_ci index: 0, value: 'bar', inlineValue: false }, 7231cb0ef41Sopenharmony_ci { kind: 'positional', index: 2, value: 'ppp' }, 7241cb0ef41Sopenharmony_ci ]; 7251cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, allowPositionals: true, args, options, tokens: true }); 7261cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7271cb0ef41Sopenharmony_ci}); 7281cb0ef41Sopenharmony_ci 7291cb0ef41Sopenharmony_citest('tokens: strict:true string long with value inline', () => { 7301cb0ef41Sopenharmony_ci // Also positional to check index correct after out-of-line. 7311cb0ef41Sopenharmony_ci const args = ['--file=bar', 'pos']; 7321cb0ef41Sopenharmony_ci const options = { 7331cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 7341cb0ef41Sopenharmony_ci }; 7351cb0ef41Sopenharmony_ci const expectedTokens = [ 7361cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 7371cb0ef41Sopenharmony_ci index: 0, value: 'bar', inlineValue: true }, 7381cb0ef41Sopenharmony_ci { kind: 'positional', index: 1, value: 'pos' }, 7391cb0ef41Sopenharmony_ci ]; 7401cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, allowPositionals: true, args, options, tokens: true }); 7411cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7421cb0ef41Sopenharmony_ci}); 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_citest('tokens: strict:false string long with value inline', () => { 7451cb0ef41Sopenharmony_ci const args = ['--file=bar']; 7461cb0ef41Sopenharmony_ci const expectedTokens = [ 7471cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 7481cb0ef41Sopenharmony_ci index: 0, value: 'bar', inlineValue: true }, 7491cb0ef41Sopenharmony_ci ]; 7501cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 7511cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7521cb0ef41Sopenharmony_ci}); 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_citest('tokens: strict:false string long missing value', () => { 7551cb0ef41Sopenharmony_ci const args = ['--file']; 7561cb0ef41Sopenharmony_ci const options = { 7571cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' } 7581cb0ef41Sopenharmony_ci }; 7591cb0ef41Sopenharmony_ci const expectedTokens = [ 7601cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 7611cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 7621cb0ef41Sopenharmony_ci ]; 7631cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, options, tokens: true }); 7641cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7651cb0ef41Sopenharmony_ci}); 7661cb0ef41Sopenharmony_ci 7671cb0ef41Sopenharmony_citest('tokens: strict:true complex option group with value after space', () => { 7681cb0ef41Sopenharmony_ci // Also positional to check index correct afterwards. 7691cb0ef41Sopenharmony_ci const args = ['-ab', 'c', 'pos']; 7701cb0ef41Sopenharmony_ci const options = { 7711cb0ef41Sopenharmony_ci alpha: { short: 'a', type: 'boolean' }, 7721cb0ef41Sopenharmony_ci beta: { short: 'b', type: 'string' }, 7731cb0ef41Sopenharmony_ci }; 7741cb0ef41Sopenharmony_ci const expectedTokens = [ 7751cb0ef41Sopenharmony_ci { kind: 'option', name: 'alpha', rawName: '-a', 7761cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 7771cb0ef41Sopenharmony_ci { kind: 'option', name: 'beta', rawName: '-b', 7781cb0ef41Sopenharmony_ci index: 0, value: 'c', inlineValue: false }, 7791cb0ef41Sopenharmony_ci { kind: 'positional', index: 2, value: 'pos' }, 7801cb0ef41Sopenharmony_ci ]; 7811cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, allowPositionals: true, args, options, tokens: true }); 7821cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 7831cb0ef41Sopenharmony_ci}); 7841cb0ef41Sopenharmony_ci 7851cb0ef41Sopenharmony_citest('tokens: strict:true complex option group with inline value', () => { 7861cb0ef41Sopenharmony_ci // Also positional to check index correct afterwards. 7871cb0ef41Sopenharmony_ci const args = ['-abc', 'pos']; 7881cb0ef41Sopenharmony_ci const options = { 7891cb0ef41Sopenharmony_ci alpha: { short: 'a', type: 'boolean' }, 7901cb0ef41Sopenharmony_ci beta: { short: 'b', type: 'string' }, 7911cb0ef41Sopenharmony_ci }; 7921cb0ef41Sopenharmony_ci const expectedTokens = [ 7931cb0ef41Sopenharmony_ci { kind: 'option', name: 'alpha', rawName: '-a', 7941cb0ef41Sopenharmony_ci index: 0, value: undefined, inlineValue: undefined }, 7951cb0ef41Sopenharmony_ci { kind: 'option', name: 'beta', rawName: '-b', 7961cb0ef41Sopenharmony_ci index: 0, value: 'c', inlineValue: true }, 7971cb0ef41Sopenharmony_ci { kind: 'positional', index: 1, value: 'pos' }, 7981cb0ef41Sopenharmony_ci ]; 7991cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: true, allowPositionals: true, args, options, tokens: true }); 8001cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 8011cb0ef41Sopenharmony_ci}); 8021cb0ef41Sopenharmony_ci 8031cb0ef41Sopenharmony_citest('tokens: strict:false with single dashes', () => { 8041cb0ef41Sopenharmony_ci const args = ['--file', '-', '-']; 8051cb0ef41Sopenharmony_ci const options = { 8061cb0ef41Sopenharmony_ci file: { short: 'f', type: 'string' }, 8071cb0ef41Sopenharmony_ci }; 8081cb0ef41Sopenharmony_ci const expectedTokens = [ 8091cb0ef41Sopenharmony_ci { kind: 'option', name: 'file', rawName: '--file', 8101cb0ef41Sopenharmony_ci index: 0, value: '-', inlineValue: false }, 8111cb0ef41Sopenharmony_ci { kind: 'positional', index: 2, value: '-' }, 8121cb0ef41Sopenharmony_ci ]; 8131cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, options, tokens: true }); 8141cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 8151cb0ef41Sopenharmony_ci}); 8161cb0ef41Sopenharmony_ci 8171cb0ef41Sopenharmony_citest('tokens: strict:false with -- --', () => { 8181cb0ef41Sopenharmony_ci const args = ['--', '--']; 8191cb0ef41Sopenharmony_ci const expectedTokens = [ 8201cb0ef41Sopenharmony_ci { kind: 'option-terminator', index: 0 }, 8211cb0ef41Sopenharmony_ci { kind: 'positional', index: 1, value: '--' }, 8221cb0ef41Sopenharmony_ci ]; 8231cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ strict: false, args, tokens: true }); 8241cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 8251cb0ef41Sopenharmony_ci}); 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_citest('default must be a boolean when option type is boolean', () => { 8281cb0ef41Sopenharmony_ci const args = []; 8291cb0ef41Sopenharmony_ci const options = { alpha: { type: 'boolean', default: 'not a boolean' } }; 8301cb0ef41Sopenharmony_ci assert.throws(() => { 8311cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8321cb0ef41Sopenharmony_ci }, /"options\.alpha\.default" property must be of type boolean/ 8331cb0ef41Sopenharmony_ci ); 8341cb0ef41Sopenharmony_ci}); 8351cb0ef41Sopenharmony_ci 8361cb0ef41Sopenharmony_citest('default must accept undefined value', () => { 8371cb0ef41Sopenharmony_ci const args = []; 8381cb0ef41Sopenharmony_ci const options = { alpha: { type: 'boolean', default: undefined } }; 8391cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 8401cb0ef41Sopenharmony_ci const expected = { 8411cb0ef41Sopenharmony_ci values: { 8421cb0ef41Sopenharmony_ci __proto__: null, 8431cb0ef41Sopenharmony_ci }, 8441cb0ef41Sopenharmony_ci positionals: [] 8451cb0ef41Sopenharmony_ci }; 8461cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 8471cb0ef41Sopenharmony_ci}); 8481cb0ef41Sopenharmony_ci 8491cb0ef41Sopenharmony_citest('default must be a boolean array when option type is boolean and multiple', () => { 8501cb0ef41Sopenharmony_ci const args = []; 8511cb0ef41Sopenharmony_ci const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } }; 8521cb0ef41Sopenharmony_ci assert.throws(() => { 8531cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8541cb0ef41Sopenharmony_ci }, /"options\.alpha\.default" property must be an instance of Array/ 8551cb0ef41Sopenharmony_ci ); 8561cb0ef41Sopenharmony_ci}); 8571cb0ef41Sopenharmony_ci 8581cb0ef41Sopenharmony_citest('default must be a boolean array when option type is string and multiple is true', () => { 8591cb0ef41Sopenharmony_ci const args = []; 8601cb0ef41Sopenharmony_ci const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } }; 8611cb0ef41Sopenharmony_ci assert.throws(() => { 8621cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8631cb0ef41Sopenharmony_ci }, /"options\.alpha\.default\[2\]" property must be of type boolean/ 8641cb0ef41Sopenharmony_ci ); 8651cb0ef41Sopenharmony_ci}); 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_citest('default must be a string when option type is string', () => { 8681cb0ef41Sopenharmony_ci const args = []; 8691cb0ef41Sopenharmony_ci const options = { alpha: { type: 'string', default: true } }; 8701cb0ef41Sopenharmony_ci assert.throws(() => { 8711cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8721cb0ef41Sopenharmony_ci }, /"options\.alpha\.default" property must be of type string/ 8731cb0ef41Sopenharmony_ci ); 8741cb0ef41Sopenharmony_ci}); 8751cb0ef41Sopenharmony_ci 8761cb0ef41Sopenharmony_citest('default must be an array when option type is string and multiple is true', () => { 8771cb0ef41Sopenharmony_ci const args = []; 8781cb0ef41Sopenharmony_ci const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } }; 8791cb0ef41Sopenharmony_ci assert.throws(() => { 8801cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8811cb0ef41Sopenharmony_ci }, /"options\.alpha\.default" property must be an instance of Array/ 8821cb0ef41Sopenharmony_ci ); 8831cb0ef41Sopenharmony_ci}); 8841cb0ef41Sopenharmony_ci 8851cb0ef41Sopenharmony_citest('default must be a string array when option type is string and multiple is true', () => { 8861cb0ef41Sopenharmony_ci const args = []; 8871cb0ef41Sopenharmony_ci const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } }; 8881cb0ef41Sopenharmony_ci assert.throws(() => { 8891cb0ef41Sopenharmony_ci parseArgs({ args, options }); 8901cb0ef41Sopenharmony_ci }, /"options\.alpha\.default\[1\]" property must be of type string/ 8911cb0ef41Sopenharmony_ci ); 8921cb0ef41Sopenharmony_ci}); 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_citest('default accepted input when multiple is true', () => { 8951cb0ef41Sopenharmony_ci const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr']; 8961cb0ef41Sopenharmony_ci const options = { 8971cb0ef41Sopenharmony_ci inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, 8981cb0ef41Sopenharmony_ci emptyStringArr: { type: 'string', multiple: true, default: [] }, 8991cb0ef41Sopenharmony_ci fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, 9001cb0ef41Sopenharmony_ci inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, 9011cb0ef41Sopenharmony_ci emptyBoolArr: { type: 'boolean', multiple: true, default: [] }, 9021cb0ef41Sopenharmony_ci fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, 9031cb0ef41Sopenharmony_ci }; 9041cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, 9051cb0ef41Sopenharmony_ci inputStringArr: ['c', 'd'], 9061cb0ef41Sopenharmony_ci inputBoolArr: [true, true], 9071cb0ef41Sopenharmony_ci emptyStringArr: [], 9081cb0ef41Sopenharmony_ci fullStringArr: ['a', 'b'], 9091cb0ef41Sopenharmony_ci emptyBoolArr: [], 9101cb0ef41Sopenharmony_ci fullBoolArr: [false, true, false] }, 9111cb0ef41Sopenharmony_ci positionals: [] }; 9121cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 9131cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 9141cb0ef41Sopenharmony_ci}); 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_citest('when default is set, the option must be added as result', () => { 9171cb0ef41Sopenharmony_ci const args = []; 9181cb0ef41Sopenharmony_ci const options = { 9191cb0ef41Sopenharmony_ci a: { type: 'string', default: 'HELLO' }, 9201cb0ef41Sopenharmony_ci b: { type: 'boolean', default: false }, 9211cb0ef41Sopenharmony_ci c: { type: 'boolean', default: true } 9221cb0ef41Sopenharmony_ci }; 9231cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] }; 9241cb0ef41Sopenharmony_ci 9251cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 9261cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 9271cb0ef41Sopenharmony_ci}); 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_citest('when default is set, the args value takes precedence', () => { 9301cb0ef41Sopenharmony_ci const args = ['--a', 'WORLD', '--b', '-c']; 9311cb0ef41Sopenharmony_ci const options = { 9321cb0ef41Sopenharmony_ci a: { type: 'string', default: 'HELLO' }, 9331cb0ef41Sopenharmony_ci b: { type: 'boolean', default: false }, 9341cb0ef41Sopenharmony_ci c: { type: 'boolean', default: true } 9351cb0ef41Sopenharmony_ci }; 9361cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] }; 9371cb0ef41Sopenharmony_ci 9381cb0ef41Sopenharmony_ci const result = parseArgs({ args, options }); 9391cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 9401cb0ef41Sopenharmony_ci}); 9411cb0ef41Sopenharmony_ci 9421cb0ef41Sopenharmony_citest('tokens should not include the default options', () => { 9431cb0ef41Sopenharmony_ci const args = []; 9441cb0ef41Sopenharmony_ci const options = { 9451cb0ef41Sopenharmony_ci a: { type: 'string', default: 'HELLO' }, 9461cb0ef41Sopenharmony_ci b: { type: 'boolean', default: false }, 9471cb0ef41Sopenharmony_ci c: { type: 'boolean', default: true } 9481cb0ef41Sopenharmony_ci }; 9491cb0ef41Sopenharmony_ci 9501cb0ef41Sopenharmony_ci const expectedTokens = []; 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ args, options, tokens: true }); 9531cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 9541cb0ef41Sopenharmony_ci}); 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_citest('tokens:true should not include the default options after the args input', () => { 9571cb0ef41Sopenharmony_ci const args = ['--z', 'zero', 'positional-item']; 9581cb0ef41Sopenharmony_ci const options = { 9591cb0ef41Sopenharmony_ci z: { type: 'string' }, 9601cb0ef41Sopenharmony_ci a: { type: 'string', default: 'HELLO' }, 9611cb0ef41Sopenharmony_ci b: { type: 'boolean', default: false }, 9621cb0ef41Sopenharmony_ci c: { type: 'boolean', default: true } 9631cb0ef41Sopenharmony_ci }; 9641cb0ef41Sopenharmony_ci 9651cb0ef41Sopenharmony_ci const expectedTokens = [ 9661cb0ef41Sopenharmony_ci { kind: 'option', name: 'z', rawName: '--z', index: 0, value: 'zero', inlineValue: false }, 9671cb0ef41Sopenharmony_ci { kind: 'positional', index: 2, value: 'positional-item' }, 9681cb0ef41Sopenharmony_ci ]; 9691cb0ef41Sopenharmony_ci 9701cb0ef41Sopenharmony_ci const { tokens } = parseArgs({ args, options, tokens: true, allowPositionals: true }); 9711cb0ef41Sopenharmony_ci assert.deepStrictEqual(tokens, expectedTokens); 9721cb0ef41Sopenharmony_ci}); 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_citest('proto as default value must be ignored', () => { 9751cb0ef41Sopenharmony_ci const args = []; 9761cb0ef41Sopenharmony_ci const options = Object.create(null); 9771cb0ef41Sopenharmony_ci 9781cb0ef41Sopenharmony_ci // eslint-disable-next-line no-proto 9791cb0ef41Sopenharmony_ci options.__proto__ = { type: 'string', default: 'HELLO' }; 9801cb0ef41Sopenharmony_ci 9811cb0ef41Sopenharmony_ci const result = parseArgs({ args, options, allowPositionals: true }); 9821cb0ef41Sopenharmony_ci const expected = { values: { __proto__: null }, positionals: [] }; 9831cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 9841cb0ef41Sopenharmony_ci}); 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ci 9871cb0ef41Sopenharmony_citest('multiple as false should expect a String', () => { 9881cb0ef41Sopenharmony_ci const args = []; 9891cb0ef41Sopenharmony_ci const options = { alpha: { type: 'string', multiple: false, default: ['array'] } }; 9901cb0ef41Sopenharmony_ci assert.throws(() => { 9911cb0ef41Sopenharmony_ci parseArgs({ args, options }); 9921cb0ef41Sopenharmony_ci }, /"options\.alpha\.default" property must be of type string/ 9931cb0ef41Sopenharmony_ci ); 9941cb0ef41Sopenharmony_ci}); 995