Skip to content

Commit

Permalink
feat: default value is now used if no right-hand value provided for n…
Browse files Browse the repository at this point in the history
…umbers/strings (#156)

BREAKING CHANGE: a flag with no right-hand value no longer populates defaulted options with `undefined`.
  • Loading branch information
bcoe committed Jan 28, 2019
1 parent a02b1d5 commit 5a7c46a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
22 changes: 17 additions & 5 deletions index.js
Expand Up @@ -200,7 +200,7 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
setArg(key, defaultForType(guessType(key, flags)))
setArg(key, defaultValue(key))
}
}

Expand All @@ -220,7 +220,7 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
setArg(key, defaultForType(guessType(key, flags)))
setArg(key, defaultValue(key))
}
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
letters = arg.slice(1, -1).split('')
Expand Down Expand Up @@ -267,7 +267,7 @@ function parse (args, opts) {
broken = true
break
} else {
setArg(letters[j], defaultForType(guessType(letters[j], flags)))
setArg(letters[j], defaultValue(letters[j]))
}
}

Expand All @@ -293,7 +293,7 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
setArg(key, defaultForType(guessType(key, flags)))
setArg(key, defaultValue(key))
}
}
}
Expand Down Expand Up @@ -749,6 +749,18 @@ function parse (args, opts) {
})
}

// make a best effor to pick a default value
// for an option based on name and type.
function defaultValue (key) {
if (!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts) &&
`${key}` in defaults) {
return defaults[key]
} else {
return defaultForType(guessType(key))
}
}

// return a default value, given the type of a flag.,
// e.g., key of type 'string' will default to '', rather than 'true'.
function defaultForType (type) {
Expand All @@ -763,7 +775,7 @@ function parse (args, opts) {
}

// given a flag, enforce a default type.
function guessType (key, flags) {
function guessType (key) {
var type = 'boolean'

if (checkAllAliases(key, flags.strings)) type = 'string'
Expand Down
24 changes: 23 additions & 1 deletion test/yargs-parser.js
Expand Up @@ -255,7 +255,7 @@ describe('yargs-parser', function () {
})

// Fixes: https://github.com/bcoe/yargs/issues/68
it('should parse flag arguments with no right-hand-value as strings, if defined as strings', function () {
it('should parse flag arguments with no right-hand value as strings, if defined as strings', function () {
var s = parser([ '-s' ], {
string: ['s']
}).s
Expand Down Expand Up @@ -2823,4 +2823,26 @@ describe('yargs-parser', function () {
args.bar.should.equal('--goodnight moon')
})
})

// see: https://github.com/yargs/yargs-parser/issues/144
it('number/string types should use default when no right-hand value', () => {
let argv = parser([ '--foo' ], {
number: ['foo'],
default: {
foo: 99
}
})
argv.foo.should.equal(99)

argv = parser([ '-b' ], {
alias: {
bar: 'b'
},
string: ['bar'],
default: {
bar: 'hello'
}
})
argv.bar.should.equal('hello')
})
})

0 comments on commit 5a7c46a

Please sign in to comment.