UNPKG

5.31 kBJavaScriptView Raw
1var optimist = require('optimist');
2var assert = require('assert');
3
4exports['short boolean'] = function (assert) {
5 var parse = optimist.parse([ '-b' ]);
6 assert.eql(parse, { b : true, _ : [], $0 : 'expresso' });
7 assert.eql(typeof parse.b, 'boolean');
8};
9
10exports['long boolean'] = function (assert) {
11 assert.eql(
12 optimist.parse([ '--bool' ]),
13 { bool : true, _ : [], $0 : 'expresso' }
14 );
15};
16
17exports.bare = function (assert) {
18 assert.eql(
19 optimist.parse([ 'foo', 'bar', 'baz' ]),
20 { _ : [ 'foo', 'bar', 'baz' ], $0 : 'expresso' }
21 );
22};
23
24exports['short group'] = function (assert) {
25 assert.eql(
26 optimist.parse([ '-cats' ]),
27 { c : true, a : true, t : true, s : true, _ : [], $0 : 'expresso' }
28 );
29};
30
31exports['short group next'] = function (assert) {
32 assert.eql(
33 optimist.parse([ '-cats', 'meow' ]),
34 { c : true, a : true, t : true, s : 'meow', _ : [], $0 : 'expresso' }
35 );
36};
37
38exports['short capture'] = function (assert) {
39 assert.eql(
40 optimist.parse([ '-h', 'localhost' ]),
41 { h : 'localhost', _ : [], $0 : 'expresso' }
42 );
43};
44
45exports['short captures'] = function (assert) {
46 assert.eql(
47 optimist.parse([ '-h', 'localhost', '-p', '555' ]),
48 { h : 'localhost', p : 555, _ : [], $0 : 'expresso' }
49 );
50};
51
52exports['long capture sp'] = function (assert) {
53 assert.eql(
54 optimist.parse([ '--pow', 'xixxle' ]),
55 { pow : 'xixxle', _ : [], $0 : 'expresso' }
56 );
57};
58
59exports['long capture eq'] = function (assert) {
60 assert.eql(
61 optimist.parse([ '--pow=xixxle' ]),
62 { pow : 'xixxle', _ : [], $0 : 'expresso' }
63 );
64};
65
66exports['long captures sp'] = function (assert) {
67 assert.eql(
68 optimist.parse([ '--host', 'localhost', '--port', '555' ]),
69 { host : 'localhost', port : 555, _ : [], $0 : 'expresso' }
70 );
71};
72
73exports['long captures eq'] = function (assert) {
74 assert.eql(
75 optimist.parse([ '--host=localhost', '--port=555' ]),
76 { host : 'localhost', port : 555, _ : [], $0 : 'expresso' }
77 );
78};
79
80exports['mixed short bool and capture'] = function (assert) {
81 assert.eql(
82 optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
83 {
84 f : true, p : 555, h : 'localhost',
85 _ : [ 'script.js' ], $0 : 'expresso',
86 }
87 );
88};
89
90exports['short and long'] = function (assert) {
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
100exports.no = function (assert) {
101 assert.eql(
102 optimist.parse([ '--no-moo' ]),
103 { moo : false, _ : [], $0 : 'expresso' }
104 );
105};
106
107exports.multi = function (assert) {
108 assert.eql(
109 optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
110 { v : ['a','b','c'], _ : [], $0 : 'expresso' }
111 );
112};
113
114exports.comprehensive = function (assert) {
115 assert.eql(
116 optimist.parse([
117 '--name=meowmers', 'bare', '-cats', 'woo',
118 '-h', 'awesome', '--multi=quux',
119 '--key', 'value',
120 '-b', '--bool', '--no-meep', '--multi=baz',
121 '--', '--not-a-flag', 'eek'
122 ]),
123 {
124 c : true,
125 a : true,
126 t : true,
127 s : 'woo',
128 h : 'awesome',
129 b : true,
130 bool : true,
131 key : 'value',
132 multi : [ 'quux', 'baz' ],
133 meep : false,
134 name : 'meowmers',
135 _ : [ 'bare', '--not-a-flag', 'eek' ],
136 $0 : 'expresso'
137 }
138 );
139};
140
141exports.nums = function (assert) {
142 var argv = optimist.parse([
143 '-x', '1234',
144 '-y', '5.67',
145 '-z', '1e7',
146 '-w', '10f',
147 '--hex', '0xdeadbeef',
148 '789',
149 ]);
150 assert.eql(argv, {
151 x : 1234,
152 y : 5.67,
153 z : 1e7,
154 w : '10f',
155 hex : 0xdeadbeef,
156 _ : [ 789 ],
157 $0 : 'expresso'
158 });
159 assert.eql(typeof argv.x, 'number');
160 assert.eql(typeof argv.y, 'number');
161 assert.eql(typeof argv.z, 'number');
162 assert.eql(typeof argv.w, 'string');
163 assert.eql(typeof argv.hex, 'number');
164 assert.eql(typeof argv._[0], 'number');
165};
166
167exports['flag boolean'] = function (assert) {
168 var parse = optimist([ '-t', 'moo' ]).boolean(['t']).argv;
169 assert.eql(parse, { t : true, _ : [ 'moo' ], $0 : 'expresso' });
170 assert.eql(typeof parse.t, 'boolean');
171};
172
173exports['boolean groups'] = function (assert) {
174 var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
175 .boolean(['x','y','z']).argv;
176
177 assert.eql(parse, {
178 x : true,
179 y : false,
180 z : true,
181 _ : [ 'one', 'two', 'three' ],
182 $0 : 'expresso',
183 });
184
185 assert.eql(typeof parse.x, 'boolean');
186 assert.eql(typeof parse.y, 'boolean');
187 assert.eql(typeof parse.z, 'boolean');
188};
189
190exports.strings = function () {
191 var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
192 assert.eql(s, '0001234');
193 assert.eql(typeof s, 'string');
194
195 var x = optimist([ '-x', '56' ]).string('x').argv.x;
196 assert.eql(x, '56');
197 assert.eql(typeof x, 'string');
198};