Skip to content

Commit cc295c0

Browse files
committed
Added 'require' and 'required' as synonyms for 'demand'
1 parent f84038a commit cc295c0

3 files changed

Lines changed: 162 additions & 78 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,12 @@ Set `argv[key]` to `value` if no option was specified on `process.argv`.
372372

373373
Optionally `.default()` can take an object that maps keys to default values.
374374

375-
.demand(key, [boolean | msg])
376-
------------
375+
.demand(key, [msg | boolean])
376+
-----------------------------
377+
.require(key, [msg | boolean])
378+
------------------------------
379+
.required(key, [msg | boolean])
380+
-------------------------------
377381

378382
If `key` is a string, show the usage information and exit if `key` wasn't
379383
specified in `process.argv`.

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function Argv (processArgs, cwd) {
110110
};
111111

112112
var demanded = {};
113-
self.demand = function (keys, msg) {
113+
self.demand = self.required = self.require = function (keys, msg) {
114114
if (typeof keys == 'number') {
115115
if (!demanded._) demanded._ = { count: 0, msg: null };
116116
demanded._.count += keys;
@@ -223,7 +223,12 @@ function Argv (processArgs, cwd) {
223223
}
224224
else {
225225
if (opt.alias) self.alias(key, opt.alias);
226-
if (opt.demand) self.demand(key, opt.demand);
226+
227+
var demand = opt.demand || opt.required || opt.require;
228+
if (demand) {
229+
self.demand(key, demand);
230+
}
231+
227232
if (typeof opt.default !== 'undefined') {
228233
self.default(key, opt.default);
229234
}

test/usage.js

Lines changed: 149 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,89 @@ var should = require('chai').should(),
44

55
describe('usage', function () {
66

7-
it ('should show an error along with the missing arguments on demand fail', function () {
8-
var r = checkUsage(function () {
9-
return yargs('-x 10 -z 20'.split(' '))
10-
.usage('Usage: $0 -x NUM -y NUM')
11-
.demand(['x','y'])
12-
.argv;
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+
});
1352
});
14-
r.result.should.have.property('x', 10);
15-
r.result.should.have.property('z', 20);
16-
r.result.should.have.property('_').with.length(0);
17-
r.errors.join('\n').split(/\n+/).should.deep.equal([
18-
'Usage: ./usage -x NUM -y NUM',
19-
'Options:',
20-
' -x [required]',
21-
' -y [required]',
22-
'Missing required arguments: y'
23-
]);
24-
r.logs.should.have.length(0);
25-
r.exit.should.be.ok;
26-
});
2753

28-
it('should show an error along with a custom message on demand fail', function () {
29-
var r = checkUsage(function () {
30-
return yargs('-z 20'.split(' '))
31-
.usage('Usage: $0 -x NUM -y NUM')
32-
.demand(['x','y'], 'x and y are both required to multiply all the things')
33-
.argv;
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;
3473
});
35-
r.result.should.have.property('z', 20);
36-
r.result.should.have.property('_').with.length(0);
37-
r.errors.join('\n').split(/\n+/).should.deep.equal([
38-
'Usage: ./usage -x NUM -y NUM',
39-
'Options:',
40-
' -x [required]',
41-
' -y [required]',
42-
'Missing required arguments: x, y',
43-
'x and y are both required to multiply all the things'
44-
]);
45-
r.logs.should.have.length(0);
46-
r.exit.should.be.ok;
47-
});
4874

49-
it('should return valid values when demand passes', function () {
50-
var r = checkUsage(function () {
51-
return yargs('-x 10 -y 20'.split(' '))
52-
.usage('Usage: $0 -x NUM -y NUM')
53-
.demand(['x','y'])
54-
.argv;
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);
5589
});
56-
r.should.have.property('result');
57-
r.result.should.have.property('x', 10);
58-
r.result.should.have.property('y', 20)
59-
r.result.should.have.property('_').with.length(0);
60-
r.should.have.property('errors').with.length(0);
61-
r.should.have.property('logs').with.length(0);
62-
r.should.have.property('exit', false);
6390
});
6491

6592
it('should return valid values when check passes', function () {
@@ -532,30 +559,78 @@ describe('usage', function () {
532559
]);
533560
});
534561

535-
it('should allow demand option with boolean flag', function () {
536-
var r = checkUsage(function () {
537-
return yargs('-y 10 -z 20'.split(' '))
538-
.usage('Usage: $0 -x NUM [-y NUM]')
539-
.options({
540-
'x': { description: 'an option', demand: true },
541-
'y': { description: 'another option', demand: false }
542-
})
543-
.argv;
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);
544633
});
545-
r.result.should.have.property('y', 10);
546-
r.result.should.have.property('z', 20);
547-
r.result.should.have.property('_').with.length(0);
548-
r.errors.join('\n').split(/\n/).should.deep.equal([
549-
'Usage: ./usage -x NUM [-y NUM]',
550-
'',
551-
'Options:',
552-
' -x an option [required]',
553-
' -y another option',
554-
'',
555-
'Missing required arguments: x'
556-
]);
557-
r.logs.should.have.length(0);
558-
r.exit.should.be.ok;
559634
});
560635

561636
describe('help option', function () {

0 commit comments

Comments
 (0)