diff --git a/index.d.ts b/index.d.ts index 67c6057..6052c49 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,6 +40,7 @@ declare namespace meow { - `isRequired`: Determine if the flag is required. 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. - `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false) + 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. @example ``` diff --git a/index.js b/index.js index b3f85ce..c726013 100644 --- a/index.js +++ b/index.js @@ -125,11 +125,13 @@ const meow = (helpText, options) => { parserOptions = buildParserOptions(parserOptions); + parserOptions.configuration = { + ...parserOptions.configuration, + 'greedy-arrays': false + }; + if (parserOptions['--']) { - parserOptions.configuration = { - ...parserOptions.configuration, - 'populate--': true - }; + parserOptions.configuration['populate--'] = true; } const {pkg} = options; diff --git a/readme.md b/readme.md index 9e6ab8a..1c547dc 100644 --- a/readme.md +++ b/readme.md @@ -143,6 +143,7 @@ The key is the flag name and the value is an object with any of: - The second argument is the **input** string array, which contains the non-flag arguments. - The function should return a `boolean`, true if the flag is required, otherwise false. - `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false) + - 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). Example: diff --git a/test/is-required-flag.js b/test/is-required-flag.js index 66907fb..a8c5730 100644 --- a/test/is-required-flag.js +++ b/test/is-required-flag.js @@ -87,9 +87,9 @@ test('spawn cli and test isRequired with isMultiple giving a single value', asyn t.is(stdout, '[ 1 ]'); }); -test('spawn cli and test isRequired with isMultiple giving a multiple values', async t => { - const {stdout} = await execa(fixtureRequiredMultiplePath, ['--test', '1', '2', '3']); - t.is(stdout, '[ 1, 2, 3 ]'); +test('spawn cli and test isRequired with isMultiple giving multiple values', async t => { + const {stdout} = await execa(fixtureRequiredMultiplePath, ['--test', '1', '--test', '2']); + t.is(stdout, '[ 1, 2 ]'); }); test('spawn cli and test isRequired with isMultiple giving no values, but flag is given', async t => { diff --git a/test/test.js b/test/test.js index abaebef..cfae265 100644 --- a/test/test.js +++ b/test/test.js @@ -355,7 +355,7 @@ test('isMultiple - flag set multiple times', t => { }); test('isMultiple - flag with space separated values', t => { - t.deepEqual(meow({ + const {input, flags} = meow({ argv: ['--foo', 'bar', 'baz'], flags: { foo: { @@ -363,8 +363,23 @@ test('isMultiple - flag with space separated values', t => { isMultiple: true } } + }); + + t.deepEqual(input, ['baz']); + t.deepEqual(flags.foo, ['bar']); +}); + +test('isMultiple - flag with comma separated values', t => { + t.deepEqual(meow({ + argv: ['--foo', 'bar,baz'], + flags: { + foo: { + type: 'string', + isMultiple: true + } + } }).flags, { - foo: ['bar', 'baz'] + foo: ['bar,baz'] }); });