test.variadic.args.js
This file
Path
test/test.variadic.args.js
64 lines · 1.4 KiB · JavaScript
Last changed by
Merge pull request #313 from zhiyelee/codestyleZhiye Libefore adoption543e47f
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465/** * 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 optionsvar 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);