Skip to content

Commit c679e90

Browse files
edmBernardbcoe
authored andcommitted
fix: 'undefined' should be taken to mean no argument was provided (#1015)
1 parent a70b285 commit c679e90

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/validation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = function validation (yargs, usage, y18n) {
5757
// make sure that any args that require an
5858
// value (--foo=bar), have a value.
5959
self.missingArgumentValue = function missingArgumentValue (argv) {
60-
const defaultValues = [true, false, '']
60+
const defaultValues = [true, false, '', undefined]
6161
const options = yargs.getOptions()
6262

6363
if (options.requiresArg.length > 0) {

test/validation.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,46 @@ describe('validation tests', () => {
369369
.argv
370370
})
371371

372+
it('fails when a required argument of type number is missing', (done) => {
373+
yargs('-w')
374+
.option('w', {type: 'number', requiresArg: true})
375+
.fail((msg) => {
376+
msg.should.equal('Missing argument value: w')
377+
return done()
378+
})
379+
.argv
380+
})
381+
382+
it('fails when a required argument of type string is missing', (done) => {
383+
yargs('-w')
384+
.option('w', {type: 'string', requiresArg: true})
385+
.fail((msg) => {
386+
msg.should.equal('Missing argument value: w')
387+
return done()
388+
})
389+
.argv
390+
})
391+
392+
it('fails when a required argument of type boolean is missing', (done) => {
393+
yargs('-w')
394+
.option('w', {type: 'boolean', requiresArg: true})
395+
.fail((msg) => {
396+
msg.should.equal('Missing argument value: w')
397+
return done()
398+
})
399+
.argv
400+
})
401+
402+
it('fails when a required argument of type array is missing', (done) => {
403+
yargs('-w')
404+
.option('w', {type: 'array', requiresArg: true})
405+
.fail((msg) => {
406+
msg.should.equal('Missing argument value: w')
407+
return done()
408+
})
409+
.argv
410+
})
411+
372412
it('fails when required arguments are present, but a command is missing', (done) => {
373413
yargs('-w 10 -m wombat')
374414
.demand(1, ['w', 'm'])

0 commit comments

Comments
 (0)