Skip to content

Commit fbf41ae

Browse files
authored
fix: requiresArg should only be enforced if argument exists (#1043)
1 parent 232f9ca commit fbf41ae

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎lib/validation.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ module.exports = function validation (yargs, usage, y18n) {
5959
self.missingArgumentValue = function missingArgumentValue (argv) {
6060
const defaultValues = [true, false, '', undefined]
6161
const options = yargs.getOptions()
62-
6362
if (options.requiresArg.length > 0) {
6463
const missingRequiredArgs = []
6564

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

70+
const value = argv[key]
6971
// if a value is explicitly requested,
7072
// flag argument as missing if it does not
7173
// look like foo=bar was entered.

‎test/validation.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ describe('validation tests', () => {
419419
.argv
420420
})
421421

422+
// see: https://github.com/yargs/yargs/issues/1041
423+
it('does not fail if required argument is not provided', (done) => {
424+
yargs('')
425+
.option('w', {type: 'array', requiresArg: true})
426+
.parse('', (err, argv, output) => {
427+
expect(err).to.equal(null)
428+
return done()
429+
})
430+
})
431+
422432
it('fails without a message if msg is null', (done) => {
423433
yargs([])
424434
.demand(1, null)

0 commit comments

Comments
 (0)