Skip to content

Commit

Permalink
refactor: args to flags (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Dec 20, 2021
1 parent 886e99c commit 80843da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][]

Expand All @@ -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']
```
Expand All @@ -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 // []
```
Expand All @@ -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 // []
```
Expand All @@ -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']
```
Expand Down
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const parseArgs = (
}

const result = {
args: {},
flags: {},
values: {},
positionals: []
};
Expand All @@ -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 &&
Expand All @@ -57,15 +57,15 @@ 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
// b as the value for foo, evaluate --bar next and skip 'b'
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) &&
Expand All @@ -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) &&
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -87,4 +87,4 @@ test('string passed to "withValue" option', function (t) {
t.throws(function() { parseArgs(passedArgs, passedOptions) });

t.end()
})
})

0 comments on commit 80843da

Please sign in to comment.