-
Notifications
You must be signed in to change notification settings - Fork 2
/
coerce.js
38 lines (36 loc) · 1.31 KB
/
coerce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var path = require('path');
var expect = require('chai').expect;
var pkg = path.normalize(path.join(__dirname, '..', '..', 'package.json'));
function range(val) {
return val.split('..').map(Number);
}
function list(val) {
return val.split(',');
}
describe('cli-command:', function() {
it('should coerce argument values', function(done) {
var cli = require('../..')(pkg);
var args = ['assert', '-i', '10', '-f', '3.14',
'--range', '1..10', '--list=apples,oranges', 'file.txt', '-v=value'];
cli
.configure({usage: '[options] <file ...>'})
.option('-i, --integer <n>', 'an integer argument', parseInt)
.option('-f, --float <n>', 'a float argument', parseFloat)
.option('-r, --range <a>..<b>', 'a range', range)
.option('-l, --list <items>', 'a list', list)
.option('-v, --value [value]', 'an optional value')
.command('assert')
.description('assert on arguments')
.action(function(cmd, req, next) {
expect(cli.integer).to.eql(10);
expect(cli.float).to.eql(3.14);
expect(cli.range).to.eql([1,10]);
expect(cli.list).to.eql(['apples','oranges']);
expect(req.args).to.eql(['file.txt']);
expect(cli.value).to.eql('value');
done();
})
//console.dir(cli);
cli.parse(args);
});
})