Skip to content

Commit

Permalink
feat: interpret demand() numbers as relative to executing command (ya…
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew authored and bcoe committed Aug 9, 2016
1 parent 882a127 commit 927810c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/validation.js
Expand Up @@ -11,18 +11,19 @@ module.exports = function (yargs, usage, y18n) {
// arguments were provided, i.e., '_'.
self.nonOptionCount = function (argv) {
const demanded = yargs.getDemanded()
const _s = argv._.length
// don't count currently executing commands
const _s = argv._.length - yargs.getContext().commands.length

if (demanded._ && (_s < demanded._.count || _s > demanded._.max)) {
if (demanded._.msg !== undefined) {
usage.fail(demanded._.msg)
} else if (_s < demanded._.count) {
usage.fail(
__('Not enough non-option arguments: got %s, need at least %s', argv._.length, demanded._.count)
__('Not enough non-option arguments: got %s, need at least %s', _s, demanded._.count)
)
} else {
usage.fail(
__('Too many non-option arguments: got %s, maximum of %s', argv._.length, demanded._.max)
__('Too many non-option arguments: got %s, maximum of %s', _s, demanded._.max)
)
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/validation.js
Expand Up @@ -135,6 +135,30 @@ describe('validation tests', function () {
})
.argv
})

it('interprets min relative to command', function () {
var failureMsg
yargs('lint')
.command('lint', 'Lint a file', function (yargs) {
yargs.demand(1).fail(function (msg) {
failureMsg = msg
})
})
.argv
expect(failureMsg).to.equal('Not enough non-option arguments: got 0, need at least 1')
})

it('interprets max relative to command', function () {
var failureMsg
yargs('lint one.js two.js')
.command('lint', 'Lint a file', function (yargs) {
yargs.demand(0, 1).fail(function (msg) {
failureMsg = msg
})
})
.argv
expect(failureMsg).to.equal('Too many non-option arguments: got 2, maximum of 1')
})
})

describe('choices', function () {
Expand Down

0 comments on commit 927810c

Please sign in to comment.