Skip to content

Commit

Permalink
fix: requiresArg should only be enforced if argument exists (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Jan 15, 2018
1 parent 232f9ca commit fbf41ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/validation.js
Expand Up @@ -59,13 +59,15 @@ module.exports = function validation (yargs, usage, y18n) {
self.missingArgumentValue = function missingArgumentValue (argv) {
const defaultValues = [true, false, '', undefined]
const options = yargs.getOptions()

if (options.requiresArg.length > 0) {
const missingRequiredArgs = []

options.requiresArg.forEach((key) => {
const value = argv[key]
// if the argument is not set in argv no need to check
// whether a right-hand-side has been provided.
if (!argv.hasOwnProperty(key)) return

const value = argv[key]
// if a value is explicitly requested,
// flag argument as missing if it does not
// look like foo=bar was entered.
Expand Down
10 changes: 10 additions & 0 deletions test/validation.js
Expand Up @@ -419,6 +419,16 @@ describe('validation tests', () => {
.argv
})

// see: https://github.com/yargs/yargs/issues/1041
it('does not fail if required argument is not provided', (done) => {
yargs('')
.option('w', {type: 'array', requiresArg: true})
.parse('', (err, argv, output) => {
expect(err).to.equal(null)
return done()
})
})

it('fails without a message if msg is null', (done) => {
yargs([])
.demand(1, null)
Expand Down

0 comments on commit fbf41ae

Please sign in to comment.