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

feat: default value is now used if no right-hand value provided for numbers/strings #156

Merged
merged 1 commit into from
Jan 28, 2019
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
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')
})
})