Skip to content

Commit

Permalink
Make isMultiple non-greedy (#162)
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 28, 2020
1 parent 14924de commit 49ce74d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -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
```
Expand Down
10 changes: 6 additions & 4 deletions index.js
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions test/is-required-flag.js
Expand Up @@ -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 => {
Expand Down
19 changes: 17 additions & 2 deletions test/test.js
Expand Up @@ -355,16 +355,31 @@ 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: {
type: 'string',
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']
});
});

Expand Down

0 comments on commit 49ce74d

Please sign in to comment.