Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var optimist = require('../index');
2
var assert = require('assert');
3
var path = require('path');
4
5
var localExpresso = path.normalize(
6
__dirname + '/../node_modules/.bin/expresso'
7
);
8
9
var expresso = process.argv[1] === localExpresso
10
? 'node ./node_modules/.bin/expresso'
11
: 'expresso'
12
;
13
14
exports['short boolean'] = function () {
15
var parse = optimist.parse([ '-b' ]);
16
assert.eql(parse, { b : true, _ : [], $0 : expresso });
17
assert.eql(typeof parse.b, 'boolean');
18
};
19
20
exports['long boolean'] = function () {
21
assert.eql(
22
optimist.parse([ '--bool' ]),
23
{ bool : true, _ : [], $0 : expresso }
24
);
25
};
26
27
exports.bare = function () {
28
assert.eql(
29
optimist.parse([ 'foo', 'bar', 'baz' ]),
30
{ _ : [ 'foo', 'bar', 'baz' ], $0 : expresso }
31
);
32
};
33
34
exports['short group'] = function () {
35
assert.eql(
36
optimist.parse([ '-cats' ]),
37
{ c : true, a : true, t : true, s : true, _ : [], $0 : expresso }
38
);
39
};
40
41
exports['short group next'] = function () {
42
assert.eql(
43
optimist.parse([ '-cats', 'meow' ]),
44
{ c : true, a : true, t : true, s : 'meow', _ : [], $0 : expresso }
45
);
46
};
47
48
exports['short capture'] = function () {
49
assert.eql(
50
optimist.parse([ '-h', 'localhost' ]),
51
{ h : 'localhost', _ : [], $0 : expresso }
52
);
53
};
54
55
exports['short captures'] = function () {
56
assert.eql(
57
optimist.parse([ '-h', 'localhost', '-p', '555' ]),
58
{ h : 'localhost', p : 555, _ : [], $0 : expresso }
59
);
60
};
61
62
exports['long capture sp'] = function () {
63
assert.eql(
64
optimist.parse([ '--pow', 'xixxle' ]),
65
{ pow : 'xixxle', _ : [], $0 : expresso }
66
);
67
};
68
69
exports['long capture eq'] = function () {
70
assert.eql(
71
optimist.parse([ '--pow=xixxle' ]),
72
{ pow : 'xixxle', _ : [], $0 : expresso }
73
);
74
};
75
76
exports['long captures sp'] = function () {
77
assert.eql(
78
optimist.parse([ '--host', 'localhost', '--port', '555' ]),
79
{ host : 'localhost', port : 555, _ : [], $0 : expresso }
80
);
81
};
82
83
exports['long captures eq'] = function () {
84
assert.eql(
85
optimist.parse([ '--host=localhost', '--port=555' ]),
86
{ host : 'localhost', port : 555, _ : [], $0 : expresso }
87
);
88
};
89
90
exports['mixed short bool and capture'] = function () {
91
assert.eql(
92
optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
93
{
94
f : true, p : 555, h : 'localhost',
95
_ : [ 'script.js' ], $0 : expresso,
96
}
97
);
98
};
99
100
exports['short and long'] = function () {
101
assert.eql(
102
optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
103
{
104
f : true, p : 555, h : 'localhost',
105
_ : [ 'script.js' ], $0 : expresso,
106
}
107
);
108
};
109
110
exports.no = function () {
111
assert.eql(
112
optimist.parse([ '--no-moo' ]),
113
{ moo : false, _ : [], $0 : expresso }
114
);
115
};
116
117
exports.multi = function () {
118
assert.eql(
119
optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
120
{ v : ['a','b','c'], _ : [], $0 : expresso }
121
);
122
};
123
124
exports.comprehensive = function () {
125
assert.eql(
126
optimist.parse([
127
'--name=meowmers', 'bare', '-cats', 'woo',
128
'-h', 'awesome', '--multi=quux',
129
'--key', 'value',
130
'-b', '--bool', '--no-meep', '--multi=baz',
131
'--', '--not-a-flag', 'eek'
132
]),
133
{
134
c : true,
135
a : true,
136
t : true,
137
s : 'woo',
138
h : 'awesome',
139
b : true,
140
bool : true,
141
key : 'value',
142
multi : [ 'quux', 'baz' ],
143
meep : false,
144
name : 'meowmers',
145
_ : [ 'bare', '--not-a-flag', 'eek' ],
146
$0 : expresso
147
}
148
);
149
};
150
151
exports.nums = function () {
152
var argv = optimist.parse([
153
'-x', '1234',
154
'-y', '5.67',
155
'-z', '1e7',
156
'-w', '10f',
157
'--hex', '0xdeadbeef',
158
'789',
159
]);
160
assert.eql(argv, {
161
x : 1234,
162
y : 5.67,
163
z : 1e7,
164
w : '10f',
165
hex : 0xdeadbeef,
166
_ : [ 789 ],
167
$0 : expresso
168
});
169
assert.eql(typeof argv.x, 'number');
170
assert.eql(typeof argv.y, 'number');
171
assert.eql(typeof argv.z, 'number');
172
assert.eql(typeof argv.w, 'string');
173
assert.eql(typeof argv.hex, 'number');
174
assert.eql(typeof argv._[0], 'number');
175
};
176
177
exports['flag boolean'] = function () {
178
var parse = optimist([ '-t', 'moo' ]).boolean(['t']).argv;
179
assert.eql(parse, { t : true, _ : [ 'moo' ], $0 : expresso });
180
assert.eql(typeof parse.t, 'boolean');
181
};
182
183
exports['flag boolean value'] = function () {
184
var parse = optimist(['--verbose', 'false', 'moo', '-t', 'true'])
185
.boolean(['t', 'verbose']).default('verbose', true).argv;
186
187
assert.eql(parse, {
188
verbose: false,
189
t: true,
190
_: ['moo'],
191
$0 : expresso
192
});
193
194
assert.eql(typeof parse.verbose, 'boolean');
195
assert.eql(typeof parse.t, 'boolean');
196
};
197
198
exports['flag boolean default false'] = function () {
199
var parse = optimist(['moo'])
200
.boolean(['t', 'verbose'])
201
.default('verbose', false)
202
.default('t', false).argv;
203
204
assert.eql(parse, {
205
verbose: false,
206
t: false,
207
_: ['moo'],
208
$0 : expresso
209
});
210
211
assert.eql(typeof parse.verbose, 'boolean');
212
assert.eql(typeof parse.t, 'boolean');
213
214
};
215
216
exports['boolean groups'] = function () {
217
var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
218
.boolean(['x','y','z']).argv;
219
220
assert.eql(parse, {
221
x : true,
222
y : false,
223
z : true,
224
_ : [ 'one', 'two', 'three' ],
225
$0 : expresso
226
});
227
228
assert.eql(typeof parse.x, 'boolean');
229
assert.eql(typeof parse.y, 'boolean');
230
assert.eql(typeof parse.z, 'boolean');
231
};
232
233
exports.strings = function () {
234
var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
235
assert.eql(s, '0001234');
236
assert.eql(typeof s, 'string');
237
238
var x = optimist([ '-x', '56' ]).string('x').argv.x;
239
assert.eql(x, '56');
240
assert.eql(typeof x, 'string');
241
};
242
243
exports.stringArgs = function () {
244
var s = optimist([ ' ', ' ' ]).string('_').argv._;
245
assert.eql(s.length, 2);
246
assert.eql(typeof s[0], 'string');
247
assert.eql(s[0], ' ');
248
assert.eql(typeof s[1], 'string');
249
assert.eql(s[1], ' ');
250
};
251
252
exports.slashBreak = function () {
253
assert.eql(
254
optimist.parse([ '-I/foo/bar/baz' ]),
255
{ I : '/foo/bar/baz', _ : [], $0 : expresso }
256
);
257
assert.eql(
258
optimist.parse([ '-xyz/foo/bar/baz' ]),
259
{ x : true, y : true, z : '/foo/bar/baz', _ : [], $0 : expresso }
260
);
261
};
262
263
exports.alias = function () {
264
var argv = optimist([ '-f', '11', '--zoom', '55' ])
265
.alias('z', 'zoom')
266
.argv
267
;
268
assert.equal(argv.zoom, 55);
269
assert.equal(argv.z, argv.zoom);
270
assert.equal(argv.f, 11);
271
};
272
273
exports.multiAlias = function () {
274
var argv = optimist([ '-f', '11', '--zoom', '55' ])
275
.alias('z', [ 'zm', 'zoom' ])
276
.argv
277
;
278
assert.equal(argv.zoom, 55);
279
assert.equal(argv.z, argv.zoom);
280
assert.equal(argv.z, argv.zm);
281
assert.equal(argv.f, 11);
282
};
283
284
exports['boolean default true'] = function () {
285
var argv = optimist.options({
286
sometrue: {
287
boolean: true,
288
default: true
289
}
290
}).argv;
291
292
assert.equal(argv.sometrue, true);
293
};
294
295
exports['boolean default false'] = function () {
296
var argv = optimist.options({
297
somefalse: {
298
boolean: true,
299
default: false
300
}
301
}).argv;
302
303
assert.equal(argv.somefalse, false);
304
};
305
306