Skip to content

Commit

Permalink
Default isMultiple to empty array (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
ulken and sindresorhus committed Oct 19, 2020
1 parent dc7dae4 commit 14924de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
8 changes: 6 additions & 2 deletions index.d.ts
Expand Up @@ -125,9 +125,13 @@ declare namespace meow {
readonly inferType?: boolean;

/**
Value of `boolean` flags not defined in `argv`. If set to `undefined` the flags not defined in `argv` will be excluded from the result. The `default` value set in `boolean` flags take precedence over `booleanDefault`.
Value of `boolean` flags not defined in `argv`.
__Caution: Explicitly specifying undefined for `booleanDefault` has different meaning from omitting key itself.__
If set to `undefined`, the flags not defined in `argv` will be excluded from the result. The `default` value set in `boolean` flags take precedence over `booleanDefault`.
_Note: If used in conjunction with `isMultiple`, the default flag value is set to `[]`._
__Caution: Explicitly specifying `undefined` for `booleanDefault` has different meaning from omitting key itself.__
@example
```
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -68,6 +68,7 @@ const buildParserFlags = ({flags, booleanDefault}) =>

if (flag.isMultiple) {
flag.type = flag.type ? `${flag.type}-array` : 'array';
flag.default = flag.default || [];
delete flag.isMultiple;
}

Expand Down
7 changes: 6 additions & 1 deletion readme.md
Expand Up @@ -239,9 +239,14 @@ Type: `boolean | null | undefined`\
Default: `false`
Value of `boolean` flags not defined in `argv`.
If set to `undefined` the flags not defined in `argv` will be excluded from the result.
If set to `undefined`, the flags not defined in `argv` will be excluded from the result.
The `default` value set in `boolean` flags take precedence over `booleanDefault`.
_Note: If used in conjunction with `isMultiple`, the default flag value is set to `[]`._
__Caution: Explicitly specifying `undefined` for `booleanDefault` has different meaning from omitting key itself.__
Example:
```js
Expand Down
44 changes: 14 additions & 30 deletions test/test.js
Expand Up @@ -312,6 +312,20 @@ test('supports `number` flag type - throws on incorrect default value', t => {
});
});

test('isMultiple - unset flag returns empty array', t => {
t.deepEqual(meow({
argv: [],
flags: {
foo: {
type: 'string',
isMultiple: true
}
}
}).flags, {
foo: []
});
});

test('isMultiple - flag set once returns array', t => {
t.deepEqual(meow({
argv: ['--foo=bar'],
Expand Down Expand Up @@ -408,25 +422,6 @@ test('isMultiple - boolean flag is false by default', t => {
});
});

test('isMultiple - flag with `booleanDefault: undefined` => filter out unset boolean args', t => {
t.deepEqual(meow({
argv: ['--foo'],
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean',
isMultiple: true
},
bar: {
type: 'boolean',
isMultiple: true
}
}
}).flags, {
foo: [true]
});
});

test('isMultiple - number flag', t => {
t.deepEqual(meow({
argv: ['--foo=1.3', '--foo=-1'],
Expand Down Expand Up @@ -510,17 +505,6 @@ test('isMultiple - handles multi-word flag name', t => {
});
});

test('isMultiple - handles non-set flags correctly', t => {
t.deepEqual(meow({
argv: [],
flags: {
foo: {
isMultiple: true
}
}
}).flags, {});
});

if (NODE_MAJOR_VERSION >= 14) {
test('supports es modules', async t => {
try {
Expand Down

0 comments on commit 14924de

Please sign in to comment.