diff --git a/README.md b/README.md index 0d3aa74..eeba977 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ into an `Array` * `short` {Object} (Optional) An `Object` of key, value pairs of strings which map a "short" alias to an argument; When appearing multiples times in `argv`; Respects `withValue` & `multiples` * `strict` {Boolean} (Optional) A `Boolean` on wheather or not to throw an error when unknown args are encountered * Returns: {Object} An object having properties: - * `args` {Object}, having properties and `Boolean` values corresponding to parsed options passed + * `flags` {Object}, having properties and `Boolean` values corresponding to parsed options passed * `values` {Object}, have properties and `String` values corresponding to parsed options passed * `positionals` {string[]}, containing [Positionals][] @@ -100,8 +100,8 @@ const { parseArgs } = require('util') // default const argv = ['-f', '--foo=a', '--foo', 'b'] const options = {} -const { args, values, positionals } = parseArgs(argv, options) -args // { f: true, foo: true} +const { flags, values, positionals } = parseArgs(argv, options) +flags // { f: true, foo: true} values // { f: [undefined], foo: [undefined] } positionals // ['b'] ``` @@ -111,8 +111,8 @@ const argv = ['-f', '--foo=a', '--foo', 'b'] const options = { withValue: ['foo'] } -const { args, values, positionals } = parseArgs(argv, options) -args // { f: true, foo: true} +const { flags, values, positionals } = parseArgs(argv, options) +flags // { f: true, foo: true} values // { f: [undefined], foo: ['b'] } positionals // [] ``` @@ -123,8 +123,8 @@ const options = { withValue: ['foo'], multiples: ['foo'] } -const { args, values, positionals } = parseArgs(argv, options) -args // { f: true, foo: true} +const { flags, values, positionals } = parseArgs(argv, options) +flags // { f: true, foo: true} values // { f: [undefined], foo: ['a','b'] } positionals // [] ``` @@ -134,8 +134,8 @@ const argv = ['-f', '--foo=a', '--foo', 'b'] const options = { short: { f: 'foo' } } -const { args, values, positionals } = parseArgs(argv, options) -args // { foo: true} +const { flags, values, positionals } = parseArgs(argv, options) +flags // { foo: true} values // { foo: [undefined] } positionals // ['b'] ``` diff --git a/index.js b/index.js index bc0d5a9..ca430e1 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const parseArgs = ( } const result = { - args: {}, + flags: {}, values: {}, positionals: [] }; @@ -39,13 +39,13 @@ const parseArgs = ( // withValue equals(=) case const argParts = arg.split('='); - result.args[argParts[0]] = true; + result.flags[argParts[0]] = true; // If withValue option is specified, take 2nd part after '=' as value, // else set value as undefined const val = options.withValue && options.withValue.includes(argParts[0]) ? argParts[1] : undefined; - // Append value to previous arg values array for case of multiples + // Append value to previous values array for case of multiples // option, else add to empty array result.values[argParts[0]] = [].concat( options.multiples && @@ -57,7 +57,7 @@ const parseArgs = ( // withValue option should also support setting values when '= // isn't used ie. both --foo=b and --foo b should work - result.args[arg] = true; + result.flags[arg] = true; // If withValue option is specified, take next position arguement as // value and then increment pos so that we don't re-evaluate that // arg, else set value as undefined ie. --foo b --bar c, after setting @@ -65,7 +65,7 @@ const parseArgs = ( const val = options.withValue && options.withValue.includes(arg) ? argv[++pos] : undefined; - // Append value to previous arg values array for case of multiples + // Append value to previous values array for case of multiples // option, else add to empty array result.values[arg] = [].concat( options.multiples && options.multiples.includes(arg) && @@ -75,10 +75,10 @@ const parseArgs = ( val); } else { // Cases when an arg is specified without a value, example - // '--foo --bar' <- 'foo' and 'bar' args should be set to true and + // '--foo --bar' <- 'foo' and 'bar' flags should be set to true and // shave value as undefined - result.args[arg] = true; - // Append undefined to previous arg values array for case of + result.flags[arg] = true; + // Append undefined to previous values array for case of // multiples option, else add to empty array result.values[arg] = [].concat( options.multiples && options.multiples.includes(arg) && diff --git a/package-lock.json b/package-lock.json index 85b7467..6cc791a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -907,6 +907,13 @@ "@babel/plugin-syntax-top-level-await": "^7.14.5", "eslint-plugin-markdown": "^2.2.1", "eslint-plugin-node-core": "^1.0.0" + }, + "dependencies": { + "eslint-plugin-node-core": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-node-core/-/eslint-plugin-node-core-1.0.0.tgz", + "integrity": "sha512-1i9imacKER4Noc7AkfgnPfRcirKb9GICuSmmu3q/V2VjsQA4jCYh6cExvpRsdPkX4mQh8tSpoDRrCfE7az04KA==" + } } }, "eslint-scope": { diff --git a/test/index.js b/test/index.js index e0f66e9..fc02d55 100644 --- a/test/index.js +++ b/test/index.js @@ -7,7 +7,7 @@ const {parseArgs} = require('../index.js') test('Everything after a bare `--` is considered a positional argument', function (t) { const passedArgs = ['--', 'barepositionals', 'mopositionals'] - const expected = { args: {}, values: {}, positionals: ['barepositionals', 'mopositionals'] } + const expected = { flags: {}, values: {}, positionals: ['barepositionals', 'mopositionals'] } const args = parseArgs(passedArgs) t.deepEqual(args, expected, 'testing bare positionals') @@ -17,7 +17,7 @@ test('Everything after a bare `--` is considered a positional argument', functio test('args are true', function (t) { const passedArgs = ['--foo', '--bar'] - const expected = { args: { foo: true, bar: true}, values: {foo: [undefined], bar: [undefined]}, positionals: [] } + const expected = { flags: { foo: true, bar: true}, values: {foo: [undefined], bar: [undefined]}, positionals: [] } const args = parseArgs(passedArgs) t.deepEqual(args, expected, 'args are true') @@ -27,7 +27,7 @@ test('args are true', function (t) { test('arg is true and positional is identified', function (t) { const passedArgs = ['--foo=a', '--foo', 'b'] - const expected = { args: { foo: true}, values: { foo: [undefined]}, positionals: ['b'] } + const expected = { flags: { foo: true}, values: { foo: [undefined]}, positionals: ['b'] } const args = parseArgs(passedArgs) t.deepEqual(args, expected, 'arg is true and positional is identified') @@ -38,7 +38,7 @@ test('arg is true and positional is identified', function (t) { test('args equals are passed "withValue"', function (t) { const passedArgs = ['--so=wat'] const passedOptions = { withValue: ['so'] } - const expected = { args: { so: true}, values: { so: ["wat"]}, positionals: [] } + const expected = { flags: { so: true}, values: { so: ["wat"]}, positionals: [] } const args = parseArgs(passedArgs, passedOptions) t.deepEqual(args, expected, 'arg value is passed') @@ -49,7 +49,7 @@ test('args equals are passed "withValue"', function (t) { test('same arg is passed twice "withValue" and last value is recorded', function (t) { const passedArgs = ['--foo=a', '--foo', 'b'] const passedOptions = { withValue: ['foo'] } - const expected = { args: { foo: true}, values: { foo: ['b']}, positionals: [] } + const expected = { flags: { foo: true}, values: { foo: ['b']}, positionals: [] } const args = parseArgs(passedArgs, passedOptions) t.deepEqual(args, expected, 'last arg value is passed') @@ -60,7 +60,7 @@ test('same arg is passed twice "withValue" and last value is recorded', function test('args are passed "withValue" and "multiples"', function (t) { const passedArgs = ['--foo=a', '--foo', 'b'] const passedOptions = { withValue: ['foo'], multiples: ['foo'] } - const expected = { args: { foo: true}, values: { foo: ['a', 'b']}, positionals: [] } + const expected = { flags: { foo: true}, values: { foo: ['a', 'b']}, positionals: [] } const args = parseArgs(passedArgs, passedOptions) t.deepEqual(args, expected, 'both arg values are passed') @@ -87,4 +87,4 @@ test('string passed to "withValue" option', function (t) { t.throws(function() { parseArgs(passedArgs, passedOptions) }); t.end() -}) \ No newline at end of file +})