/
test.variadic.args.js
64 lines (51 loc) · 1.41 KB
/
test.variadic.args.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Module dependencies.
*/
var program = require('../')
, should = require('should')
, util = require('util')
, programArgs = ['node', 'test', 'mycommand', 'arg0', 'arg1', 'arg2', 'arg3']
, requiredArg
, variadicArg;
program
.version('0.0.1')
.command('mycommand <id> [variadicArg...]')
.action(function (arg0, arg1) {
requiredArg = arg0;
variadicArg = arg1;
});
program.parse(programArgs);
requiredArg.should.eql('arg0');
variadicArg.should.eql(['arg1', 'arg2', 'arg3']);
program.args.should.have.lengthOf(3);
program.args[0].should.eql('arg0');
program.args[1].should.eql(['arg1', 'arg2', 'arg3']);
program
.version('0.0.1')
.command('mycommand <variadicArg...> [optionalArg]')
.action(function (arg0, arg1) {
});
// Make sure we still catch errors with required values for options
var consoleErrors = [];
var oldProcessExit = process.exit;
var oldConsoleError = console.error;
var errorMessage;
process.exit = function () {
throw new Error(consoleErrors.join('\n'));
};
console.error = function () {
consoleErrors.push(util.format.apply(util, arguments));
};
try {
program.parse(programArgs);
should.fail(null, null, 'An Error should had been thrown above');
} catch (err) {
errorMessage = err.message;
}
process.exit = oldProcessExit;
console.error = oldConsoleError;
[
'',
' error: variadic arguments must be last `variadicArg\'',
''
].join('\n').should.eql(errorMessage);