Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
var nopt = require("../")
2
, test = require('tap').test
3
4
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")
8
t.end()
9
})
10
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, "")
15
t.end()
16
})
17
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"))
23
t.end()
24
})
25
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)
31
t.end()
32
});
33
34
test("other tests", function (t) {
35
36
var util = require("util")
37
, Stream = require("stream")
38
, path = require("path")
39
, url = require("url")
40
41
, shorthands =
42
{ s : ["--loglevel", "silent"]
43
, d : ["--loglevel", "info"]
44
, dd : ["--loglevel", "verbose"]
45
, ddd : ["--loglevel", "silly"]
46
, noreg : ["--no-registry"]
47
, reg : ["--registry"]
48
, "no-reg" : ["--no-registry"]
49
, silent : ["--loglevel", "silent"]
50
, verbose : ["--loglevel", "verbose"]
51
, h : ["--usage"]
52
, H : ["--usage"]
53
, "?" : ["--usage"]
54
, help : ["--usage"]
55
, v : ["--version"]
56
, f : ["--force"]
57
, desc : ["--description"]
58
, "no-desc" : ["--no-description"]
59
, "local" : ["--no-global"]
60
, l : ["--long"]
61
, p : ["--parseable"]
62
, porcelain : ["--parseable"]
63
, g : ["--global"]
64
}
65
66
, types =
67
{ aoa: Array
68
, nullstream: [null, Stream]
69
, date: Date
70
, str: String
71
, browser : String
72
, cache : path
73
, color : ["always", Boolean]
74
, depth : Number
75
, description : Boolean
76
, dev : Boolean
77
, editor : path
78
, force : Boolean
79
, global : Boolean
80
, globalconfig : path
81
, group : [String, Number]
82
, gzipbin : String
83
, logfd : [Number, Stream]
84
, loglevel : ["silent","win","error","warn","info","verbose","silly"]
85
, long : Boolean
86
, "node-version" : [false, String]
87
, npaturl : url
88
, npat : Boolean
89
, "onload-script" : [false, String]
90
, outfd : [Number, Stream]
91
, parseable : Boolean
92
, pre: Boolean
93
, prefix: path
94
, proxy : url
95
, "rebuild-bundle" : Boolean
96
, registry : url
97
, searchopts : String
98
, searchexclude: [null, String]
99
, shell : path
100
, t: [Array, String]
101
, tag : String
102
, tar : String
103
, tmp : path
104
, "unsafe-perm" : Boolean
105
, usage : Boolean
106
, user : String
107
, username : String
108
, userconfig : path
109
, version : Boolean
110
, viewer: path
111
, _exit : Boolean
112
, path: path
113
}
114
115
; [["-v", {version:true}, []]
116
,["---v", {version:true}, []]
117
,["ls -s --no-reg connect -d",
118
{loglevel:"info",registry:null},["ls","connect"]]
119
,["ls ---s foo",{loglevel:"silent"},["ls","foo"]]
120
,["ls --registry blargle", {}, ["ls"]]
121
,["--no-registry", {registry:null}, []]
122
,["--no-color true", {color:false}, []]
123
,["--no-color false", {color:true}, []]
124
,["--no-color", {color:false}, []]
125
,["--color false", {color:false}, []]
126
,["--color --logfd 7", {logfd:7,color:true}, []]
127
,["--color=true", {color:true}, []]
128
,["--logfd=10", {logfd:10}, []]
129
,["--tmp=/tmp -tar=gtar",{tmp:"/tmp",tar:"gtar"},[]]
130
,["--tmp=tmp -tar=gtar",
131
{tmp:path.resolve(process.cwd(), "tmp"),tar:"gtar"},[]]
132
,["--logfd x", {}, []]
133
,["a -true -- -no-false", {true:true},["a","-no-false"]]
134
,["a -no-false", {false:false},["a"]]
135
,["a -no-no-true", {true:true}, ["a"]]
136
,["a -no-no-no-false", {false:false}, ["a"]]
137
,["---NO-no-No-no-no-no-nO-no-no"+
138
"-No-no-no-no-no-no-no-no-no"+
139
"-no-no-no-no-NO-NO-no-no-no-no-no-no"+
140
"-no-body-can-do-the-boogaloo-like-I-do"
141
,{"body-can-do-the-boogaloo-like-I-do":false}, []]
142
,["we are -no-strangers-to-love "+
143
"--you-know=the-rules --and=so-do-i "+
144
"---im-thinking-of=a-full-commitment "+
145
"--no-you-would-get-this-from-any-other-guy "+
146
"--no-gonna-give-you-up "+
147
"-no-gonna-let-you-down=true "+
148
"--no-no-gonna-run-around false "+
149
"--desert-you=false "+
150
"--make-you-cry false "+
151
"--no-tell-a-lie "+
152
"--no-no-and-hurt-you false"
153
,{"strangers-to-love":false
154
,"you-know":"the-rules"
155
,"and":"so-do-i"
156
,"you-would-get-this-from-any-other-guy":false
157
,"gonna-give-you-up":false
158
,"gonna-let-you-down":false
159
,"gonna-run-around":false
160
,"desert-you":false
161
,"make-you-cry":false
162
,"tell-a-lie":false
163
,"and-hurt-you":false
164
},["we", "are"]]
165
,["-t one -t two -t three"
166
,{t: ["one", "two", "three"]}
167
,[]]
168
,["-t one -t null -t three four five null"
169
,{t: ["one", "null", "three"]}
170
,["four", "five", "null"]]
171
,["-t foo"
172
,{t:["foo"]}
173
,[]]
174
,["--no-t"
175
,{t:["false"]}
176
,[]]
177
,["-no-no-t"
178
,{t:["true"]}
179
,[]]
180
,["-aoa one -aoa null -aoa 100"
181
,{aoa:["one", null, '100']}
182
,[]]
183
,["-str 100"
184
,{str:"100"}
185
,[]]
186
,["--color always"
187
,{color:"always"}
188
,[]]
189
,["--no-nullstream"
190
,{nullstream:null}
191
,[]]
192
,["--nullstream false"
193
,{nullstream:null}
194
,[]]
195
,["--notadate=2011-01-25"
196
,{notadate: "2011-01-25"}
197
,[]]
198
,["--date 2011-01-25"
199
,{date: new Date("2011-01-25")}
200
,[]]
201
,["-cl 1"
202
,{config: true, length: 1}
203
,[]
204
,{config: Boolean, length: Number, clear: Boolean}
205
,{c: "--config", l: "--length"}]
206
,["--acount bla"
207
,{"acount":true}
208
,["bla"]
209
,{account: Boolean, credentials: Boolean, options: String}
210
,{a:"--account", c:"--credentials",o:"--options"}]
211
,["--clear"
212
,{clear:true}
213
,[]
214
,{clear:Boolean,con:Boolean,len:Boolean,exp:Boolean,add:Boolean,rep:Boolean}
215
,{c:"--con",l:"--len",e:"--exp",a:"--add",r:"--rep"}]
216
,["--file -"
217
,{"file":"-"}
218
,[]
219
,{file:String}
220
,{}]
221
,["--file -"
222
,{"file":true}
223
,["-"]
224
,{file:Boolean}
225
,{}]
226
,["--path"
227
,{"path":null}
228
,[]]
229
,["--path ."
230
,{"path":process.cwd()}
231
,[]]
232
].forEach(function (test) {
233
var argv = test[0].split(/\s+/)
234
, opts = test[1]
235
, rem = test[2]
236
, actual = nopt(test[3] || types, test[4] || shorthands, argv, 0)
237
, parsed = actual.argv
238
delete actual.argv
239
for (var i in opts) {
240
var e = JSON.stringify(opts[i])
241
, a = JSON.stringify(actual[i] === undefined ? null : actual[i])
242
if (e && typeof e === "object") {
243
t.deepEqual(e, a)
244
} else {
245
t.equal(e, a)
246
}
247
}
248
t.deepEqual(rem, parsed.remain)
249
})
250
t.end()
251
})
252
253