yargs 1.3.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -30
- package/index.js +99 -71
- package/lib/{minimist.js → parser.js} +93 -38
- package/package.json +46 -6
- package/.npmignore +0 -3
- package/.travis.yml +0 -3
- package/example/bool.js +0 -10
- package/example/boolean_double.js +0 -7
- package/example/boolean_single.js +0 -7
- package/example/count.js +0 -15
- package/example/default_hash.js +0 -8
- package/example/default_singles.js +0 -7
- package/example/demand_count.js +0 -5
- package/example/divide.js +0 -8
- package/example/help.js +0 -27
- package/example/implies.js +0 -10
- package/example/implies_hash.js +0 -26
- package/example/line_count.js +0 -20
- package/example/line_count_options.js +0 -29
- package/example/line_count_wrap.js +0 -29
- package/example/nonopt.js +0 -4
- package/example/requires_arg.js +0 -19
- package/example/short.js +0 -3
- package/example/strict.js +0 -19
- package/example/string.js +0 -11
- package/example/usage-options.js +0 -19
- package/example/xup.js +0 -9
- package/lib/wordwrap.js +0 -50
- package/test/_/bin.js +0 -3
- package/test/_.js +0 -64
- package/test/config.json +0 -5
- package/test/count.js +0 -28
- package/test/dash.js +0 -35
- package/test/mocha.opts +0 -1
- package/test/parse.js +0 -355
- package/test/parse_camelCase.js +0 -129
- package/test/parse_defaults.js +0 -82
- package/test/parse_modified.js +0 -21
- package/test/short.js +0 -20
- package/test/usage.js +0 -747
- package/test/whitespace.js +0 -12
package/test/usage.js
DELETED
|
@@ -1,747 +0,0 @@
|
|
|
1
|
-
var should = require('chai').should(),
|
|
2
|
-
Hash = require('hashish'),
|
|
3
|
-
yargs = require('../');
|
|
4
|
-
|
|
5
|
-
describe('usage', function () {
|
|
6
|
-
|
|
7
|
-
describe('demand options', function () {
|
|
8
|
-
describe('using .demand()', function () {
|
|
9
|
-
it ('should show an error along with the missing arguments on demand fail', function () {
|
|
10
|
-
var r = checkUsage(function () {
|
|
11
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
12
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
13
|
-
.demand(['x','y'])
|
|
14
|
-
.argv;
|
|
15
|
-
});
|
|
16
|
-
r.result.should.have.property('x', 10);
|
|
17
|
-
r.result.should.have.property('z', 20);
|
|
18
|
-
r.result.should.have.property('_').with.length(0);
|
|
19
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
20
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
21
|
-
'Options:',
|
|
22
|
-
' -x [required]',
|
|
23
|
-
' -y [required]',
|
|
24
|
-
'Missing required arguments: y'
|
|
25
|
-
]);
|
|
26
|
-
r.logs.should.have.length(0);
|
|
27
|
-
r.exit.should.be.ok;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('using .require()', function() {
|
|
31
|
-
it ('should show an error along with the missing arguments on demand fail', function () {
|
|
32
|
-
var r = checkUsage(function () {
|
|
33
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
34
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
35
|
-
.require(['x','y'])
|
|
36
|
-
.argv;
|
|
37
|
-
});
|
|
38
|
-
r.result.should.have.property('x', 10);
|
|
39
|
-
r.result.should.have.property('z', 20);
|
|
40
|
-
r.result.should.have.property('_').with.length(0);
|
|
41
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
42
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
43
|
-
'Options:',
|
|
44
|
-
' -x [required]',
|
|
45
|
-
' -y [required]',
|
|
46
|
-
'Missing required arguments: y'
|
|
47
|
-
]);
|
|
48
|
-
r.logs.should.have.length(0);
|
|
49
|
-
r.exit.should.be.ok;
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('should show an error along with a custom message on demand fail', function () {
|
|
55
|
-
var r = checkUsage(function () {
|
|
56
|
-
return yargs('-z 20'.split(' '))
|
|
57
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
58
|
-
.demand(['x','y'], 'x and y are both required to multiply all the things')
|
|
59
|
-
.argv;
|
|
60
|
-
});
|
|
61
|
-
r.result.should.have.property('z', 20);
|
|
62
|
-
r.result.should.have.property('_').with.length(0);
|
|
63
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
64
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
65
|
-
'Options:',
|
|
66
|
-
' -x [required]',
|
|
67
|
-
' -y [required]',
|
|
68
|
-
'Missing required arguments: x, y',
|
|
69
|
-
'x and y are both required to multiply all the things'
|
|
70
|
-
]);
|
|
71
|
-
r.logs.should.have.length(0);
|
|
72
|
-
r.exit.should.be.ok;
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should return valid values when demand passes', function () {
|
|
76
|
-
var r = checkUsage(function () {
|
|
77
|
-
return yargs('-x 10 -y 20'.split(' '))
|
|
78
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
79
|
-
.demand(['x','y'])
|
|
80
|
-
.argv;
|
|
81
|
-
});
|
|
82
|
-
r.should.have.property('result');
|
|
83
|
-
r.result.should.have.property('x', 10);
|
|
84
|
-
r.result.should.have.property('y', 20)
|
|
85
|
-
r.result.should.have.property('_').with.length(0);
|
|
86
|
-
r.should.have.property('errors').with.length(0);
|
|
87
|
-
r.should.have.property('logs').with.length(0);
|
|
88
|
-
r.should.have.property('exit', false);
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should return valid values when check passes', function () {
|
|
93
|
-
var r = checkUsage(function () {
|
|
94
|
-
return yargs('-x 10 -y 20'.split(' '))
|
|
95
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
96
|
-
.check(function (argv) {
|
|
97
|
-
if (!('x' in argv)) throw 'You forgot about -x';
|
|
98
|
-
if (!('y' in argv)) throw 'You forgot about -y';
|
|
99
|
-
})
|
|
100
|
-
.argv;
|
|
101
|
-
});
|
|
102
|
-
r.should.have.property('result');
|
|
103
|
-
r.result.should.have.property('x', 10);
|
|
104
|
-
r.result.should.have.property('y', 20);
|
|
105
|
-
r.result.should.have.property('_').with.length(0);
|
|
106
|
-
r.should.have.property('errors').with.length(0);
|
|
107
|
-
r.should.have.property('logs').with.length(0);
|
|
108
|
-
r.should.have.property('exit', false);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('should display missing arguments when check fails with a thrown exception', function () {
|
|
112
|
-
var r = checkUsage(function () {
|
|
113
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
114
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
115
|
-
.check(function (argv) {
|
|
116
|
-
if (!('x' in argv)) throw 'You forgot about -x';
|
|
117
|
-
if (!('y' in argv)) throw 'You forgot about -y';
|
|
118
|
-
})
|
|
119
|
-
.argv;
|
|
120
|
-
});
|
|
121
|
-
r.should.have.property('result');
|
|
122
|
-
r.result.should.have.property('x', 10);
|
|
123
|
-
r.result.should.have.property('z', 20);
|
|
124
|
-
r.result.should.have.property('_').with.length(0);
|
|
125
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
126
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
127
|
-
'You forgot about -y'
|
|
128
|
-
]);
|
|
129
|
-
r.should.have.property('logs').with.length(0);
|
|
130
|
-
r.should.have.property('exit').and.be.ok;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should display missing arguments when check fails with a return value', function () {
|
|
134
|
-
var r = checkUsage(function () {
|
|
135
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
136
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
137
|
-
.check(function (argv) {
|
|
138
|
-
if (!('x' in argv)) return 'You forgot about -x';
|
|
139
|
-
if (!('y' in argv)) return 'You forgot about -y';
|
|
140
|
-
})
|
|
141
|
-
.argv;
|
|
142
|
-
});
|
|
143
|
-
r.should.have.property('result');
|
|
144
|
-
r.result.should.have.property('x', 10);
|
|
145
|
-
r.result.should.have.property('z', 20);
|
|
146
|
-
r.result.should.have.property('_').with.length(0);
|
|
147
|
-
r.should.have.property('logs').with.length(0);
|
|
148
|
-
r.should.have.property('exit').and.be.ok;
|
|
149
|
-
r.should.have.property('errors');
|
|
150
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
151
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
152
|
-
'You forgot about -y'
|
|
153
|
-
]);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
exports.checkFailReturn = function () {
|
|
157
|
-
var r = checkUsage(function () {
|
|
158
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
159
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
160
|
-
.check(function (argv) {
|
|
161
|
-
if (!('x' in argv)) return 'You forgot about -x';
|
|
162
|
-
if (!('y' in argv)) return 'You forgot about -y';
|
|
163
|
-
})
|
|
164
|
-
.argv;
|
|
165
|
-
});
|
|
166
|
-
r.should.have.property('result');
|
|
167
|
-
r.result.should.have.property('x', 10);
|
|
168
|
-
r.result.should.have.property('z', 20);
|
|
169
|
-
r.result.should.have.property('_').with.length(0);
|
|
170
|
-
r.should.have.property('logs').with.length(0);
|
|
171
|
-
r.should.have.property('exit').and.be.ok;
|
|
172
|
-
r.should.have.property('errors');
|
|
173
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
174
|
-
'Usage: ./usage -x NUM -y NUM',
|
|
175
|
-
'You forgot about -y'
|
|
176
|
-
]);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
it('should return a valid result when check condition passes', function () {
|
|
180
|
-
function checker (argv) {
|
|
181
|
-
return 'x' in argv && 'y' in argv;
|
|
182
|
-
}
|
|
183
|
-
var r = checkUsage(function () {
|
|
184
|
-
return yargs('-x 10 -y 20'.split(' '))
|
|
185
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
186
|
-
.check(checker)
|
|
187
|
-
.argv;
|
|
188
|
-
});
|
|
189
|
-
r.should.have.property('result');
|
|
190
|
-
r.result.should.have.property('x', 10);
|
|
191
|
-
r.result.should.have.property('y', 20);
|
|
192
|
-
r.result.should.have.property('_').with.length(0);
|
|
193
|
-
r.should.have.property('errors').with.length(0);
|
|
194
|
-
r.should.have.property('logs').with.length(0);
|
|
195
|
-
r.should.have.property('exit', false);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should display a failed message when check condition fails', function () {
|
|
199
|
-
function checker (argv) {
|
|
200
|
-
return 'x' in argv && 'y' in argv;
|
|
201
|
-
}
|
|
202
|
-
var r = checkUsage(function () {
|
|
203
|
-
return yargs('-x 10 -z 20'.split(' '))
|
|
204
|
-
.usage('Usage: $0 -x NUM -y NUM')
|
|
205
|
-
.check(checker)
|
|
206
|
-
.argv;
|
|
207
|
-
});
|
|
208
|
-
r.should.have.property('result');
|
|
209
|
-
r.result.should.have.property('x', 10);
|
|
210
|
-
r.result.should.have.property('z', 20);
|
|
211
|
-
r.result.should.have.property('_').with.length(0);
|
|
212
|
-
r.should.have.property('logs').with.length(0);
|
|
213
|
-
r.should.have.property('exit').and.be.ok;
|
|
214
|
-
r.should.have.property('errors');
|
|
215
|
-
r.errors.join('\n').split(/\n+/).join('\n').should.equal(
|
|
216
|
-
'Usage: ./usage -x NUM -y NUM\n'
|
|
217
|
-
+ 'Argument check failed: ' + checker.toString()
|
|
218
|
-
);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it('should return a valid result when demanding a count of non-hyphenated values', function () {
|
|
222
|
-
var r = checkUsage(function () {
|
|
223
|
-
return yargs('1 2 3 --moo'.split(' '))
|
|
224
|
-
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
|
|
225
|
-
.demand(3)
|
|
226
|
-
.argv;
|
|
227
|
-
});
|
|
228
|
-
r.should.have.property('result');
|
|
229
|
-
r.should.have.property('errors').with.length(0);
|
|
230
|
-
r.should.have.property('logs').with.length(0);
|
|
231
|
-
r.should.have.property('exit', false);
|
|
232
|
-
r.result.should.have.property('_').and.deep.equal([1,2,3]);
|
|
233
|
-
r.result.should.have.property('moo', true);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('should return a failure message when not enough non-hyphenated arguments are found after a demand count', function () {
|
|
237
|
-
var r = checkUsage(function () {
|
|
238
|
-
return yargs('1 2 --moo'.split(' '))
|
|
239
|
-
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
|
|
240
|
-
.demand(3)
|
|
241
|
-
.argv;
|
|
242
|
-
});
|
|
243
|
-
r.should.have.property('result');
|
|
244
|
-
r.should.have.property('logs').with.length(0);
|
|
245
|
-
r.should.have.property('exit').and.be.ok;
|
|
246
|
-
r.result.should.have.property('_').and.deep.equal([1,2]);
|
|
247
|
-
r.result.should.have.property('moo', true);
|
|
248
|
-
r.should.have.property('errors');
|
|
249
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
250
|
-
'Usage: ./usage [x] [y] [z] {OPTIONS}',
|
|
251
|
-
'Not enough non-option arguments: got 2, need at least 3'
|
|
252
|
-
]);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('should return a custom failure message when not enough non-hyphenated arguments are found after a demand count', function () {
|
|
256
|
-
var r = checkUsage(function () {
|
|
257
|
-
return yargs('src --moo'.split(' '))
|
|
258
|
-
.usage('Usage: $0 [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]')
|
|
259
|
-
.demand(2, 'src and dest files are both required')
|
|
260
|
-
.argv;
|
|
261
|
-
});
|
|
262
|
-
r.should.have.property('result');
|
|
263
|
-
r.should.have.property('logs').with.length(0);
|
|
264
|
-
r.should.have.property('exit').and.be.ok;
|
|
265
|
-
r.result.should.have.property('_').and.deep.equal(['src']);
|
|
266
|
-
r.result.should.have.property('moo', true);
|
|
267
|
-
r.should.have.property('errors');
|
|
268
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
269
|
-
'Usage: ./usage [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]',
|
|
270
|
-
'src and dest files are both required'
|
|
271
|
-
]);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('should return a valid result when setting defaults for singles', function () {
|
|
275
|
-
var r = checkUsage(function () {
|
|
276
|
-
return yargs('--foo 50 --baz 70 --powsy'.split(' '))
|
|
277
|
-
.default('foo', 5)
|
|
278
|
-
.default('bar', 6)
|
|
279
|
-
.default('baz', 7)
|
|
280
|
-
.argv
|
|
281
|
-
;
|
|
282
|
-
});
|
|
283
|
-
r.should.have.property('result');
|
|
284
|
-
r.result.should.have.property('foo', 50);
|
|
285
|
-
r.result.should.have.property('bar', 6);
|
|
286
|
-
r.result.should.have.property('baz', 70);
|
|
287
|
-
r.result.should.have.property('powsy', true);
|
|
288
|
-
r.result.should.have.property('_').with.length(0);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
it('should return a valid result when default is set for an alias', function () {
|
|
292
|
-
var r = checkUsage(function () {
|
|
293
|
-
return yargs('')
|
|
294
|
-
.alias('f', 'foo')
|
|
295
|
-
.default('f', 5)
|
|
296
|
-
.argv
|
|
297
|
-
;
|
|
298
|
-
});
|
|
299
|
-
r.should.have.property('result');
|
|
300
|
-
r.result.should.have.property('f', 5);
|
|
301
|
-
r.result.should.have.property('foo', 5);
|
|
302
|
-
r.result.should.have.property('_').with.length(0);
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
it('should print a single line when failing and default is set for an alias', function() {
|
|
306
|
-
var r = checkUsage(function() {
|
|
307
|
-
return yargs('')
|
|
308
|
-
.alias('f', 'foo')
|
|
309
|
-
.default('f', 5)
|
|
310
|
-
.demand(1)
|
|
311
|
-
.argv
|
|
312
|
-
;
|
|
313
|
-
});
|
|
314
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
315
|
-
'Options:',
|
|
316
|
-
' -f, --foo [default: 5]',
|
|
317
|
-
'Not enough non-option arguments: got 0, need at least 1',
|
|
318
|
-
]);
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
it('should allow you to set default values for a hash of options', function () {
|
|
322
|
-
var r = checkUsage(function () {
|
|
323
|
-
return yargs('--foo 50 --baz 70'.split(' '))
|
|
324
|
-
.default({ foo : 10, bar : 20, quux : 30 })
|
|
325
|
-
.argv
|
|
326
|
-
;
|
|
327
|
-
});
|
|
328
|
-
r.should.have.property('result');
|
|
329
|
-
r.result.should.have.property('_').with.length(0);
|
|
330
|
-
r.result.should.have.property('foo', 50);
|
|
331
|
-
r.result.should.have.property('baz', 70);
|
|
332
|
-
r.result.should.have.property('bar', 20);
|
|
333
|
-
r.result.should.have.property('quux', 30);
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
describe('required arguments', function () {
|
|
337
|
-
describe('with options object', function () {
|
|
338
|
-
it('should show a failure message if a required option is missing', function () {
|
|
339
|
-
var r = checkUsage(function () {
|
|
340
|
-
var opts = {
|
|
341
|
-
foo: { description: 'foo option', alias: 'f', requiresArg: true },
|
|
342
|
-
bar: { description: 'bar option', alias: 'b', requiresArg: true }
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
return yargs('-f --bar 20'.split(' '))
|
|
346
|
-
.usage('Usage: $0 [options]', opts)
|
|
347
|
-
.argv;
|
|
348
|
-
});
|
|
349
|
-
r.should.have.property('result');
|
|
350
|
-
r.result.should.have.property('_').with.length(0);
|
|
351
|
-
r.should.have.property('errors');
|
|
352
|
-
r.should.have.property('logs').with.length(0);
|
|
353
|
-
r.should.have.property('exit').and.be.ok;
|
|
354
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
355
|
-
'Usage: ./usage [options]',
|
|
356
|
-
'Options:',
|
|
357
|
-
' --foo, -f foo option',
|
|
358
|
-
' --bar, -b bar option',
|
|
359
|
-
'Missing argument value: foo',
|
|
360
|
-
]);
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
it('should show a failure message if more than one required option is missing', function () {
|
|
364
|
-
var r = checkUsage(function () {
|
|
365
|
-
var opts = {
|
|
366
|
-
foo: { description: 'foo option', alias: 'f', requiresArg: true },
|
|
367
|
-
bar: { description: 'bar option', alias: 'b', requiresArg: true }
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
return yargs('-f --bar'.split(' '))
|
|
371
|
-
.usage('Usage: $0 [options]', opts)
|
|
372
|
-
.argv;
|
|
373
|
-
});
|
|
374
|
-
r.should.have.property('result');
|
|
375
|
-
r.result.should.have.property('_').with.length(0);
|
|
376
|
-
r.should.have.property('errors');
|
|
377
|
-
r.should.have.property('logs').with.length(0);
|
|
378
|
-
r.should.have.property('exit').and.be.ok;
|
|
379
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
380
|
-
'Usage: ./usage [options]',
|
|
381
|
-
'Options:',
|
|
382
|
-
' --foo, -f foo option',
|
|
383
|
-
' --bar, -b bar option',
|
|
384
|
-
'Missing argument values: foo, bar',
|
|
385
|
-
]);
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
describe('with requiresArg method', function () {
|
|
390
|
-
it('should show a failure message if a required option is missing', function () {
|
|
391
|
-
var r = checkUsage(function () {
|
|
392
|
-
var opts = {
|
|
393
|
-
foo: { description: 'foo option', alias: 'f' },
|
|
394
|
-
bar: { description: 'bar option', alias: 'b' }
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
return yargs('-f --bar 20'.split(' '))
|
|
398
|
-
.usage('Usage: $0 [options]', opts)
|
|
399
|
-
.requiresArg(['foo', 'bar'])
|
|
400
|
-
.argv;
|
|
401
|
-
});
|
|
402
|
-
r.should.have.property('result');
|
|
403
|
-
r.result.should.have.property('_').with.length(0);
|
|
404
|
-
r.should.have.property('errors');
|
|
405
|
-
r.should.have.property('logs').with.length(0);
|
|
406
|
-
r.should.have.property('exit').and.be.ok;
|
|
407
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
408
|
-
'Usage: ./usage [options]',
|
|
409
|
-
'Options:',
|
|
410
|
-
' --foo, -f foo option',
|
|
411
|
-
' --bar, -b bar option',
|
|
412
|
-
'Missing argument value: foo',
|
|
413
|
-
]);
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
context("with strict() option set", function () {
|
|
419
|
-
it('should fail given an option argument that is not demanded', function () {
|
|
420
|
-
var r = checkUsage(function () {
|
|
421
|
-
opts = {
|
|
422
|
-
foo: { demand: 'foo option', alias: 'f' },
|
|
423
|
-
bar: { demand: 'bar option', alias: 'b' }
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
return yargs('-f 10 --bar 20 --baz 30'.split(' '))
|
|
427
|
-
.usage('Usage: $0 [options]', opts)
|
|
428
|
-
.strict()
|
|
429
|
-
.argv;
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
r.should.have.property('result');
|
|
433
|
-
r.result.should.have.property('_').with.length(0);
|
|
434
|
-
r.result.should.have.property('f', 10);
|
|
435
|
-
r.result.should.have.property('foo', 10);
|
|
436
|
-
r.result.should.have.property('b', 20);
|
|
437
|
-
r.result.should.have.property('bar', 20);
|
|
438
|
-
r.result.should.have.property('baz', 30);
|
|
439
|
-
r.should.have.property('errors');
|
|
440
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
441
|
-
'Usage: ./usage [options]',
|
|
442
|
-
'Options:',
|
|
443
|
-
' --foo, -f [required]',
|
|
444
|
-
' --bar, -b [required]',
|
|
445
|
-
'Unknown argument: baz',
|
|
446
|
-
]);
|
|
447
|
-
r.should.have.property('logs').with.length(0);
|
|
448
|
-
r.should.have.property('exit').and.be.ok;
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
it('should fail given an option argument without a corresponding description', function () {
|
|
452
|
-
var r = checkUsage(function () {
|
|
453
|
-
opts = {
|
|
454
|
-
foo: { description: 'foo option', alias: 'f' },
|
|
455
|
-
bar: { description: 'bar option', alias: 'b' }
|
|
456
|
-
};
|
|
457
|
-
|
|
458
|
-
return yargs('-f 10 --bar 20 --baz 30'.split(' '))
|
|
459
|
-
.usage('Usage: $0 [options]', opts)
|
|
460
|
-
.strict()
|
|
461
|
-
.argv;
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
r.should.have.property('result');
|
|
465
|
-
r.result.should.have.property('_').with.length(0);
|
|
466
|
-
r.result.should.have.property('f', 10);
|
|
467
|
-
r.result.should.have.property('foo', 10);
|
|
468
|
-
r.result.should.have.property('b', 20);
|
|
469
|
-
r.result.should.have.property('bar', 20);
|
|
470
|
-
r.result.should.have.property('baz', 30);
|
|
471
|
-
r.should.have.property('errors');
|
|
472
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
473
|
-
'Usage: ./usage [options]',
|
|
474
|
-
'Options:',
|
|
475
|
-
' --foo, -f foo option',
|
|
476
|
-
' --bar, -b bar option',
|
|
477
|
-
'Unknown argument: baz',
|
|
478
|
-
]);
|
|
479
|
-
r.should.have.property('logs').with.length(0);
|
|
480
|
-
r.should.have.property('exit').and.be.ok;
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
it('should fail given multiple option arguments without corresponding descriptions', function () {
|
|
484
|
-
var r = checkUsage(function () {
|
|
485
|
-
opts = {
|
|
486
|
-
foo: { description: 'foo option', alias: 'f' },
|
|
487
|
-
bar: { description: 'bar option', alias: 'b' }
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
return yargs('-f 10 --bar 20 --baz 30 -q 40'.split(' '))
|
|
491
|
-
.usage('Usage: $0 [options]', opts)
|
|
492
|
-
.strict()
|
|
493
|
-
.argv;
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
r.should.have.property('result');
|
|
497
|
-
r.result.should.have.property('_').with.length(0);
|
|
498
|
-
r.result.should.have.property('f', 10);
|
|
499
|
-
r.result.should.have.property('foo', 10);
|
|
500
|
-
r.result.should.have.property('b', 20);
|
|
501
|
-
r.result.should.have.property('bar', 20);
|
|
502
|
-
r.result.should.have.property('baz', 30);
|
|
503
|
-
r.result.should.have.property('q', 40);
|
|
504
|
-
r.should.have.property('errors');
|
|
505
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
506
|
-
'Usage: ./usage [options]',
|
|
507
|
-
'Options:',
|
|
508
|
-
' --foo, -f foo option',
|
|
509
|
-
' --bar, -b bar option',
|
|
510
|
-
'Unknown arguments: baz, q',
|
|
511
|
-
]);
|
|
512
|
-
r.should.have.property('logs').with.length(0);
|
|
513
|
-
r.should.have.property('exit').and.be.ok;
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
it('should pass given option arguments with corresponding descriptions', function () {
|
|
517
|
-
var r = checkUsage(function () {
|
|
518
|
-
opts = {
|
|
519
|
-
foo: { description: 'foo option' },
|
|
520
|
-
bar: { description: 'bar option' }
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
return yargs('--foo 10 --bar 20'.split(' '))
|
|
524
|
-
.usage('Usage: $0 [options]', opts)
|
|
525
|
-
.strict()
|
|
526
|
-
.argv;
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
r.should.have.property('result');
|
|
530
|
-
r.result.should.have.property('foo', 10);
|
|
531
|
-
r.result.should.have.property('bar', 20)
|
|
532
|
-
r.result.should.have.property('_').with.length(0);
|
|
533
|
-
r.should.have.property('errors').with.length(0);
|
|
534
|
-
r.should.have.property('logs').with.length(0);
|
|
535
|
-
r.should.have.property('exit', false);
|
|
536
|
-
});
|
|
537
|
-
});
|
|
538
|
-
|
|
539
|
-
it('should display example on fail', function () {
|
|
540
|
-
var r = checkUsage(function () {
|
|
541
|
-
return yargs('')
|
|
542
|
-
.example("$0 something", "description")
|
|
543
|
-
.example("$0 something else", "other description")
|
|
544
|
-
.demand(['y'])
|
|
545
|
-
.argv;
|
|
546
|
-
});
|
|
547
|
-
r.should.have.property('result');
|
|
548
|
-
r.result.should.have.property('_').with.length(0);
|
|
549
|
-
r.should.have.property('errors');
|
|
550
|
-
r.should.have.property('logs').with.length(0);
|
|
551
|
-
r.should.have.property('exit').and.be.ok;
|
|
552
|
-
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
553
|
-
'Examples:',
|
|
554
|
-
' ./usage something description',
|
|
555
|
-
' ./usage something else other description',
|
|
556
|
-
'Options:',
|
|
557
|
-
' -y [required]',
|
|
558
|
-
'Missing required arguments: y'
|
|
559
|
-
]);
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
describe('demand option with boolean flag', function () {
|
|
563
|
-
describe('with demand option', function () {
|
|
564
|
-
it('should report missing required arguments', function () {
|
|
565
|
-
var r = checkUsage(function () {
|
|
566
|
-
return yargs('-y 10 -z 20'.split(' '))
|
|
567
|
-
.usage('Usage: $0 -x NUM [-y NUM]')
|
|
568
|
-
.options({
|
|
569
|
-
'x': { description: 'an option', demand: true },
|
|
570
|
-
'y': { description: 'another option', demand: false }
|
|
571
|
-
})
|
|
572
|
-
.argv;
|
|
573
|
-
});
|
|
574
|
-
r.result.should.have.property('y', 10);
|
|
575
|
-
r.result.should.have.property('z', 20);
|
|
576
|
-
r.result.should.have.property('_').with.length(0);
|
|
577
|
-
r.errors.join('\n').split(/\n/).should.deep.equal([
|
|
578
|
-
'Usage: ./usage -x NUM [-y NUM]',
|
|
579
|
-
'',
|
|
580
|
-
'Options:',
|
|
581
|
-
' -x an option [required]',
|
|
582
|
-
' -y another option',
|
|
583
|
-
'',
|
|
584
|
-
'Missing required arguments: x'
|
|
585
|
-
]);
|
|
586
|
-
r.logs.should.have.length(0);
|
|
587
|
-
r.exit.should.be.ok;
|
|
588
|
-
});
|
|
589
|
-
});
|
|
590
|
-
|
|
591
|
-
describe('with required option', function () {
|
|
592
|
-
it('should report missing required arguments', function () {
|
|
593
|
-
var r = checkUsage(function () {
|
|
594
|
-
return yargs('-y 10 -z 20'.split(' '))
|
|
595
|
-
.usage('Usage: $0 -x NUM [-y NUM]')
|
|
596
|
-
.options({
|
|
597
|
-
'x': { description: 'an option', required: true },
|
|
598
|
-
'y': { description: 'another option', required: false }
|
|
599
|
-
})
|
|
600
|
-
.argv;
|
|
601
|
-
});
|
|
602
|
-
r.result.should.have.property('y', 10);
|
|
603
|
-
r.result.should.have.property('z', 20);
|
|
604
|
-
r.result.should.have.property('_').with.length(0);
|
|
605
|
-
r.errors.join('\n').split(/\n/).should.deep.equal([
|
|
606
|
-
'Usage: ./usage -x NUM [-y NUM]',
|
|
607
|
-
'',
|
|
608
|
-
'Options:',
|
|
609
|
-
' -x an option [required]',
|
|
610
|
-
' -y another option',
|
|
611
|
-
'',
|
|
612
|
-
'Missing required arguments: x'
|
|
613
|
-
]);
|
|
614
|
-
r.logs.should.have.length(0);
|
|
615
|
-
r.exit.should.be.ok;
|
|
616
|
-
});
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
it('should not report missing required arguments when given an alias', function () {
|
|
620
|
-
var r = checkUsage(function () {
|
|
621
|
-
return yargs('-w 10'.split(' '))
|
|
622
|
-
.usage('Usage: $0 --width NUM [--height NUM]')
|
|
623
|
-
.options({
|
|
624
|
-
'width': { description: 'Width', alias: 'w', demand: true },
|
|
625
|
-
'height': { description: 'Height', alias: 'h', demand: false }
|
|
626
|
-
})
|
|
627
|
-
.argv;
|
|
628
|
-
});
|
|
629
|
-
r.result.should.have.property('w', 10);
|
|
630
|
-
r.result.should.have.property('_').with.length(0);
|
|
631
|
-
r.should.have.property('errors').with.length(0);
|
|
632
|
-
r.logs.should.have.length(0);
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
describe('help option', function () {
|
|
637
|
-
it('should display usage', function () {
|
|
638
|
-
var r = checkUsage(function () {
|
|
639
|
-
return yargs(['--help'])
|
|
640
|
-
.demand(['y'])
|
|
641
|
-
.help('help')
|
|
642
|
-
.argv;
|
|
643
|
-
});
|
|
644
|
-
r.should.have.property('result');
|
|
645
|
-
r.result.should.have.property('_').with.length(0);
|
|
646
|
-
r.should.have.property('errors');
|
|
647
|
-
r.should.have.property('logs').with.length(1);
|
|
648
|
-
r.should.have.property('exit').and.be.ok;
|
|
649
|
-
r.logs.join('\n').split(/\n+/).should.deep.equal([
|
|
650
|
-
'Options:',
|
|
651
|
-
' --help Show help',
|
|
652
|
-
' -y [required]',
|
|
653
|
-
''
|
|
654
|
-
]);
|
|
655
|
-
});
|
|
656
|
-
});
|
|
657
|
-
|
|
658
|
-
describe('version option', function () {
|
|
659
|
-
it('should display version', function () {
|
|
660
|
-
var r = checkUsage(function () {
|
|
661
|
-
return yargs(['--version'])
|
|
662
|
-
.version('1.0.1', 'version', 'Show version number')
|
|
663
|
-
.argv;
|
|
664
|
-
});
|
|
665
|
-
r.should.have.property('result');
|
|
666
|
-
r.result.should.have.property('_').with.length(0);
|
|
667
|
-
r.should.have.property('errors');
|
|
668
|
-
r.should.have.property('logs').with.length(1);
|
|
669
|
-
r.should.have.property('exit').and.be.ok;
|
|
670
|
-
r.logs.join('\n').split(/\n+/).should.deep.equal([
|
|
671
|
-
'1.0.1'
|
|
672
|
-
]);
|
|
673
|
-
});
|
|
674
|
-
});
|
|
675
|
-
|
|
676
|
-
describe('showHelpOnFail', function () {
|
|
677
|
-
it('should display user supplied message', function () {
|
|
678
|
-
var opts = {
|
|
679
|
-
foo: { desc: 'foo option', alias: 'f' },
|
|
680
|
-
bar: { desc: 'bar option', alias: 'b' }
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
var r = checkUsage(function () {
|
|
684
|
-
return yargs(['--foo'])
|
|
685
|
-
.usage('Usage: $0 [options]')
|
|
686
|
-
.options(opts)
|
|
687
|
-
.demand(['foo', 'bar'])
|
|
688
|
-
.showHelpOnFail(false, "Specify --help for available options")
|
|
689
|
-
.argv;
|
|
690
|
-
});
|
|
691
|
-
r.should.have.property('result');
|
|
692
|
-
r.result.should.have.property('_').with.length(0);
|
|
693
|
-
r.should.have.property('errors');
|
|
694
|
-
r.should.have.property('logs').with.length(0);
|
|
695
|
-
r.should.have.property('exit').and.be.ok;
|
|
696
|
-
r.errors.join('\n').split(/\n/).should.deep.equal([
|
|
697
|
-
'Missing required arguments: bar',
|
|
698
|
-
'',
|
|
699
|
-
'Specify --help for available options'
|
|
700
|
-
]);
|
|
701
|
-
});
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
it('should succeed when rebase', function () {
|
|
705
|
-
yargs.rebase('/home/chevex', '/home/chevex/foo/bar/baz').should.equal('./foo/bar/baz');
|
|
706
|
-
yargs.rebase('/home/chevex/foo/bar/baz', '/home/chevex').should.equal('../../..');
|
|
707
|
-
yargs.rebase('/home/chevex/foo', '/home/chevex/pow/zoom.txt').should.equal('../pow/zoom.txt');
|
|
708
|
-
});
|
|
709
|
-
|
|
710
|
-
function checkUsage (f) {
|
|
711
|
-
|
|
712
|
-
var exit = false;
|
|
713
|
-
|
|
714
|
-
process._exit = process.exit;
|
|
715
|
-
process._env = process.env;
|
|
716
|
-
process._argv = process.argv;
|
|
717
|
-
|
|
718
|
-
process.exit = function () { exit = true };
|
|
719
|
-
process.env = Hash.merge(process.env, { _ : 'node' });
|
|
720
|
-
process.argv = [ './usage' ];
|
|
721
|
-
|
|
722
|
-
var errors = [];
|
|
723
|
-
var logs = [];
|
|
724
|
-
|
|
725
|
-
console._error = console.error;
|
|
726
|
-
console.error = function (msg) { errors.push(msg) };
|
|
727
|
-
console._log = console.log;
|
|
728
|
-
console.log = function (msg) { logs.push(msg) };
|
|
729
|
-
|
|
730
|
-
var result = f();
|
|
731
|
-
|
|
732
|
-
process.exit = process._exit;
|
|
733
|
-
process.env = process._env;
|
|
734
|
-
process.argv = process._argv;
|
|
735
|
-
|
|
736
|
-
console.error = console._error;
|
|
737
|
-
console.log = console._log;
|
|
738
|
-
|
|
739
|
-
return {
|
|
740
|
-
errors : errors,
|
|
741
|
-
logs : logs,
|
|
742
|
-
exit : exit,
|
|
743
|
-
result : result
|
|
744
|
-
};
|
|
745
|
-
};
|
|
746
|
-
|
|
747
|
-
});
|