Websocket
[VSoRC/.git] / node_modules / node-static / node_modules / optimist / test / parse.js
1 var optimist = require('../index');
2 var path = require('path');
3 var test = require('tap').test;
4
5 var $0 = 'node ./' + path.relative(process.cwd(), __filename);
6
7 test('short boolean', function (t) {
8     var parse = optimist.parse([ '-b' ]);
9     t.same(parse, { b : true, _ : [], $0 : $0 });
10     t.same(typeof parse.b, 'boolean');
11     t.end();
12 });
13
14 test('long boolean', function (t) {
15     t.same(
16         optimist.parse([ '--bool' ]),
17         { bool : true, _ : [], $0 : $0 }
18     );
19     t.end();
20 });
21     
22 test('bare', function (t) {
23     t.same(
24         optimist.parse([ 'foo', 'bar', 'baz' ]),
25         { _ : [ 'foo', 'bar', 'baz' ], $0 : $0 }
26     );
27     t.end();
28 });
29
30 test('short group', function (t) {
31     t.same(
32         optimist.parse([ '-cats' ]),
33         { c : true, a : true, t : true, s : true, _ : [], $0 : $0 }
34     );
35     t.end();
36 });
37
38 test('short group next', function (t) {
39     t.same(
40         optimist.parse([ '-cats', 'meow' ]),
41         { c : true, a : true, t : true, s : 'meow', _ : [], $0 : $0 }
42     );
43     t.end();
44 });
45  
46 test('short capture', function (t) {
47     t.same(
48         optimist.parse([ '-h', 'localhost' ]),
49         { h : 'localhost', _ : [], $0 : $0 }
50     );
51     t.end();
52 });
53
54 test('short captures', function (t) {
55     t.same(
56         optimist.parse([ '-h', 'localhost', '-p', '555' ]),
57         { h : 'localhost', p : 555, _ : [], $0 : $0 }
58     );
59     t.end();
60 });
61
62 test('long capture sp', function (t) {
63     t.same(
64         optimist.parse([ '--pow', 'xixxle' ]),
65         { pow : 'xixxle', _ : [], $0 : $0 }
66     );
67     t.end();
68 });
69
70 test('long capture eq', function (t) {
71     t.same(
72         optimist.parse([ '--pow=xixxle' ]),
73         { pow : 'xixxle', _ : [], $0 : $0 }
74     );
75     t.end()
76 });
77
78 test('long captures sp', function (t) {
79     t.same(
80         optimist.parse([ '--host', 'localhost', '--port', '555' ]),
81         { host : 'localhost', port : 555, _ : [], $0 : $0 }
82     );
83     t.end();
84 });
85
86 test('long captures eq', function (t) {
87     t.same(
88         optimist.parse([ '--host=localhost', '--port=555' ]),
89         { host : 'localhost', port : 555, _ : [], $0 : $0 }
90     );
91     t.end();
92 });
93
94 test('mixed short bool and capture', function (t) {
95     t.same(
96         optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
97         {
98             f : true, p : 555, h : 'localhost',
99             _ : [ 'script.js' ], $0 : $0,
100         }
101     );
102     t.end();
103 });
104  
105 test('short and long', function (t) {
106     t.same(
107         optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
108         {
109             f : true, p : 555, h : 'localhost',
110             _ : [ 'script.js' ], $0 : $0,
111         }
112     );
113     t.end();
114 });
115
116 test('no', function (t) {
117     t.same(
118         optimist.parse([ '--no-moo' ]),
119         { moo : false, _ : [], $0 : $0 }
120     );
121     t.end();
122 });
123  
124 test('multi', function (t) {
125     t.same(
126         optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
127         { v : ['a','b','c'], _ : [], $0 : $0 }
128     );
129     t.end();
130 });
131  
132 test('comprehensive', function (t) {
133     t.same(
134         optimist.parse([
135             '--name=meowmers', 'bare', '-cats', 'woo',
136             '-h', 'awesome', '--multi=quux',
137             '--key', 'value',
138             '-b', '--bool', '--no-meep', '--multi=baz',
139             '--', '--not-a-flag', 'eek'
140         ]),
141         {
142             c : true,
143             a : true,
144             t : true,
145             s : 'woo',
146             h : 'awesome',
147             b : true,
148             bool : true,
149             key : 'value',
150             multi : [ 'quux', 'baz' ],
151             meep : false,
152             name : 'meowmers',
153             _ : [ 'bare', '--not-a-flag', 'eek' ],
154             $0 : $0
155         }
156     );
157     t.end();
158 });
159
160 test('nums', function (t) {
161     var argv = optimist.parse([
162         '-x', '1234',
163         '-y', '5.67',
164         '-z', '1e7',
165         '-w', '10f',
166         '--hex', '0xdeadbeef',
167         '789',
168     ]);
169     t.same(argv, {
170         x : 1234,
171         y : 5.67,
172         z : 1e7,
173         w : '10f',
174         hex : 0xdeadbeef,
175         _ : [ 789 ],
176         $0 : $0
177     });
178     t.same(typeof argv.x, 'number');
179     t.same(typeof argv.y, 'number');
180     t.same(typeof argv.z, 'number');
181     t.same(typeof argv.w, 'string');
182     t.same(typeof argv.hex, 'number');
183     t.same(typeof argv._[0], 'number');
184     t.end();
185 });
186
187 test('flag boolean', function (t) {
188     var parse = optimist([ '-t', 'moo' ]).boolean(['t']).argv;
189     t.same(parse, { t : true, _ : [ 'moo' ], $0 : $0 });
190     t.same(typeof parse.t, 'boolean');
191     t.end();
192 });
193
194 test('flag boolean value', function (t) {
195     var parse = optimist(['--verbose', 'false', 'moo', '-t', 'true'])
196         .boolean(['t', 'verbose']).default('verbose', true).argv;
197     
198     t.same(parse, {
199         verbose: false,
200         t: true,
201         _: ['moo'],
202         $0 : $0
203     });
204     
205     t.same(typeof parse.verbose, 'boolean');
206     t.same(typeof parse.t, 'boolean');
207     t.end();
208 });
209
210 test('flag boolean default false', function (t) {
211     var parse = optimist(['moo'])
212         .boolean(['t', 'verbose'])
213         .default('verbose', false)
214         .default('t', false).argv;
215     
216     t.same(parse, {
217         verbose: false,
218         t: false,
219         _: ['moo'],
220         $0 : $0
221     });
222     
223     t.same(typeof parse.verbose, 'boolean');
224     t.same(typeof parse.t, 'boolean');
225     t.end();
226
227 });
228
229 test('boolean groups', function (t) {
230     var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
231         .boolean(['x','y','z']).argv;
232     
233     t.same(parse, {
234         x : true,
235         y : false,
236         z : true,
237         _ : [ 'one', 'two', 'three' ],
238         $0 : $0
239     });
240     
241     t.same(typeof parse.x, 'boolean');
242     t.same(typeof parse.y, 'boolean');
243     t.same(typeof parse.z, 'boolean');
244     t.end();
245 });
246
247 test('newlines in params' , function (t) {
248     var args = optimist.parse([ '-s', "X\nX" ])
249     t.same(args, { _ : [], s : "X\nX", $0 : $0 });
250
251     // reproduce in bash:
252     // VALUE="new
253     // line"
254     // node program.js --s="$VALUE"
255     args = optimist.parse([ "--s=X\nX" ])
256     t.same(args, { _ : [], s : "X\nX", $0 : $0 });
257     t.end();
258 });
259
260 test('strings' , function (t) {
261     var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
262     t.same(s, '0001234');
263     t.same(typeof s, 'string');
264     
265     var x = optimist([ '-x', '56' ]).string('x').argv.x;
266     t.same(x, '56');
267     t.same(typeof x, 'string');
268     t.end();
269 });
270
271 test('stringArgs', function (t) {
272     var s = optimist([ '  ', '  ' ]).string('_').argv._;
273     t.same(s.length, 2);
274     t.same(typeof s[0], 'string');
275     t.same(s[0], '  ');
276     t.same(typeof s[1], 'string');
277     t.same(s[1], '  ');
278     t.end();
279 });
280
281 test('slashBreak', function (t) {
282     t.same(
283         optimist.parse([ '-I/foo/bar/baz' ]),
284         { I : '/foo/bar/baz', _ : [], $0 : $0 }
285     );
286     t.same(
287         optimist.parse([ '-xyz/foo/bar/baz' ]),
288         { x : true, y : true, z : '/foo/bar/baz', _ : [], $0 : $0 }
289     );
290     t.end();
291 });
292
293 test('alias', function (t) {
294     var argv = optimist([ '-f', '11', '--zoom', '55' ])
295         .alias('z', 'zoom')
296         .argv
297     ;
298     t.equal(argv.zoom, 55);
299     t.equal(argv.z, argv.zoom);
300     t.equal(argv.f, 11);
301     t.end();
302 });
303
304 test('multiAlias', function (t) {
305     var argv = optimist([ '-f', '11', '--zoom', '55' ])
306         .alias('z', [ 'zm', 'zoom' ])
307         .argv
308     ;
309     t.equal(argv.zoom, 55);
310     t.equal(argv.z, argv.zoom);
311     t.equal(argv.z, argv.zm);
312     t.equal(argv.f, 11);
313     t.end();
314 });
315
316 test('boolean default true', function (t) {
317     var argv = optimist.options({
318         sometrue: {
319             boolean: true,
320             default: true
321         }
322     }).argv;
323   
324     t.equal(argv.sometrue, true);
325     t.end();
326 });
327
328 test('boolean default false', function (t) {
329     var argv = optimist.options({
330         somefalse: {
331             boolean: true,
332             default: false
333         }
334     }).argv;
335
336     t.equal(argv.somefalse, false);
337     t.end();
338 });
339
340 test('nested dotted objects', function (t) {
341     var argv = optimist([
342         '--foo.bar', '3', '--foo.baz', '4',
343         '--foo.quux.quibble', '5', '--foo.quux.o_O',
344         '--beep.boop'
345     ]).argv;
346     
347     t.same(argv.foo, {
348         bar : 3,
349         baz : 4,
350         quux : {
351             quibble : 5,
352             o_O : true
353         },
354     });
355     t.same(argv.beep, { boop : true });
356     t.end();
357 });
358
359 test('boolean and alias with chainable api', function (t) {
360     var aliased = [ '-h', 'derp' ];
361     var regular = [ '--herp',  'derp' ];
362     var opts = {
363         herp: { alias: 'h', boolean: true }
364     };
365     var aliasedArgv = optimist(aliased)
366         .boolean('herp')
367         .alias('h', 'herp')
368         .argv;
369     var propertyArgv = optimist(regular)
370         .boolean('herp')
371         .alias('h', 'herp')
372         .argv;
373     var expected = {
374         herp: true,
375         h: true,
376         '_': [ 'derp' ],
377         '$0': $0,
378     };
379
380     t.same(aliasedArgv, expected);
381     t.same(propertyArgv, expected); 
382     t.end();
383 });
384
385 test('boolean and alias with options hash', function (t) {
386     var aliased = [ '-h', 'derp' ];
387     var regular = [ '--herp', 'derp' ];
388     var opts = {
389         herp: { alias: 'h', boolean: true }
390     };
391     var aliasedArgv = optimist(aliased)
392       .options(opts)
393       .argv;
394     var propertyArgv = optimist(regular).options(opts).argv;
395     var expected = {
396         herp: true,
397         h: true,
398         '_': [ 'derp' ],
399         '$0': $0,
400     };
401
402     t.same(aliasedArgv, expected);
403     t.same(propertyArgv, expected);
404
405     t.end();
406 });
407
408 test('boolean and alias using explicit true', function (t) {
409     var aliased = [ '-h', 'true' ];
410     var regular = [ '--herp',  'true' ];
411     var opts = {
412         herp: { alias: 'h', boolean: true }
413     };
414     var aliasedArgv = optimist(aliased)
415         .boolean('h')
416         .alias('h', 'herp')
417         .argv;
418     var propertyArgv = optimist(regular)
419         .boolean('h')
420         .alias('h', 'herp')
421         .argv;
422     var expected = {
423         herp: true,
424         h: true,
425         '_': [ ],
426         '$0': $0,
427     };
428
429     t.same(aliasedArgv, expected);
430     t.same(propertyArgv, expected); 
431     t.end();
432 });
433
434 // regression, see https://github.com/substack/node-optimist/issues/71
435 test('boolean and --x=true', function(t) {
436     var parsed = optimist(['--boool', '--other=true']).boolean('boool').argv;
437
438     t.same(parsed.boool, true);
439     t.same(parsed.other, 'true');
440
441     parsed = optimist(['--boool', '--other=false']).boolean('boool').argv;
442
443     t.same(parsed.boool, true);
444     t.same(parsed.other, 'false');
445     t.end();
446 });