Skip to content

Commit 49ce74d

Browse files
ulkensindresorhus
andauthoredOct 28, 2020
Make isMultiple non-greedy (#162)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 14924de commit 49ce74d

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed
 

‎index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare namespace meow {
4040
- `isRequired`: Determine if the flag is required.
4141
If it's only known at runtime whether the flag is required or not you can pass a Function instead of a boolean, which based on the given flags and other non-flag arguments should decide if the flag is required.
4242
- `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false)
43+
Multiple values are provided by specifying the flag multiple times, for example, `$ foo -u rainbow -u cat`. Space- or comma-separated values are *not* supported.
4344
4445
@example
4546
```

‎index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ const meow = (helpText, options) => {
125125

126126
parserOptions = buildParserOptions(parserOptions);
127127

128+
parserOptions.configuration = {
129+
...parserOptions.configuration,
130+
'greedy-arrays': false
131+
};
132+
128133
if (parserOptions['--']) {
129-
parserOptions.configuration = {
130-
...parserOptions.configuration,
131-
'populate--': true
132-
};
134+
parserOptions.configuration['populate--'] = true;
133135
}
134136

135137
const {pkg} = options;

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ The key is the flag name and the value is an object with any of:
143143
- The second argument is the **input** string array, which contains the non-flag arguments.
144144
- The function should return a `boolean`, true if the flag is required, otherwise false.
145145
- `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false)
146+
- Multiple values are provided by specifying the flag multiple times, for example, `$ foo -u rainbow -u cat`. Space- or comma-separated values are [currently *not* supported](https://github.com/sindresorhus/meow/issues/164).
146147
147148
Example:
148149

‎test/is-required-flag.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ test('spawn cli and test isRequired with isMultiple giving a single value', asyn
8787
t.is(stdout, '[ 1 ]');
8888
});
8989

90-
test('spawn cli and test isRequired with isMultiple giving a multiple values', async t => {
91-
const {stdout} = await execa(fixtureRequiredMultiplePath, ['--test', '1', '2', '3']);
92-
t.is(stdout, '[ 1, 2, 3 ]');
90+
test('spawn cli and test isRequired with isMultiple giving multiple values', async t => {
91+
const {stdout} = await execa(fixtureRequiredMultiplePath, ['--test', '1', '--test', '2']);
92+
t.is(stdout, '[ 1, 2 ]');
9393
});
9494

9595
test('spawn cli and test isRequired with isMultiple giving no values, but flag is given', async t => {

‎test/test.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,31 @@ test('isMultiple - flag set multiple times', t => {
355355
});
356356

357357
test('isMultiple - flag with space separated values', t => {
358-
t.deepEqual(meow({
358+
const {input, flags} = meow({
359359
argv: ['--foo', 'bar', 'baz'],
360360
flags: {
361361
foo: {
362362
type: 'string',
363363
isMultiple: true
364364
}
365365
}
366+
});
367+
368+
t.deepEqual(input, ['baz']);
369+
t.deepEqual(flags.foo, ['bar']);
370+
});
371+
372+
test('isMultiple - flag with comma separated values', t => {
373+
t.deepEqual(meow({
374+
argv: ['--foo', 'bar,baz'],
375+
flags: {
376+
foo: {
377+
type: 'string',
378+
isMultiple: true
379+
}
380+
}
366381
}).flags, {
367-
foo: ['bar', 'baz']
382+
foo: ['bar,baz']
368383
});
369384
});
370385

0 commit comments

Comments
 (0)
Please sign in to comment.