1 var optimist = require('../index');
2 var path = require('path');
3 var test = require('tap').test;
5 var $0 = 'node ./' + path.relative(process.cwd(), __filename);
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');
14 test('long boolean', function (t) {
16 optimist.parse([ '--bool' ]),
17 { bool : true, _ : [], $0 : $0 }
22 test('bare', function (t) {
24 optimist.parse([ 'foo', 'bar', 'baz' ]),
25 { _ : [ 'foo', 'bar', 'baz' ], $0 : $0 }
30 test('short group', function (t) {
32 optimist.parse([ '-cats' ]),
33 { c : true, a : true, t : true, s : true, _ : [], $0 : $0 }
38 test('short group next', function (t) {
40 optimist.parse([ '-cats', 'meow' ]),
41 { c : true, a : true, t : true, s : 'meow', _ : [], $0 : $0 }
46 test('short capture', function (t) {
48 optimist.parse([ '-h', 'localhost' ]),
49 { h : 'localhost', _ : [], $0 : $0 }
54 test('short captures', function (t) {
56 optimist.parse([ '-h', 'localhost', '-p', '555' ]),
57 { h : 'localhost', p : 555, _ : [], $0 : $0 }
62 test('long capture sp', function (t) {
64 optimist.parse([ '--pow', 'xixxle' ]),
65 { pow : 'xixxle', _ : [], $0 : $0 }
70 test('long capture eq', function (t) {
72 optimist.parse([ '--pow=xixxle' ]),
73 { pow : 'xixxle', _ : [], $0 : $0 }
78 test('long captures sp', function (t) {
80 optimist.parse([ '--host', 'localhost', '--port', '555' ]),
81 { host : 'localhost', port : 555, _ : [], $0 : $0 }
86 test('long captures eq', function (t) {
88 optimist.parse([ '--host=localhost', '--port=555' ]),
89 { host : 'localhost', port : 555, _ : [], $0 : $0 }
94 test('mixed short bool and capture', function (t) {
96 optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
98 f : true, p : 555, h : 'localhost',
99 _ : [ 'script.js' ], $0 : $0,
105 test('short and long', function (t) {
107 optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
109 f : true, p : 555, h : 'localhost',
110 _ : [ 'script.js' ], $0 : $0,
116 test('no', function (t) {
118 optimist.parse([ '--no-moo' ]),
119 { moo : false, _ : [], $0 : $0 }
124 test('multi', function (t) {
126 optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
127 { v : ['a','b','c'], _ : [], $0 : $0 }
132 test('comprehensive', function (t) {
135 '--name=meowmers', 'bare', '-cats', 'woo',
136 '-h', 'awesome', '--multi=quux',
138 '-b', '--bool', '--no-meep', '--multi=baz',
139 '--', '--not-a-flag', 'eek'
150 multi : [ 'quux', 'baz' ],
153 _ : [ 'bare', '--not-a-flag', 'eek' ],
160 test('nums', function (t) {
161 var argv = optimist.parse([
166 '--hex', '0xdeadbeef',
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');
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');
194 test('flag boolean value', function (t) {
195 var parse = optimist(['--verbose', 'false', 'moo', '-t', 'true'])
196 .boolean(['t', 'verbose']).default('verbose', true).argv;
205 t.same(typeof parse.verbose, 'boolean');
206 t.same(typeof parse.t, 'boolean');
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;
223 t.same(typeof parse.verbose, 'boolean');
224 t.same(typeof parse.t, 'boolean');
229 test('boolean groups', function (t) {
230 var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
231 .boolean(['x','y','z']).argv;
237 _ : [ 'one', 'two', 'three' ],
241 t.same(typeof parse.x, 'boolean');
242 t.same(typeof parse.y, 'boolean');
243 t.same(typeof parse.z, 'boolean');
247 test('newlines in params' , function (t) {
248 var args = optimist.parse([ '-s', "X\nX" ])
249 t.same(args, { _ : [], s : "X\nX", $0 : $0 });
251 // reproduce in bash:
254 // node program.js --s="$VALUE"
255 args = optimist.parse([ "--s=X\nX" ])
256 t.same(args, { _ : [], s : "X\nX", $0 : $0 });
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');
265 var x = optimist([ '-x', '56' ]).string('x').argv.x;
267 t.same(typeof x, 'string');
271 test('stringArgs', function (t) {
272 var s = optimist([ ' ', ' ' ]).string('_').argv._;
274 t.same(typeof s[0], 'string');
276 t.same(typeof s[1], 'string');
281 test('slashBreak', function (t) {
283 optimist.parse([ '-I/foo/bar/baz' ]),
284 { I : '/foo/bar/baz', _ : [], $0 : $0 }
287 optimist.parse([ '-xyz/foo/bar/baz' ]),
288 { x : true, y : true, z : '/foo/bar/baz', _ : [], $0 : $0 }
293 test('alias', function (t) {
294 var argv = optimist([ '-f', '11', '--zoom', '55' ])
298 t.equal(argv.zoom, 55);
299 t.equal(argv.z, argv.zoom);
304 test('multiAlias', function (t) {
305 var argv = optimist([ '-f', '11', '--zoom', '55' ])
306 .alias('z', [ 'zm', 'zoom' ])
309 t.equal(argv.zoom, 55);
310 t.equal(argv.z, argv.zoom);
311 t.equal(argv.z, argv.zm);
316 test('boolean default true', function (t) {
317 var argv = optimist.options({
324 t.equal(argv.sometrue, true);
328 test('boolean default false', function (t) {
329 var argv = optimist.options({
336 t.equal(argv.somefalse, false);
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',
355 t.same(argv.beep, { boop : true });
359 test('boolean and alias with chainable api', function (t) {
360 var aliased = [ '-h', 'derp' ];
361 var regular = [ '--herp', 'derp' ];
363 herp: { alias: 'h', boolean: true }
365 var aliasedArgv = optimist(aliased)
369 var propertyArgv = optimist(regular)
380 t.same(aliasedArgv, expected);
381 t.same(propertyArgv, expected);
385 test('boolean and alias with options hash', function (t) {
386 var aliased = [ '-h', 'derp' ];
387 var regular = [ '--herp', 'derp' ];
389 herp: { alias: 'h', boolean: true }
391 var aliasedArgv = optimist(aliased)
394 var propertyArgv = optimist(regular).options(opts).argv;
402 t.same(aliasedArgv, expected);
403 t.same(propertyArgv, expected);
408 test('boolean and alias using explicit true', function (t) {
409 var aliased = [ '-h', 'true' ];
410 var regular = [ '--herp', 'true' ];
412 herp: { alias: 'h', boolean: true }
414 var aliasedArgv = optimist(aliased)
418 var propertyArgv = optimist(regular)
429 t.same(aliasedArgv, expected);
430 t.same(propertyArgv, expected);
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;
438 t.same(parsed.boool, true);
439 t.same(parsed.other, 'true');
441 parsed = optimist(['--boool', '--other=false']).boolean('boool').argv;
443 t.same(parsed.boool, true);
444 t.same(parsed.other, 'false');