1 var nopt = require("../")
2 , test = require('tap').test
5 test("passing a string results in a string", function (t) {
6 var parsed = nopt({ key: String }, {}, ["--key", "myvalue"], 0)
7 t.same(parsed.key, "myvalue")
11 // https://github.com/npm/nopt/issues/31
12 test("Empty String results in empty string, not true", function (t) {
13 var parsed = nopt({ empty: String }, {}, ["--empty"], 0)
14 t.same(parsed.empty, "")
18 test("~ path is resolved to $HOME", function (t) {
19 var path = require("path")
20 if (!process.env.HOME) process.env.HOME = "/tmp"
21 var parsed = nopt({key: path}, {}, ["--key=~/val"], 0)
22 t.same(parsed.key, path.resolve(process.env.HOME, "val"))
26 // https://github.com/npm/nopt/issues/24
27 test("Unknown options are not parsed as numbers", function (t) {
28 var parsed = nopt({"parse-me": Number}, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0)
29 t.equal(parsed['leave-as-is'], '1.20')
30 t.equal(parsed['parse-me'], 1.2)
34 // https://github.com/npm/nopt/issues/48
35 test("Check types based on name of type", function (t) {
36 var parsed = nopt({"parse-me": {name: "Number"}}, null, ['--parse-me=1.20'], 0)
37 t.equal(parsed['parse-me'], 1.2)
42 test("Missing types are not parsed", function (t) {
43 var parsed = nopt({"parse-me": {}}, null, ['--parse-me=1.20'], 0)
44 //should only contain argv
45 t.equal(Object.keys(parsed).length, 1)
49 test("Types passed without a name are not parsed", function (t) {
50 var parsed = nopt({"parse-me": {}}, {}, ['--parse-me=1.20'], 0)
51 //should only contain argv
52 t.equal(Object.keys(parsed).length, 1)
56 test("other tests", function (t) {
58 var util = require("util")
59 , Stream = require("stream")
60 , path = require("path")
61 , url = require("url")
64 { s : ["--loglevel", "silent"]
65 , d : ["--loglevel", "info"]
66 , dd : ["--loglevel", "verbose"]
67 , ddd : ["--loglevel", "silly"]
68 , noreg : ["--no-registry"]
69 , reg : ["--registry"]
70 , "no-reg" : ["--no-registry"]
71 , silent : ["--loglevel", "silent"]
72 , verbose : ["--loglevel", "verbose"]
79 , desc : ["--description"]
80 , "no-desc" : ["--no-description"]
81 , "local" : ["--no-global"]
84 , porcelain : ["--parseable"]
90 , nullstream: [null, Stream]
95 , color : ["always", Boolean]
97 , description : Boolean
102 , globalconfig : path
103 , group : [String, Number]
105 , logfd : [Number, Stream]
106 , loglevel : ["silent","win","error","warn","info","verbose","silly"]
108 , "node-version" : [false, String]
111 , "onload-script" : [false, String]
112 , outfd : [Number, Stream]
113 , parseable : Boolean
117 , "rebuild-bundle" : Boolean
119 , searchopts : String
120 , searchexclude: [null, String]
126 , "unsafe-perm" : Boolean
137 ; [["-v", {version:true}, []]
138 ,["---v", {version:true}, []]
139 ,["ls -s --no-reg connect -d",
140 {loglevel:"info",registry:null},["ls","connect"]]
141 ,["ls ---s foo",{loglevel:"silent"},["ls","foo"]]
142 ,["ls --registry blargle", {}, ["ls"]]
143 ,["--no-registry", {registry:null}, []]
144 ,["--no-color true", {color:false}, []]
145 ,["--no-color false", {color:true}, []]
146 ,["--no-color", {color:false}, []]
147 ,["--color false", {color:false}, []]
148 ,["--color --logfd 7", {logfd:7,color:true}, []]
149 ,["--color=true", {color:true}, []]
150 ,["--logfd=10", {logfd:10}, []]
151 ,["--tmp=/tmp -tar=gtar",{tmp:"/tmp",tar:"gtar"},[]]
152 ,["--tmp=tmp -tar=gtar",
153 {tmp:path.resolve(process.cwd(), "tmp"),tar:"gtar"},[]]
154 ,["--logfd x", {}, []]
155 ,["a -true -- -no-false", {true:true},["a","-no-false"]]
156 ,["a -no-false", {false:false},["a"]]
157 ,["a -no-no-true", {true:true}, ["a"]]
158 ,["a -no-no-no-false", {false:false}, ["a"]]
159 ,["---NO-no-No-no-no-no-nO-no-no"+
160 "-No-no-no-no-no-no-no-no-no"+
161 "-no-no-no-no-NO-NO-no-no-no-no-no-no"+
162 "-no-body-can-do-the-boogaloo-like-I-do"
163 ,{"body-can-do-the-boogaloo-like-I-do":false}, []]
164 ,["we are -no-strangers-to-love "+
165 "--you-know=the-rules --and=so-do-i "+
166 "---im-thinking-of=a-full-commitment "+
167 "--no-you-would-get-this-from-any-other-guy "+
168 "--no-gonna-give-you-up "+
169 "-no-gonna-let-you-down=true "+
170 "--no-no-gonna-run-around false "+
171 "--desert-you=false "+
172 "--make-you-cry false "+
174 "--no-no-and-hurt-you false"
175 ,{"strangers-to-love":false
176 ,"you-know":"the-rules"
178 ,"you-would-get-this-from-any-other-guy":false
179 ,"gonna-give-you-up":false
180 ,"gonna-let-you-down":false
181 ,"gonna-run-around":false
183 ,"make-you-cry":false
185 ,"and-hurt-you":false
187 ,["-t one -t two -t three"
188 ,{t: ["one", "two", "three"]}
190 ,["-t one -t null -t three four five null"
191 ,{t: ["one", "null", "three"]}
192 ,["four", "five", "null"]]
202 ,["-aoa one -aoa null -aoa 100"
203 ,{aoa:["one", null, '100']}
214 ,["--nullstream false"
217 ,["--notadate=2011-01-25"
218 ,{notadate: "2011-01-25"}
220 ,["--date 2011-01-25"
221 ,{date: new Date("2011-01-25")}
224 ,{config: true, length: 1}
226 ,{config: Boolean, length: Number, clear: Boolean}
227 ,{c: "--config", l: "--length"}]
231 ,{account: Boolean, credentials: Boolean, options: String}
232 ,{a:"--account", c:"--credentials",o:"--options"}]
236 ,{clear:Boolean,con:Boolean,len:Boolean,exp:Boolean,add:Boolean,rep:Boolean}
237 ,{c:"--con",l:"--len",e:"--exp",a:"--add",r:"--rep"}]
252 ,{"path":process.cwd()}
254 ].forEach(function (test) {
255 var argv = test[0].split(/\s+/)
258 , actual = nopt(test[3] || types, test[4] || shorthands, argv, 0)
259 , parsed = actual.argv
261 for (var i in opts) {
262 var e = JSON.stringify(opts[i])
263 , a = JSON.stringify(actual[i] === undefined ? null : actual[i])
264 if (e && typeof e === "object") {
270 t.deepEqual(rem, parsed.remain)