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

fix: requiresArg should only be enforced if argument exists #1043

Merged
merged 2 commits into from
Jan 15, 2018
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
6 changes: 4 additions & 2 deletions lib/validation.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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