Skip to content

Commit 5a7c46a

Browse files
authored
feat: default value is now used if no right-hand value provided for numbers/strings (#156)
BREAKING CHANGE: a flag with no right-hand value no longer populates defaulted options with `undefined`.
1 parent a02b1d5 commit 5a7c46a

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function parse (args, opts) {
200200
setArg(key, next)
201201
i++
202202
} else {
203-
setArg(key, defaultForType(guessType(key, flags)))
203+
setArg(key, defaultValue(key))
204204
}
205205
}
206206

@@ -220,7 +220,7 @@ function parse (args, opts) {
220220
setArg(key, next)
221221
i++
222222
} else {
223-
setArg(key, defaultForType(guessType(key, flags)))
223+
setArg(key, defaultValue(key))
224224
}
225225
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
226226
letters = arg.slice(1, -1).split('')
@@ -267,7 +267,7 @@ function parse (args, opts) {
267267
broken = true
268268
break
269269
} else {
270-
setArg(letters[j], defaultForType(guessType(letters[j], flags)))
270+
setArg(letters[j], defaultValue(letters[j]))
271271
}
272272
}
273273

@@ -293,7 +293,7 @@ function parse (args, opts) {
293293
setArg(key, next)
294294
i++
295295
} else {
296-
setArg(key, defaultForType(guessType(key, flags)))
296+
setArg(key, defaultValue(key))
297297
}
298298
}
299299
}
@@ -749,6 +749,18 @@ function parse (args, opts) {
749749
})
750750
}
751751

752+
// make a best effor to pick a default value
753+
// for an option based on name and type.
754+
function defaultValue (key) {
755+
if (!checkAllAliases(key, flags.bools) &&
756+
!checkAllAliases(key, flags.counts) &&
757+
`${key}` in defaults) {
758+
return defaults[key]
759+
} else {
760+
return defaultForType(guessType(key))
761+
}
762+
}
763+
752764
// return a default value, given the type of a flag.,
753765
// e.g., key of type 'string' will default to '', rather than 'true'.
754766
function defaultForType (type) {
@@ -763,7 +775,7 @@ function parse (args, opts) {
763775
}
764776

765777
// given a flag, enforce a default type.
766-
function guessType (key, flags) {
778+
function guessType (key) {
767779
var type = 'boolean'
768780

769781
if (checkAllAliases(key, flags.strings)) type = 'string'

test/yargs-parser.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe('yargs-parser', function () {
255255
})
256256

257257
// Fixes: https://github.com/bcoe/yargs/issues/68
258-
it('should parse flag arguments with no right-hand-value as strings, if defined as strings', function () {
258+
it('should parse flag arguments with no right-hand value as strings, if defined as strings', function () {
259259
var s = parser([ '-s' ], {
260260
string: ['s']
261261
}).s
@@ -2823,4 +2823,26 @@ describe('yargs-parser', function () {
28232823
args.bar.should.equal('--goodnight moon')
28242824
})
28252825
})
2826+
2827+
// see: https://github.com/yargs/yargs-parser/issues/144
2828+
it('number/string types should use default when no right-hand value', () => {
2829+
let argv = parser([ '--foo' ], {
2830+
number: ['foo'],
2831+
default: {
2832+
foo: 99
2833+
}
2834+
})
2835+
argv.foo.should.equal(99)
2836+
2837+
argv = parser([ '-b' ], {
2838+
alias: {
2839+
bar: 'b'
2840+
},
2841+
string: ['bar'],
2842+
default: {
2843+
bar: 'hello'
2844+
}
2845+
})
2846+
argv.bar.should.equal('hello')
2847+
})
28262848
})

0 commit comments

Comments
 (0)