Skip to content

Commit

Permalink
do not try to convert unknown values to numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbretman committed May 27, 2014
1 parent d5a0601 commit c3b8bf0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ considered valid values. For instance, in the example above, the
and any other value will be rejected.
When parsing unknown fields, `"true"`, `"false"`, and `"null"` will be
interpreted as their JavaScript equivalents, and numeric values will be
interpreted as a number.
interpreted as their JavaScript equivalents.
You can also mix types and values, or multiple types, in a list. For
instance `{ blah: [Number, null] }` would allow a value to be set to
Expand Down
2 changes: 1 addition & 1 deletion lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function nopt (types, shorthands, args, slice) {
function clean (data, types, typeDefs) {
typeDefs = typeDefs || exports.typeDefs
var remove = {}
, typeDefault = [false, true, null, String, Number, Array]
, typeDefault = [false, true, null, String, Array]

Object.keys(data).forEach(function (k) {
if (k === "argv") return
Expand Down
10 changes: 9 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ test("~ path is resolved to $HOME", function (t) {
t.end()
})

// https://github.com/npm/nopt/issues/24
test("Unknown options are not parsed as numbers", function (t) {
var parsed = nopt({"parse-me": Number}, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0)
t.equal(parsed['leave-as-is'], '1.20')
t.equal(parsed['parse-me'], 1.2)
t.end()
});

test("other tests", function (t) {

var util = require("util")
Expand Down Expand Up @@ -170,7 +178,7 @@ test("other tests", function (t) {
,{t:["true"]}
,[]]
,["-aoa one -aoa null -aoa 100"
,{aoa:["one", null, 100]}
,{aoa:["one", null, '100']}
,[]]
,["-str 100"
,{str:"100"}
Expand Down

0 comments on commit c3b8bf0

Please sign in to comment.