Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default isMultiple to empty array #163

Merged
merged 6 commits into from Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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