yargs 1.2.5 → 1.2.6
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 +10 -0
- package/example/requires_arg.js +19 -0
- package/index.js +37 -1
- package/lib/minimist.js +0 -1
- package/package.json +1 -1
- package/test/usage.js +98 -0
package/README.md
CHANGED
|
@@ -386,6 +386,16 @@ If `key` is an Array, demand each element.
|
|
|
386
386
|
If `msg` is supplied, it will be printed when the argument is missing,
|
|
387
387
|
instead of the standard error message. This is especially helpful for the non-option arguments in `argv._`.
|
|
388
388
|
|
|
389
|
+
.requiresArg(key)
|
|
390
|
+
-----------------
|
|
391
|
+
|
|
392
|
+
Specifies either a single option key (string), or an array of options that
|
|
393
|
+
must be followed by option values. If any option value is missing, show the
|
|
394
|
+
usage information and exit.
|
|
395
|
+
|
|
396
|
+
The default behaviour is to set the value of any key not followed by an
|
|
397
|
+
option value to `true`.
|
|
398
|
+
|
|
389
399
|
.describe(key, desc)
|
|
390
400
|
--------------------
|
|
391
401
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var yargs = require('yargs');
|
|
2
|
+
|
|
3
|
+
var argv = yargs.usage('This is my awesome program', {
|
|
4
|
+
'input': {
|
|
5
|
+
description: 'Input file name',
|
|
6
|
+
requiresArg: true,
|
|
7
|
+
short: 'i',
|
|
8
|
+
},
|
|
9
|
+
'output': {
|
|
10
|
+
description: 'Output file name',
|
|
11
|
+
requiresArg: true,
|
|
12
|
+
short: 'o'
|
|
13
|
+
}
|
|
14
|
+
}).argv;
|
|
15
|
+
|
|
16
|
+
yargs.showHelp();
|
|
17
|
+
|
|
18
|
+
console.log('\n\nInspecting options');
|
|
19
|
+
console.dir(argv);
|
package/index.js
CHANGED
|
@@ -45,6 +45,7 @@ function Argv (processArgs, cwd) {
|
|
|
45
45
|
string: [],
|
|
46
46
|
alias: {},
|
|
47
47
|
default: [],
|
|
48
|
+
requiresArg: [],
|
|
48
49
|
count: [],
|
|
49
50
|
normalize: [],
|
|
50
51
|
config: []
|
|
@@ -127,6 +128,11 @@ function Argv (processArgs, cwd) {
|
|
|
127
128
|
return self;
|
|
128
129
|
};
|
|
129
130
|
|
|
131
|
+
self.requiresArg = function (requiresArgs) {
|
|
132
|
+
options.requiresArg.push.apply(options.requiresArg, [].concat(requiresArgs));
|
|
133
|
+
return self;
|
|
134
|
+
};
|
|
135
|
+
|
|
130
136
|
var implied = {};
|
|
131
137
|
self.implies = function (key, value) {
|
|
132
138
|
if (typeof key === 'object') {
|
|
@@ -225,6 +231,10 @@ function Argv (processArgs, cwd) {
|
|
|
225
231
|
if (desc) {
|
|
226
232
|
self.describe(key, desc);
|
|
227
233
|
}
|
|
234
|
+
|
|
235
|
+
if (opt.requiresArg) {
|
|
236
|
+
self.requiresArg(key);
|
|
237
|
+
}
|
|
228
238
|
}
|
|
229
239
|
|
|
230
240
|
return self;
|
|
@@ -287,6 +297,11 @@ function Argv (processArgs, cwd) {
|
|
|
287
297
|
help.unshift(usage.replace(/\$0/g, self.$0), '');
|
|
288
298
|
}
|
|
289
299
|
|
|
300
|
+
keys = keys.filter(function(key) {
|
|
301
|
+
return Object.keys(options.alias).every(function(alias) {
|
|
302
|
+
return -1 == options.alias[alias].indexOf(key);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
290
305
|
var switches = keys.reduce(function (acc, key) {
|
|
291
306
|
acc[key] = [ key ].concat(options.alias[key] || [])
|
|
292
307
|
.map(function (sw) {
|
|
@@ -296,7 +311,7 @@ function Argv (processArgs, cwd) {
|
|
|
296
311
|
;
|
|
297
312
|
return acc;
|
|
298
313
|
}, {});
|
|
299
|
-
|
|
314
|
+
|
|
300
315
|
var switchlen = longest(Object.keys(switches).map(function (s) {
|
|
301
316
|
return switches[s] || '';
|
|
302
317
|
}));
|
|
@@ -391,6 +406,27 @@ function Argv (processArgs, cwd) {
|
|
|
391
406
|
);
|
|
392
407
|
}
|
|
393
408
|
}
|
|
409
|
+
|
|
410
|
+
if (options.requiresArg.length > 0) {
|
|
411
|
+
var missingRequiredArgs = [];
|
|
412
|
+
|
|
413
|
+
options.requiresArg.forEach(function(key) {
|
|
414
|
+
var value = argv[key];
|
|
415
|
+
|
|
416
|
+
// minimist sets --foo value to true / --no-foo to false
|
|
417
|
+
if (value === true || value === false) {
|
|
418
|
+
missingRequiredArgs.push(key);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
if (missingRequiredArgs.length == 1) {
|
|
423
|
+
fail("Missing argument value: " + missingRequiredArgs[0]);
|
|
424
|
+
}
|
|
425
|
+
else if (missingRequiredArgs.length > 1) {
|
|
426
|
+
message = "Missing argument values: " + missingRequiredArgs.join(", ");
|
|
427
|
+
fail(message);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
394
430
|
|
|
395
431
|
var missing = null;
|
|
396
432
|
Object.keys(demanded).forEach(function (key) {
|
package/lib/minimist.js
CHANGED
package/package.json
CHANGED
package/test/usage.js
CHANGED
|
@@ -275,6 +275,22 @@ describe('usage', function () {
|
|
|
275
275
|
r.result.should.have.property('_').with.length(0);
|
|
276
276
|
});
|
|
277
277
|
|
|
278
|
+
it('should print a single line when failing and default is set for an alias', function() {
|
|
279
|
+
var r = checkUsage(function() {
|
|
280
|
+
return yargs('')
|
|
281
|
+
.alias('f', 'foo')
|
|
282
|
+
.default('f', 5)
|
|
283
|
+
.demand(1)
|
|
284
|
+
.argv
|
|
285
|
+
;
|
|
286
|
+
});
|
|
287
|
+
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
288
|
+
'Options:',
|
|
289
|
+
' -f, --foo [default: 5]',
|
|
290
|
+
'Not enough non-option arguments: got 0, need at least 1',
|
|
291
|
+
]);
|
|
292
|
+
});
|
|
293
|
+
|
|
278
294
|
it('should allow you to set default values for a hash of options', function () {
|
|
279
295
|
var r = checkUsage(function () {
|
|
280
296
|
return yargs('--foo 50 --baz 70'.split(' '))
|
|
@@ -290,6 +306,88 @@ describe('usage', function () {
|
|
|
290
306
|
r.result.should.have.property('quux', 30);
|
|
291
307
|
});
|
|
292
308
|
|
|
309
|
+
describe('required arguments', function () {
|
|
310
|
+
describe('with options object', function () {
|
|
311
|
+
it('should show a failure message if a required option is missing', function () {
|
|
312
|
+
var r = checkUsage(function () {
|
|
313
|
+
var opts = {
|
|
314
|
+
foo: { description: 'foo option', alias: 'f', requiresArg: true },
|
|
315
|
+
bar: { description: 'bar option', alias: 'b', requiresArg: true }
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
return yargs('-f --bar 20'.split(' '))
|
|
319
|
+
.usage('Usage: $0 [options]', opts)
|
|
320
|
+
.argv;
|
|
321
|
+
});
|
|
322
|
+
r.should.have.property('result');
|
|
323
|
+
r.result.should.have.property('_').with.length(0);
|
|
324
|
+
r.should.have.property('errors');
|
|
325
|
+
r.should.have.property('logs').with.length(0);
|
|
326
|
+
r.should.have.property('exit').and.be.ok;
|
|
327
|
+
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
328
|
+
'Usage: ./usage [options]',
|
|
329
|
+
'Options:',
|
|
330
|
+
' --foo, -f foo option',
|
|
331
|
+
' --bar, -b bar option',
|
|
332
|
+
'Missing argument value: foo',
|
|
333
|
+
]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('should show a failure message if more than one required option is missing', function () {
|
|
337
|
+
var r = checkUsage(function () {
|
|
338
|
+
var opts = {
|
|
339
|
+
foo: { description: 'foo option', alias: 'f', requiresArg: true },
|
|
340
|
+
bar: { description: 'bar option', alias: 'b', requiresArg: true }
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
return yargs('-f --bar'.split(' '))
|
|
344
|
+
.usage('Usage: $0 [options]', opts)
|
|
345
|
+
.argv;
|
|
346
|
+
});
|
|
347
|
+
r.should.have.property('result');
|
|
348
|
+
r.result.should.have.property('_').with.length(0);
|
|
349
|
+
r.should.have.property('errors');
|
|
350
|
+
r.should.have.property('logs').with.length(0);
|
|
351
|
+
r.should.have.property('exit').and.be.ok;
|
|
352
|
+
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
353
|
+
'Usage: ./usage [options]',
|
|
354
|
+
'Options:',
|
|
355
|
+
' --foo, -f foo option',
|
|
356
|
+
' --bar, -b bar option',
|
|
357
|
+
'Missing argument values: foo, bar',
|
|
358
|
+
]);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('with requiresArg method', function () {
|
|
363
|
+
it('should show a failure message if a required option is missing', function () {
|
|
364
|
+
var r = checkUsage(function () {
|
|
365
|
+
var opts = {
|
|
366
|
+
foo: { description: 'foo option', alias: 'f' },
|
|
367
|
+
bar: { description: 'bar option', alias: 'b' }
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
return yargs('-f --bar 20'.split(' '))
|
|
371
|
+
.usage('Usage: $0 [options]', opts)
|
|
372
|
+
.requiresArg(['foo', 'bar'])
|
|
373
|
+
.argv;
|
|
374
|
+
});
|
|
375
|
+
r.should.have.property('result');
|
|
376
|
+
r.result.should.have.property('_').with.length(0);
|
|
377
|
+
r.should.have.property('errors');
|
|
378
|
+
r.should.have.property('logs').with.length(0);
|
|
379
|
+
r.should.have.property('exit').and.be.ok;
|
|
380
|
+
r.errors.join('\n').split(/\n+/).should.deep.equal([
|
|
381
|
+
'Usage: ./usage [options]',
|
|
382
|
+
'Options:',
|
|
383
|
+
' --foo, -f foo option',
|
|
384
|
+
' --bar, -b bar option',
|
|
385
|
+
'Missing argument value: foo',
|
|
386
|
+
]);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
|
|
293
391
|
context("with strict() option set", function () {
|
|
294
392
|
it('should fail given an option argument that is not demanded', function () {
|
|
295
393
|
var r = checkUsage(function () {
|