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

feat: introduce greedy-arrays config, for specifying whether arrays consume multiple positionals #249

Merged
merged 2 commits into from Mar 1, 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
42 changes: 42 additions & 0 deletions .github/workflows/ci.yaml
@@ -0,0 +1,42 @@
on:
push:
branches:
- master
pull_request:
name: ci
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [8, 10, 12, 13]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: node --version
- run: npm install
- run: npm test
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm install
- run: npm test
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm install
- run: npm test
- run: npm run coverage
env:
COVERALLS_REPO_TOKEN: "${{ secrets.COVERALLS_REPO_TOKEN }}"
COVERALLS_GIT_BRANCH: "${{ github.ref }}"
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -265,6 +265,27 @@ node example.js -x 1 2 -x 3 4
{ _: [], x: [[1, 2], [3, 4]] }
```

### greedy arrays

* default: `true`
* key: `greedy-arrays`

Should arrays consume more than one positional argument following their flag.

```sh
node example --arr 1 2
{ _[], arr: [1, 2] }
```

_if disabled:_

```sh
node example --arr 1 2
{ _[2], arr: [1] }
```

**Note: in `v18.0.0` we are considering defaulting greedy arrays to `false`.**

### nargs eats options

* default: `false`
Expand Down
18 changes: 10 additions & 8 deletions index.js
Expand Up @@ -13,22 +13,23 @@ function parse (args, opts) {
// aliases might have transitive relationships, normalize this.
const aliases = combineAliases(Object.assign(Object.create(null), opts.alias))
const configuration = Object.assign({
'short-option-groups': true,
'boolean-negation': true,
'camel-case-expansion': true,
'combine-arrays': false,
'dot-notation': true,
'parse-numbers': true,
'boolean-negation': true,
'negation-prefix': 'no-',
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': true,
'greedy-arrays': true,
'halt-at-non-option': false,
'nargs-eats-options': false,
'negation-prefix': 'no-',
'parse-numbers': true,
'populate--': false,
'combine-arrays': false,
'set-placeholder-key': false,
'halt-at-non-option': false,
'short-option-groups': true,
'strip-aliased': false,
'strip-dashed': false,
'unknown-options-as-args': false,
'nargs-eats-options': false
'unknown-options-as-args': false
}, opts.configuration)
const defaults = Object.assign(Object.create(null), opts.default)
const configObjects = opts.configObjects || []
Expand Down Expand Up @@ -412,6 +413,7 @@ function parse (args, opts) {
if (/^-/.test(next) && !negative.test(next) && !isUnknownOptionAsArg(next)) break
i = ii
argsToSet.push(processValue(key, next))
if (!configuration['greedy-arrays']) break
}
}

Expand Down
36 changes: 36 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -3588,4 +3588,40 @@ describe('yargs-parser', function () {
parse.error.message.should.equal('Not enough arguments following: a')
})
})

describe('greedy-arrays=false', () => {
it('does not consume more than one argument after array option', () => {
const argv = parser(['--arr', 'foo', 'bar'], {
array: 'arr',
configuration: {
'greedy-arrays': false
}
})
argv.arr.should.eql(['foo'])
argv._.should.eql(['bar'])
})

it('places argument into array when specified multiple times', () => {
const argv = parser(['--arr', 99, 'foo', '--arr', 'hello', 'bar'], {
array: 'arr',
configuration: {
'greedy-arrays': false
}
})
argv.arr.should.eql([99, 'hello'])
argv._.should.eql(['foo', 'bar'])
})

it('places boolean arguments into array when specified multiple times', () => {
const argv = parser(['--arr', 101, '--arr', 102, '--arr', 'false'], {
array: 'arr',
boolean: 'arr',
configuration: {
'greedy-arrays': false
}
})
argv.arr.should.eql([true, true, false])
argv._.should.eql([101, 102])
})
})
})