pax_global_header00006660000000000000000000000064141066154450014520gustar00rootroot0000000000000052 comment=c1109765e0061ea41211a646fbbd8ff11bee2944 arg-5.0.1/000077500000000000000000000000001410661544500122745ustar00rootroot00000000000000arg-5.0.1/.editorconfig000066400000000000000000000003731410661544500147540ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{*.json,*.json.example,*.gyp,*.yml,*.py}] indent_style = space indent_size = 4 [*.md] trim_trailing_whitespace = false arg-5.0.1/.github/000077500000000000000000000000001410661544500136345ustar00rootroot00000000000000arg-5.0.1/.github/workflows/000077500000000000000000000000001410661544500156715ustar00rootroot00000000000000arg-5.0.1/.github/workflows/ci.yml000066400000000000000000000006351410661544500170130ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: matrix: node-version: - 16 - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test arg-5.0.1/.gitignore000066400000000000000000000001231410661544500142600ustar00rootroot00000000000000/node_modules/ /coverage/ package-lock.json yarn.lock npm-debug.log yarn-error.log arg-5.0.1/LICENSE.md000066400000000000000000000020671410661544500137050ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2021 Vercel, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. arg-5.0.1/README.md000066400000000000000000000147621410661544500135650ustar00rootroot00000000000000# Arg `arg` is an unopinionated, no-frills CLI argument parser. ## Installation ```bash npm install arg ``` ## Usage `arg()` takes either 1 or 2 arguments: 1. Command line specification object (see below) 2. Parse options (_Optional_, defaults to `{permissive: false, argv: process.argv.slice(2), stopAtPositional: false}`) It returns an object with any values present on the command-line (missing options are thus missing from the resulting object). Arg performs no validation/requirement checking - we leave that up to the application. All parameters that aren't consumed by options (commonly referred to as "extra" parameters) are added to `result._`, which is _always_ an array (even if no extra parameters are passed, in which case an empty array is returned). ```javascript const arg = require('arg'); // `options` is an optional parameter const args = arg( spec, (options = { permissive: false, argv: process.argv.slice(2) }) ); ``` For example: ```console $ node ./hello.js --verbose -vvv --port=1234 -n 'My name' foo bar --tag qux --tag=qix -- --foobar ``` ```javascript // hello.js const arg = require('arg'); const args = arg({ // Types '--help': Boolean, '--version': Boolean, '--verbose': arg.COUNT, // Counts the number of times --verbose is passed '--port': Number, // --port or --port= '--name': String, // --name or --name= '--tag': [String], // --tag or --tag= // Aliases '-v': '--verbose', '-n': '--name', // -n ; result is stored in --name '--label': '--name' // --label or --label=; // result is stored in --name }); console.log(args); /* { _: ["foo", "bar", "--foobar"], '--port': 1234, '--verbose': 4, '--name': "My name", '--tag': ["qux", "qix"] } */ ``` The values for each key=>value pair is either a type (function or [function]) or a string (indicating an alias). - In the case of a function, the string value of the argument's value is passed to it, and the return value is used as the ultimate value. - In the case of an array, the only element _must_ be a type function. Array types indicate that the argument may be passed multiple times, and as such the resulting value in the returned object is an array with all of the values that were passed using the specified flag. - In the case of a string, an alias is established. If a flag is passed that matches the _key_, then the _value_ is substituted in its place. Type functions are passed three arguments: 1. The parameter value (always a string) 2. The parameter name (e.g. `--label`) 3. The previous value for the destination (useful for reduce-like operations or for supporting `-v` multiple times, etc.) This means the built-in `String`, `Number`, and `Boolean` type constructors "just work" as type functions. Note that `Boolean` and `[Boolean]` have special treatment - an option argument is _not_ consumed or passed, but instead `true` is returned. These options are called "flags". For custom handlers that wish to behave as flags, you may pass the function through `arg.flag()`: ```javascript const arg = require('arg'); const argv = [ '--foo', 'bar', '-ff', 'baz', '--foo', '--foo', 'qux', '-fff', 'qix' ]; function myHandler(value, argName, previousValue) { /* `value` is always `true` */ return 'na ' + (previousValue || 'batman!'); } const args = arg( { '--foo': arg.flag(myHandler), '-f': '--foo' }, { argv } ); console.log(args); /* { _: ['bar', 'baz', 'qux', 'qix'], '--foo': 'na na na na na na na na batman!' } */ ``` As well, `arg` supplies a helper argument handler called `arg.COUNT`, which equivalent to a `[Boolean]` argument's `.length` property - effectively counting the number of times the boolean flag, denoted by the key, is passed on the command line.. For example, this is how you could implement `ssh`'s multiple levels of verbosity (`-vvvv` being the most verbose). ```javascript const arg = require('arg'); const argv = ['-AAAA', '-BBBB']; const args = arg( { '-A': arg.COUNT, '-B': [Boolean] }, { argv } ); console.log(args); /* { _: [], '-A': 4, '-B': [true, true, true, true] } */ ``` ### Options If a second parameter is specified and is an object, it specifies parsing options to modify the behavior of `arg()`. #### `argv` If you have already sliced or generated a number of raw arguments to be parsed (as opposed to letting `arg` slice them from `process.argv`) you may specify them in the `argv` option. For example: ```javascript const args = arg( { '--foo': String }, { argv: ['hello', '--foo', 'world'] } ); ``` results in: ```javascript const args = { _: ['hello'], '--foo': 'world' }; ``` #### `permissive` When `permissive` set to `true`, `arg` will push any unknown arguments onto the "extra" argument array (`result._`) instead of throwing an error about an unknown flag. For example: ```javascript const arg = require('arg'); const argv = [ '--foo', 'hello', '--qux', 'qix', '--bar', '12345', 'hello again' ]; const args = arg( { '--foo': String, '--bar': Number }, { argv, permissive: true } ); ``` results in: ```javascript const args = { _: ['--qux', 'qix', 'hello again'], '--foo': 'hello', '--bar': 12345 }; ``` #### `stopAtPositional` When `stopAtPositional` is set to `true`, `arg` will halt parsing at the first positional argument. For example: ```javascript const arg = require('arg'); const argv = ['--foo', 'hello', '--bar']; const args = arg( { '--foo': Boolean, '--bar': Boolean }, { argv, stopAtPositional: true } ); ``` results in: ```javascript const args = { _: ['hello', '--bar'], '--foo': true }; ``` ### Errors Some errors that `arg` throws provide a `.code` property in order to aid in recovering from user error, or to differentiate between user error and developer error (bug). ##### ARG_UNKNOWN_OPTION If an unknown option (not defined in the spec object) is passed, an error with code `ARG_UNKNOWN_OPTION` will be thrown: ```js // cli.js try { require('arg')({ '--hi': String }); } catch (err) { if (err.code === 'ARG_UNKNOWN_OPTION') { console.log(err.message); } else { throw err; } } ``` ```shell node cli.js --extraneous true Unknown or unexpected option: --extraneous ``` # FAQ A few questions and answers that have been asked before: ### How do I require an argument with `arg`? Do the assertion yourself, such as: ```javascript const args = arg({ '--name': String }); if (!args['--name']) throw new Error('missing required argument: --name'); ``` # License Released under the [MIT License](LICENSE.md). arg-5.0.1/index.d.ts000066400000000000000000000015631410661544500142020ustar00rootroot00000000000000declare const flagSymbol: unique symbol; declare function arg( spec: T, options?: arg.Options ): arg.Result; declare namespace arg { export function flag(fn: T): T & { [flagSymbol]: true }; export const COUNT: Handler & { [flagSymbol]: true }; export type Handler = ( value: string, name: string, previousValue?: T ) => T; export class ArgError extends Error { constructor(message: string, code: string); code: string; } export interface Spec { [key: string]: string | Handler | [Handler]; } export type Result = { _: string[] } & { [K in keyof T]?: T[K] extends Handler ? ReturnType : T[K] extends [Handler] ? Array> : never; }; export interface Options { argv?: string[]; permissive?: boolean; stopAtPositional?: boolean; } } export = arg; arg-5.0.1/index.js000066400000000000000000000105601410661544500137430ustar00rootroot00000000000000const flagSymbol = Symbol('arg flag'); class ArgError extends Error { constructor(msg, code) { super(msg); this.name = 'ArgError'; this.code = code; Object.setPrototypeOf(this, ArgError.prototype); } } function arg( opts, { argv = process.argv.slice(2), permissive = false, stopAtPositional = false } = {} ) { if (!opts) { throw new ArgError( 'argument specification object is required', 'ARG_CONFIG_NO_SPEC' ); } const result = { _: [] }; const aliases = {}; const handlers = {}; for (const key of Object.keys(opts)) { if (!key) { throw new ArgError( 'argument key cannot be an empty string', 'ARG_CONFIG_EMPTY_KEY' ); } if (key[0] !== '-') { throw new ArgError( `argument key must start with '-' but found: '${key}'`, 'ARG_CONFIG_NONOPT_KEY' ); } if (key.length === 1) { throw new ArgError( `argument key must have a name; singular '-' keys are not allowed: ${key}`, 'ARG_CONFIG_NONAME_KEY' ); } if (typeof opts[key] === 'string') { aliases[key] = opts[key]; continue; } let type = opts[key]; let isFlag = false; if ( Array.isArray(type) && type.length === 1 && typeof type[0] === 'function' ) { const [fn] = type; type = (value, name, prev = []) => { prev.push(fn(value, name, prev[prev.length - 1])); return prev; }; isFlag = fn === Boolean || fn[flagSymbol] === true; } else if (typeof type === 'function') { isFlag = type === Boolean || type[flagSymbol] === true; } else { throw new ArgError( `type missing or not a function or valid array type: ${key}`, 'ARG_CONFIG_VAD_TYPE' ); } if (key[1] !== '-' && key.length > 2) { throw new ArgError( `short argument keys (with a single hyphen) must have only one character: ${key}`, 'ARG_CONFIG_SHORTOPT_TOOLONG' ); } handlers[key] = [type, isFlag]; } for (let i = 0, len = argv.length; i < len; i++) { const wholeArg = argv[i]; if (stopAtPositional && result._.length > 0) { result._ = result._.concat(argv.slice(i)); break; } if (wholeArg === '--') { result._ = result._.concat(argv.slice(i + 1)); break; } if (wholeArg.length > 1 && wholeArg[0] === '-') { /* eslint-disable operator-linebreak */ const separatedArguments = wholeArg[1] === '-' || wholeArg.length === 2 ? [wholeArg] : wholeArg .slice(1) .split('') .map((a) => `-${a}`); /* eslint-enable operator-linebreak */ for (let j = 0; j < separatedArguments.length; j++) { const arg = separatedArguments[j]; const [originalArgName, argStr] = arg[1] === '-' ? arg.split(/=(.*)/, 2) : [arg, undefined]; let argName = originalArgName; while (argName in aliases) { argName = aliases[argName]; } if (!(argName in handlers)) { if (permissive) { result._.push(arg); continue; } else { throw new ArgError( `unknown or unexpected option: ${originalArgName}`, 'ARG_UNKNOWN_OPTION' ); } } const [type, isFlag] = handlers[argName]; if (!isFlag && j + 1 < separatedArguments.length) { throw new ArgError( `option requires argument (but was followed by another short argument): ${originalArgName}`, 'ARG_MISSING_REQUIRED_SHORTARG' ); } if (isFlag) { result[argName] = type(true, argName, result[argName]); } else if (argStr === undefined) { if ( argv.length < i + 2 || (argv[i + 1].length > 1 && argv[i + 1][0] === '-' && !( argv[i + 1].match(/^-?\d*(\.(?=\d))?\d*$/) && (type === Number || // eslint-disable-next-line no-undef (typeof BigInt !== 'undefined' && type === BigInt)) )) ) { const extended = originalArgName === argName ? '' : ` (alias for ${argName})`; throw new ArgError( `option requires argument: ${originalArgName}${extended}`, 'ARG_MISSING_REQUIRED_LONGARG' ); } result[argName] = type(argv[i + 1], argName, result[argName]); ++i; } else { result[argName] = type(argStr, argName, result[argName]); } } } else { result._.push(wholeArg); } } return result; } arg.flag = (fn) => { fn[flagSymbol] = true; return fn; }; // Utility types arg.COUNT = arg.flag((v, name, existingCount) => (existingCount || 0) + 1); // Expose error class arg.ArgError = ArgError; module.exports = arg; arg-5.0.1/package.json000066400000000000000000000011151410661544500145600ustar00rootroot00000000000000{ "name": "arg", "version": "5.0.1", "description": "Unopinionated, no-frills CLI argument parser", "main": "index.js", "types": "index.d.ts", "repository": "vercel/arg", "author": "Josh Junon ", "license": "MIT", "files": [ "index.js", "index.d.ts" ], "scripts": { "test": "WARN_EXIT=1 jest --coverage -w 2" }, "devDependencies": { "chai": "^4.1.1", "jest": "^27.0.6", "prettier": "^2.3.2" }, "prettier": { "arrowParens": "always", "singleQuote": true, "tabWidth": 2, "trailingComma": "none" } } arg-5.0.1/test.js000066400000000000000000000257501410661544500136220ustar00rootroot00000000000000/* global test */ /* eslint-disable no-unused-expressions */ const expect = require('chai').expect; const arg = require('.'); test('basic parses arguments from process.argv', () => { const curArgs = process.argv; process.argv = ['node', 'test.js', '--foo', '1337', '-B', 'hello', '--mcgee']; try { const args = arg({ '--foo': Number, '--bar': String, '--mcgee': Boolean, '-B': '--bar' }); expect(args).to.exist; expect(args['--foo']).to.equal(1337); expect(args['--bar']).to.equal('hello'); expect(args['--mcgee']).to.equal(true); } finally { process.argv = curArgs; } }); test('arg with no arguments', () => { expect(() => arg()).to.throw( arg.ArgError, 'argument specification object is required' ); }); test('basic extra arguments parsing', () => { const argv = ['hi', 'hello', 'there', '-']; expect(arg({}, { argv })).to.deep.equal({ _: argv }); }); test('basic string parsing', () => { const argv = ['hey', '--foo', 'hi', 'hello']; expect(arg({ '--foo': String }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': 'hi' }); }); test('basic string parsing (equals long-arg)', () => { const argv = ['hey', '--foo=hi', 'hello']; expect(arg({ '--foo': String }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': 'hi' }); }); test('basic string parsing (equals long-arg-with-equals)', () => { const argv = ['hey', '--foo=hi.hello?q=p', 'hello']; expect(arg({ '--foo': String }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': 'hi.hello?q=p' }); }); test('basic number parsing', () => { const argv = ['hey', '--foo', '1234', 'hello']; expect(arg({ '--foo': Number }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': 1234 }); }); test('basic boolean parsing', () => { const argv = ['hey', '--foo', '1234', 'hello']; expect(arg({ '--foo': Boolean }, { argv })).to.deep.equal({ _: ['hey', '1234', 'hello'], '--foo': true }); }); test('basic custom type parsing', () => { const argv = ['hey', '--foo', '1234', 'hello']; const customType = (val, name) => `:${name}:${val}:`; expect(arg({ '--foo': customType }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': ':--foo:1234:' }); }); test('basic string parsing (array)', () => { const argv = ['hey', '--foo', 'hi', 'hello', '--foo', 'hey']; expect(arg({ '--foo': [String] }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': ['hi', 'hey'] }); }); test('basic number parsing (array)', () => { const argv = ['hey', '--foo', '1234', 'hello', '--foo', '5432']; expect(arg({ '--foo': [Number] }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': [1234, 5432] }); }); test('basic boolean parsing (array)', () => { const argv = ['hey', '--foo', '1234', 'hello', '--foo', 'hallo']; expect(arg({ '--foo': [Boolean] }, { argv })).to.deep.equal({ _: ['hey', '1234', 'hello', 'hallo'], '--foo': [true, true] }); }); test('basic custom type parsing (array)', () => { const argv = ['hey', '--foo', '1234', 'hello', '--foo', '8911hi']; const customType = (val, name) => `:${name}:${val}:`; expect(arg({ '--foo': [customType] }, { argv })).to.deep.equal({ _: ['hey', 'hello'], '--foo': [':--foo:1234:', ':--foo:8911hi:'] }); }); test('basic alias parsing', () => { const argv = [ '--foo', '1234', '-B', '-', 'hello', '--not-foo-or-bar', 'ohai' ]; const opts = { '--foo': Number, '--bar': String, '--another-arg': Boolean, '-a': '--another-arg', '--not-foo-or-bar': '--another-arg', '-B': '--bar' }; expect(arg(opts, { argv })).to.deep.equal({ _: ['hello', 'ohai'], '--foo': 1234, '--bar': '-', '--another-arg': true }); }); test('double-dash parsing', () => { const argv = [ '--foo', '1234', 'hi', '--foo', '5678', 'there', '--', '--foo', '2468' ]; expect(arg({ '--foo': Number }, { argv })).to.deep.equal({ _: ['hi', 'there', '--foo', '2468'], '--foo': 5678 }); }); test('error: invalid option', () => { const argv = ['--foo', '1234', '--bar', '8765']; expect(() => arg({ '--foo': Number }, { argv })).to.throw( arg.ArgError, 'unknown or unexpected option: --bar' ); }); test('error: expected argument', () => { const argv = ['--foo', '--bar', '1234']; expect(() => arg({ '--foo': String, '--bar': Number }, { argv })).to.throw( arg.ArgError, 'option requires argument: --foo' ); }); test('error: expected argument (end flag)', () => { const argv = ['--foo', '--bar']; expect(() => arg({ '--foo': Boolean, '--bar': Number }, { argv })).to.throw( arg.ArgError, 'option requires argument: --bar' ); }); test('error: expected argument (alias)', () => { const argv = ['--foo', '--bar', '1234']; expect(() => arg( { '--realfoo': String, '--foo': '--realfoo', '--bar': Number }, { argv } ) ).to.throw( arg.ArgError, 'option requires argument: --foo (alias for --realfoo)' ); }); test('error: expected argument (end flag) (alias)', () => { const argv = ['--foo', '--bar']; expect(() => arg( { '--foo': Boolean, '--realbar': Number, '--bar': '--realbar' }, { argv } ) ).to.throw( arg.ArgError, 'option requires argument: --bar (alias for --realbar)' ); }); test('error: non-function type', () => { const argv = []; expect(() => arg({ '--foo': 10 }, { argv })).to.throw( arg.ArgError, 'type missing or not a function or valid array type: --foo' ); expect(() => arg({ '--foo': null }, { argv })).to.throw( arg.ArgError, 'type missing or not a function or valid array type: --foo' ); expect(() => arg({ '--foo': undefined }, { argv })).to.throw( arg.ArgError, 'type missing or not a function or valid array type: --foo' ); }); test('error: no singular - keys allowed', () => { const argv = ['--foo', '--bar', '1234']; expect(() => arg({ '-': Boolean, '--bar': Number }, { argv })).to.throw( arg.ArgError, "argument key must have a name; singular '-' keys are not allowed: -" ); }); test('error: no multi character short arguments', () => { const argv = ['--foo', '--bar', '1234']; expect(() => arg({ '-abc': Boolean, '--bar': Number }, { argv })).to.throw( arg.ArgError, 'short argument keys (with a single hyphen) must have only one character: -abc' ); }); test('permissive mode allows unknown args', () => { const argv = [ 'foo', '--real', 'nice', '--unreal', 'stillnice', '-a', '1', '-b', '2', 'goodbye' ]; const result = arg( { '--real': String, '--first': Number, '-a': '--first' }, { argv, permissive: true } ); expect(result).to.deep.equal({ _: ['foo', '--unreal', 'stillnice', '-b', '2', 'goodbye'], '--real': 'nice', '--first': 1 }); }); test('permissive mode works with no argv specified', () => { const curArgs = process.argv; process.argv = ['node', 'test.js', '--foo', '1337', '-B', 'hello', '--mcgee']; try { const result = arg( { '--foo': Number, '--mcgee': Boolean, '--unused': Boolean }, { permissive: true } ); expect(result).to.deep.equal({ _: ['-B', 'hello'], '--foo': 1337, '--mcgee': true }); } finally { process.argv = curArgs; } }); test('ensure that all argument properties start with a hyphen', () => { expect(() => arg({ '--foo': Number, bar: String, '--baz': Boolean }) ).to.throw(arg.ArgError, "argument key must start with '-' but found: 'bar'"); }); test('ensure argument property is not an empty string', () => { expect(() => arg({ '': Number }) ).to.throw(arg.ArgError, 'argument key cannot be an empty string'); }); test('types with the Flag symbol should be passed true instead of an argument', () => { const argv = ['--mcgee', '--foo', 'bar', '--baz', '10', 'qix']; const result = arg( { '--mcgee': Boolean, '--foo': arg.flag(() => 1337), '--baz': Number }, { argv } ); expect(result).to.deep.equal({ _: ['bar', 'qix'], '--mcgee': true, '--foo': 1337, '--baz': 10 }); }); test('COUNT should count the number of times a flag has been passed', () => { const argv = ['--verbose', '-v', '--verbose', 'foo', '-vvvv', '-vv']; const result = arg( { '--verbose': arg.COUNT, '-v': '--verbose' }, { argv } ); expect(result).to.deep.equal({ _: ['foo'], '--verbose': 9 }); }); test('should parse combined shortarg flags', () => { const argv = ['-vv', '-sd', 'foo', '-vdv']; const result = arg( { '-v': [Boolean], '-s': Boolean, '-d': arg.COUNT }, { argv } ); expect(result).to.deep.equal({ _: ['foo'], '-v': [true, true, true, true], '-s': true, '-d': 2 }); }); test('should parse combined shortarg alias flags', () => { const argv = ['-vv', '--verbose', '-dvd', 'foo', '--dee', '-vdv']; const result = arg( { '--verbose': [Boolean], '-v': '--verbose', '--dee': arg.COUNT, '-d': '--dee' }, { argv } ); expect(result).to.deep.equal({ _: ['foo'], '--verbose': [true, true, true, true, true, true], '--dee': 4 }); }); test('should allow a non-flag shortarg to suffix a string of shortarg flags', () => { const argv = ['-vvLo', 'foo']; const result = arg( { '-v': arg.COUNT, '-L': Boolean, '-o': String }, { argv } ); expect(result).to.deep.equal({ _: [], '-v': 2, '-L': true, '-o': 'foo' }); }); test('should error if a non-flag shortarg comes before a shortarg flag in a condensed shortarg argument', () => { const argv = ['-vsv', 'foo']; expect(() => arg( { '-v': arg.COUNT, '-s': String }, { argv } ) ).to.throw( arg.ArgError, 'option requires argument (but was followed by another short argument): -s' ); }); test('should stop parsing early with positional argument', () => { const argv = ['-d', 'script', '--foo', 'bar']; const result = arg( { '-d': Boolean }, { argv, stopAtPositional: true } ); expect(result).to.deep.equal({ _: ['script', '--foo', 'bar'], '-d': true }); }); test('should stop parsing early with permissive', () => { const argv = ['-dvd', '--foo', 'bar']; const result = arg( { '-d': arg.COUNT }, { argv, stopAtPositional: true, permissive: true } ); expect(result).to.deep.equal({ _: ['-v', '--foo', 'bar'], '-d': 2 }); }); test('should parse negative numbers (GNU equals form)', () => { const argv = ['--int=-5']; const result = arg( { '--int': Number }, { argv } ); expect(result).to.deep.equal({ _: [], '--int': -5 }); }); test('should parse negative numbers (separate argument form)', () => { const argv = ['--int', '-5']; const result = arg( { '--int': Number }, { argv } ); expect(result).to.deep.equal({ _: [], '--int': -5 }); }); test('should error if numeric type is followed by non-negative, non-argument', () => { const argv = ['--int', '-abc']; expect(() => arg( { '--int': Number }, { argv } ) ).to.throw(arg.ArgError, 'option requires argument: --int'); }); test('should error if negative numeric argument is passed to non-negative argument', () => { const argv = ['--str', '-15']; expect(() => arg( { '--str': String }, { argv } ) ).to.throw(arg.ArgError, 'option requires argument: --str'); });