Socket
Socket
Sign inDemoInstall

yargs

Package Overview
Dependencies
Maintainers
1
Versions
250
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yargs - npm Package Compare versions

Comparing version 1.0.15 to 1.1.0

test/mocha.opts

2

lib/minimist.js

@@ -252,2 +252,2 @@ var path = require('path');

return Math.max.apply(null, xs.map(function (x) { return x.length }));
}
}
{
"name": "yargs",
"version": "1.0.15",
"version": "1.1.0",
"description": "Light-weight option parsing with an argv hash. No optstrings attached.",

@@ -10,7 +10,8 @@ "main": "./index.js",

"devDependencies": {
"hashish": "~0.0.4",
"tap": "~0.4.0"
"hashish": "*",
"mocha": "*",
"chai": "*"
},
"scripts": {
"test": "tap ./test/*.js"
"test": "mocha -R nyan"
},

@@ -31,3 +32,3 @@ "repository": {

"author": {
"name": "Alex Ford (forked from James Halliday)",
"name": "Alex Ford",
"email": "Alex.Ford@CodeTunnel.com",

@@ -34,0 +35,0 @@ "url": "http://CodeTunnel.com"

@@ -1,71 +0,64 @@

var spawn = require('child_process').spawn;
var test = require('tap').test;
var spawn = require('child_process').spawn,
should = require('chai').should();
test('dotSlashEmpty', testCmd('./bin.js', []));
describe('bin script', function () {
test('dotSlashArgs', testCmd('./bin.js', [ 'a', 'b', 'c' ]));
it('should run as a shell script with no arguments', function (done) {
testCmd('./bin.js', [], done);
});
test('nodeEmpty', testCmd('node bin.js', []));
it('should run as a shell script with arguments', function (done) {
testCmd('./bin.js', [ 'a', 'b', 'c' ], done);
});
test('nodeArgs', testCmd('node bin.js', [ 'x', 'y', 'z' ]));
it('should run as a node script with no arguments', function (done) {
testCmd('node bin.js', [], done);
});
test('whichNodeEmpty', function (t) {
var which = spawn('which', ['node']);
which.stdout.on('data', function (buf) {
t.test(
testCmd(buf.toString().trim() + ' bin.js', [])
);
t.end();
it('should run as a node script with arguments', function (done) {
testCmd('node bin.js', [ 'x', 'y', 'z' ], done);
});
which.stderr.on('data', function (err) {
assert.error(err);
t.end();
describe('path returned by "which"', function () {
beforeEach(function () {
this.which = spawn('which', ['node']);
});
it('should match the actual path to the script file', function (done) {
this.which.stdout.on('data', function (buf) {
testCmd(buf.toString().trim() + ' bin.js', [], done);
});
this.which.stderr.on('data', done);
});
it('should match the actual path to the script file, with arguments', function (done) {
this.which.stdout.on('data', function (buf) {
testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ], done);
});
this.which.stderr.on('data', done);
});
});
});
test('whichNodeArgs', function (t) {
var which = spawn('which', ['node']);
function testCmd(cmd, args, done) {
which.stdout.on('data', function (buf) {
t.test(
testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ])
);
t.end();
});
var oldDir = process.cwd();
process.chdir(__dirname + '/_');
which.stderr.on('data', function (err) {
t.error(err);
t.end();
var cmds = cmd.split(' ');
var bin = spawn(cmds[0], cmds.slice(1).concat(args.map(String)));
process.chdir(oldDir);
bin.stderr.on('data', done);
bin.stdout.on('data', function (buf) {
var _ = JSON.parse(buf.toString());
_.map(String).should.deep.equal(args.map(String));
done();
});
});
function testCmd (cmd, args) {
return function (t) {
var to = setTimeout(function () {
assert.fail('Never got stdout data.')
}, 5000);
var oldDir = process.cwd();
process.chdir(__dirname + '/_');
var cmds = cmd.split(' ');
var bin = spawn(cmds[0], cmds.slice(1).concat(args.map(String)));
process.chdir(oldDir);
bin.stderr.on('data', function (err) {
t.error(err);
t.end();
});
bin.stdout.on('data', function (buf) {
clearTimeout(to);
var _ = JSON.parse(buf.toString());
t.same(_.map(String), args.map(String));
t.end();
});
};
}

@@ -1,25 +0,28 @@

var optimist = require('../index');
var path = require('path');
var test = require('tap').test;
var should = require('chai').should()
yargs = require('../index');
var $0 = 'node ./' + path.relative(process.cwd(), __filename);
describe('count', function () {
test('count', function(t) {
var parsed;
it('should count the number of times a boolean is present', function () {
var parsed;
parsed = optimist(['-x']).count('verbose').argv;
console.error(parsed);
t.same(parsed.verbose, 0);
parsed = yargs(['-x']).count('verbose').argv;
parsed.verbose.should.equal(0);
parsed = optimist(['--verbose']).count('verbose').argv;
t.same(parsed.verbose, 1);
parsed = yargs(['--verbose']).count('verbose').argv;
parsed.verbose.should.equal(1);
parsed = optimist(['--verbose', '--verbose']).count('verbose').argv;
t.same(parsed.verbose, 2);
parsed = yargs(['--verbose', '--verbose']).count('verbose').argv;
parsed.verbose.should.equal(2);
// w/alias
parsed = optimist(['--verbose', '--verbose', '-v', '--verbose']).count('verbose').alias('v', 'verbose').argv;
t.same(parsed.verbose, 4);
parsed = yargs(['-vvv']).alias('v', 'verbose').count('verbose').argv;
parsed.verbose.should.equal(3);
t.end();
});
parsed = yargs(['--verbose', '--verbose', '-v', '--verbose']).count('verbose').alias('v', 'verbose').argv;
parsed.verbose.should.equal(4);
parsed = yargs(['--verbose', '--verbose', '-v', '-vv']).count('verbose').alias('v', 'verbose').argv;
parsed.verbose.should.equal(5);
});
});

@@ -1,31 +0,35 @@

var optimist = require('../index');
var test = require('tap').test;
var should = require('chai').should(),
yargsLib = require('../index');
test('-', function (t) {
t.plan(5);
t.deepEqual(
fix(optimist.parse([ '-n', '-' ])),
{ n: '-', _: [] }
);
t.deepEqual(
fix(optimist.parse([ '-' ])),
{ _: [ '-' ] }
);
t.deepEqual(
fix(optimist.parse([ '-f-' ])),
{ f: '-', _: [] }
);
t.deepEqual(
fix(optimist([ '-b', '-' ]).boolean('b').argv),
{ b: true, _: [ '-' ] }
);
t.deepEqual(
fix(optimist([ '-s', '-' ]).string('s').argv),
{ s: '-', _: [] }
);
describe('-', function () {
it('should set - as value of n', function () {
var argv = yargs.parse(['-n', '-']);
argv.should.have.property('n', '-');
argv.should.have.property('_').with.length(0);
});
it('should set - as a non-hyphenated value', function () {
var argv = yargs.parse(['-']);
argv.should.have.property('_').and.deep.equal(['-']);
});
it('should set - as a value of f', function () {
var argv = yargs.parse(['-f-']);
argv.should.have.property('f', '-');
argv.should.have.property('_').with.length(0);
});
it('should set b to true and set - as a non-hyphenated value when b is set as a boolean', function () {
var argv = yargs(['-b', '-']).boolean('b').argv;
argv.should.have.property('b', true);
argv.should.have.property('_').and.deep.equal(['-']);
});
it('should set - as the value of s when s is set as a string', function () {
var argv = yargs([ '-s', '-' ]).string('s').argv;
argv.should.have.property('s', '-');
argv.should.have.property('_').with.length(0);
});
});
function fix (obj) {
delete obj.$0;
return obj;
}

@@ -1,14 +0,21 @@

var optimist = require('../');
var test = require('tap').test;
var should = require('chai').should(),
yargs = require('../');
test('parse with modifier functions' , function (t) {
t.plan(1);
var argv = optimist().boolean('b').parse([ '-b', '123' ]);
t.deepEqual(fix(argv), { b: true, _: ['123'] });
describe('parse', function () {
describe('boolean modifier function', function () {
it('should prevent yargs from sucking in the next option as the value of the first option', function () {
// Arrange & Act
var result = yargs().boolean('b').parse([ '-b', '123' ]);
// Assert
result.should.have.property('b').that.is.a('boolean').and.is.true;
result.should.have.property('_').and.deep.equal([123]);
});
});
});
function fix (obj) {
delete obj.$0;
return obj;
}

@@ -1,132 +0,114 @@

var optimist = require('../index');
var path = require('path');
var test = require('tap').test;
var should = require('chai').should(),
yargs = require('../'),
path = require('path');
var $0 = 'node ./' + path.relative(process.cwd(), __filename);
describe('parse', function () {
test('short boolean', function (t) {
var parse = optimist.parse([ '-b' ]);
t.same(parse, { b : true, _ : [], $0 : $0 });
t.same(typeof parse.b, 'boolean');
t.end();
});
beforeEach(function (done) {
setTimeout(done, 75);
});
test('long boolean', function (t) {
t.same(
optimist.parse([ '--bool' ]),
{ bool : true, _ : [], $0 : $0 }
);
t.end();
});
test('bare', function (t) {
t.same(
optimist.parse([ 'foo', 'bar', 'baz' ]),
{ _ : [ 'foo', 'bar', 'baz' ], $0 : $0 }
);
t.end();
});
it('should pass when specifying a "short boolean"', function () {
var parse = yargs.parse([ '-b' ]);
parse.should.have.property('b').to.be.ok.and.be.a('boolean');
parse.should.have.property('_').with.length(0);
});
test('short group', function (t) {
t.same(
optimist.parse([ '-cats' ]),
{ c : true, a : true, t : true, s : true, _ : [], $0 : $0 }
);
t.end();
});
it('should pass when specifying a "long boolean"', function () {
var parse = yargs.parse(['--bool']);
parse.should.have.property('bool', true);
parse.should.have.property('_').with.length(0);
});
it('should place bare options in the _ array', function () {
var parse = yargs.parse(['foo', 'bar', 'baz']);
parse.should.have.property('_').and.deep.equal(['foo','bar','baz']);
});
test('short group next', function (t) {
t.same(
optimist.parse([ '-cats', 'meow' ]),
{ c : true, a : true, t : true, s : 'meow', _ : [], $0 : $0 }
);
t.end();
});
test('short capture', function (t) {
t.same(
optimist.parse([ '-h', 'localhost' ]),
{ h : 'localhost', _ : [], $0 : $0 }
);
t.end();
});
it('should expand grouped short options to a hash with a key for each', function () {
var parse = yargs.parse(['-cats']);
parse.should.have.property('c', true);
parse.should.have.property('a', true);
parse.should.have.property('t', true);
parse.should.have.property('s', true);
parse.should.have.property('_').with.length(0);
});
test('short captures', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '-p', '555' ]),
{ h : 'localhost', p : 555, _ : [], $0 : $0 }
);
t.end();
});
it('should set the value of the final option in a group to the next supplied value', function () {
var parse = yargs.parse(['-cats', 'meow']);
parse.should.have.property('c', true);
parse.should.have.property('a', true);
parse.should.have.property('t', true);
parse.should.have.property('s', 'meow');
parse.should.have.property('_').with.length(0);
});
it('should set the value of a single short option to the next supplied value', function () {
var parse = yargs.parse(['-h', 'localhost']);
parse.should.have.property('h', 'localhost');
parse.should.have.property('_').with.length(0);
});
test('long capture sp', function (t) {
t.same(
optimist.parse([ '--pow', 'xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : $0 }
);
t.end();
});
it('should set the value of multiple single short options to the next supplied values relative to each', function () {
var parse = yargs.parse(['-h', 'localhost', '-p', '555']);
parse.should.have.property('h', 'localhost');
parse.should.have.property('p', 555);
parse.should.have.property('_').with.length(0);
});
test('long capture eq', function (t) {
t.same(
optimist.parse([ '--pow=xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : $0 }
);
t.end()
});
it('should set the value of a single long option to the next supplied value', function () {
var parse = yargs.parse(['--pow', 'xixxle']);
parse.should.have.property('pow', 'xixxle');
parse.should.have.property('_').with.length(0);
});
test('long captures sp', function (t) {
t.same(
optimist.parse([ '--host', 'localhost', '--port', '555' ]),
{ host : 'localhost', port : 555, _ : [], $0 : $0 }
);
t.end();
});
it('should set the value of a single long option if an = was used', function () {
var parse = yargs.parse(['--pow=xixxle']);
parse.should.have.property('pow', 'xixxle');
parse.should.have.property('_').with.length(0);
});
test('long captures eq', function (t) {
t.same(
optimist.parse([ '--host=localhost', '--port=555' ]),
{ host : 'localhost', port : 555, _ : [], $0 : $0 }
);
t.end();
});
it('should set the value of multiple long options to the next supplied values relative to each', function () {
var parse = yargs.parse(['--host', 'localhost', '--port', '555']);
parse.should.have.property('host', 'localhost');
parse.should.have.property('port', 555);
parse.should.have.property('_').with.length(0);
});
test('mixed short bool and capture', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
{
f : true, p : 555, h : 'localhost',
_ : [ 'script.js' ], $0 : $0,
}
);
t.end();
});
test('short and long', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '--port', '555' ]),
{ h : 'localhost', port : 555, _ : [], $0 : $0 }
);
t.end();
});
it('should set the value of multiple long options if = signs were used', function () {
var parse = yargs.parse(['--host=localhost', '--port=555']);
parse.should.have.property('host', 'localhost');
parse.should.have.property('port', 555);
parse.should.have.property('_').with.length(0);
});
test('no', function (t) {
t.same(
optimist.parse([ '--no-moo' ]),
{ moo : false, _ : [], $0 : $0 }
);
t.end();
});
test('multi', function (t) {
t.same(
optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
{ v : ['a','b','c'], _ : [], $0 : $0 }
);
t.end();
});
test('comprehensive', function (t) {
t.same(
optimist.parse([
it('should still set values appropriately if a mix of short, long, and grouped short options are specified', function () {
var parse = yargs.parse(['-h', 'localhost', '-fp', '555', 'script.js']);
parse.should.have.property('f', true);
parse.should.have.property('p', 555);
parse.should.have.property('h', 'localhost');
parse.should.have.property('_').and.deep.equal(['script.js']);
});
it('should still set values appropriately if a mix of short and long options are specified', function () {
var parse = yargs.parse(['-h', 'localhost', '--port', '555']);
parse.should.have.property('h', 'localhost');
parse.should.have.property('port', 555);
parse.should.have.property('_').with.length(0);
});
it('should explicitly set a boolean option to false if preceeded by "--no-"', function () {
var parse = yargs.parse(['--no-moo']);
parse.should.have.property('moo', false);
parse.should.have.property('_').with.length(0);
});
it('should group values into an array if the same option is specified multiple times', function () {
var parse = yargs.parse(['-v', 'a', '-v', 'b', '-v', 'c' ]);
parse.should.have.property('v').and.deep.equal(['a','b','c']);
parse.should.have.property('_').with.length(0);
});
it('should still set values appropriately if we supply a comprehensive list of various types of options', function () {
var parse = yargs.parse([
'--name=meowmers', 'bare', '-cats', 'woo',

@@ -137,336 +119,242 @@ '-h', 'awesome', '--multi=quux',

'--', '--not-a-flag', 'eek'
]),
{
c : true,
a : true,
t : true,
s : 'woo',
h : 'awesome',
b : true,
bool : true,
key : 'value',
multi : [ 'quux', 'baz' ],
meep : false,
name : 'meowmers',
_ : [ 'bare', '--not-a-flag', 'eek' ],
$0 : $0
}
);
t.end();
});
]);
parse.should.have.property('c', true);
parse.should.have.property('a', true);
parse.should.have.property('t', true);
parse.should.have.property('s', 'woo');
parse.should.have.property('h', 'awesome');
parse.should.have.property('b', true);
parse.should.have.property('bool', true);
parse.should.have.property('key', 'value');
parse.should.have.property('multi').and.deep.equal(['quux', 'baz']);
parse.should.have.property('meep', false);
parse.should.have.property('name', 'meowmers');
parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', 'eek']);
});
test('nums', function (t) {
var argv = optimist.parse([
'-x', '1234',
'-y', '5.67',
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'789',
]);
t.same(argv, {
x : 1234,
y : 5.67,
z : 1e7,
w : '10f',
hex : 0xdeadbeef,
_ : [ 789 ],
$0 : $0
it('should parse numbers appropriately', function () {
var argv = yargs.parse([
'-x', '1234',
'-y', '5.67',
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'789',
]);
argv.should.have.property('x', 1234).and.be.a('number');
argv.should.have.property('y', 5.67).and.be.a('number');
argv.should.have.property('z', 1e7).and.be.a('number');
argv.should.have.property('w', '10f').and.be.a('string');
argv.should.have.property('hex', 0xdeadbeef).and.be.a('number');
argv.should.have.property('_').and.deep.equal([789]);
argv._[0].should.be.a('number');
});
t.same(typeof argv.x, 'number');
t.same(typeof argv.y, 'number');
t.same(typeof argv.z, 'number');
t.same(typeof argv.w, 'string');
t.same(typeof argv.hex, 'number');
t.same(typeof argv._[0], 'number');
t.end();
});
test('flag boolean', function (t) {
var parse = optimist([ '-t', 'moo' ]).boolean(['t']).argv;
t.same(parse, { t : true, _ : [ 'moo' ], $0 : $0 });
t.same(typeof parse.t, 'boolean');
t.end();
});
it('should not set the next value as the value of a short option if that option is explicitly defined as a boolean', function () {
var parse = yargs([ '-t', 'moo' ]).boolean(['t']).argv;
parse.should.have.property('t', true).and.be.a('boolean');
parse.should.have.property('_').and.deep.equal(['moo']);
});
test('flag boolean value', function (t) {
var parse = optimist(['--verbose', 'false', 'moo', '-t', 'true'])
.boolean(['t', 'verbose']).default('verbose', true).argv;
t.same(parse, {
verbose: false,
t: true,
_: ['moo'],
$0 : $0
it('should set boolean options values if the next value is "true" or "false"', function () {
var parse = yargs(['--verbose', 'false', 'moo', '-t', 'true'])
.boolean(['t', 'verbose']).default('verbose', true).argv;
parse.should.have.property('verbose', false).and.be.a('boolean');
parse.should.have.property('t', true).and.be.a('boolean');
parse.should.have.property('_').and.deep.equal(['moo']);
});
t.same(typeof parse.verbose, 'boolean');
t.same(typeof parse.t, 'boolean');
t.end();
});
test('flag boolean default false', function (t) {
var parse = optimist(['moo'])
.boolean(['t', 'verbose'])
.default('verbose', false)
.default('t', false).argv;
t.same(parse, {
verbose: false,
t: false,
_: ['moo'],
$0 : $0
it('should set boolean options to false by default', function () {
var parse = yargs(['moo'])
.boolean(['t', 'verbose'])
.default('verbose', false)
.default('t', false).argv;
parse.should.have.property('verbose', false).and.be.a('boolean');
parse.should.have.property('t', false).and.be.a('boolean');
parse.should.have.property('_').and.deep.equal(['moo']);
});
t.same(typeof parse.verbose, 'boolean');
t.same(typeof parse.t, 'boolean');
t.end();
});
it('should allow defining options as boolean in groups', function () {
var parse = yargs([ '-x', '-z', 'one', 'two', 'three' ])
.boolean(['x','y','z']).argv;
parse.should.have.property('x', true).and.be.a('boolean');
parse.should.have.property('y', false).and.be.a('boolean');
parse.should.have.property('z', true).and.be.a('boolean');
parse.should.have.property('_').and.deep.equal(['one','two','three']);
});
test('boolean groups', function (t) {
var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
.boolean(['x','y','z']).argv;
t.same(parse, {
x : true,
y : false,
z : true,
_ : [ 'one', 'two', 'three' ],
$0 : $0
it('should preserve newlines in option values' , function () {
var args = yargs.parse(['-s', "X\nX"]);
args.should.have.property('_').with.length(0);
args.should.have.property('s', 'X\nX');
// reproduce in bash:
// VALUE="new
// line"
// node program.js --s="$VALUE"
args = yargs.parse(["--s=X\nX"]);
args.should.have.property('_').with.length(0);
args.should.have.property('s', 'X\nX');
});
t.same(typeof parse.x, 'boolean');
t.same(typeof parse.y, 'boolean');
t.same(typeof parse.z, 'boolean');
t.end();
});
test('newlines in params' , function (t) {
var args = optimist.parse([ '-s', "X\nX" ])
t.same(args, { _ : [], s : "X\nX", $0 : $0 });
it('should not convert numbers to type number if explicitly defined as strings' , function () {
var s = yargs([ '-s', '0001234' ]).string('s').argv.s;
s.should.be.a('string').and.equal('0001234');
var x = yargs([ '-x', '56' ]).string('x').argv.x;
x.should.be.a('string').and.equal('56');
});
// reproduce in bash:
// VALUE="new
// line"
// node program.js --s="$VALUE"
args = optimist.parse([ "--s=X\nX" ])
t.same(args, { _ : [], s : "X\nX", $0 : $0 });
t.end();
});
it('should leave all non-hyphenated values as strings if _ is defined as a string', function () {
var s = yargs([ ' ', ' ' ]).string('_').argv._;
s.should.have.length(2);
s[0].should.be.a('string').and.equal(' ');
s[1].should.be.a('string').and.equal(' ');
});
test('strings' , function (t) {
var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
t.same(s, '0001234');
t.same(typeof s, 'string');
var x = optimist([ '-x', '56' ]).string('x').argv.x;
t.same(x, '56');
t.same(typeof x, 'string');
t.end();
});
it('should normalize redundant paths', function () {
var a = yargs([ '-s', '/tmp/../' ]).alias('s', 'save').normalize('s').argv;
a.should.have.property('s', '/');
a.should.have.property('save', '/');
});
test('stringArgs', function (t) {
var s = optimist([ ' ', ' ' ]).string('_').argv._;
t.same(s.length, 2);
t.same(typeof s[0], 'string');
t.same(s[0], ' ');
t.same(typeof s[1], 'string');
t.same(s[1], ' ');
t.end();
});
it('should normalize redundant paths when a value is later assigned', function () {
var a = yargs(['-s']).normalize('s').argv;
a.should.have.property('s', true);
a.s = '/path/to/new/dir/../../';
a.s.should.equal('/path/to/');
});
test('normalize', function (t) {
var a = optimist([ '-s', '/tmp/../' ]).alias('s', 'save').normalize('s').argv;
t.same(a.s, '/');
t.same(a.save, '/');
t.end();
});
it('should assign data after forward slash to the option before the slash', function () {
var parse = yargs.parse(['-I/foo/bar/baz']);
parse.should.have.property('_').with.length(0);
parse.should.have.property('I', '/foo/bar/baz');
parse = yargs.parse(['-xyz/foo/bar/baz']);
parse.should.have.property('x', true);
parse.should.have.property('y', true);
parse.should.have.property('z', '/foo/bar/baz');
parse.should.have.property('_').with.length(0);
});
test('normalize setter', function (t) {
var a = optimist(['-s']).normalize('s').argv;
t.same(a.s, true);
a.s = '/path/to/new/dir/../../';
t.same(a.s, '/path/to/');
t.end();
});
it('should set alias value to the same value as the full option', function () {
var argv = yargs([ '-f', '11', '--zoom', '55' ])
.alias('z', 'zoom')
.argv;
argv.should.have.property('zoom', 55);
argv.should.have.property('z', 55);
argv.should.have.property('f', 11);
});
test('slashBreak', function (t) {
t.same(
optimist.parse([ '-I/foo/bar/baz' ]),
{ I : '/foo/bar/baz', _ : [], $0 : $0 }
);
t.same(
optimist.parse([ '-xyz/foo/bar/baz' ]),
{ x : true, y : true, z : '/foo/bar/baz', _ : [], $0 : $0 }
);
t.end();
});
/*
*it('should load options and values from a file when config is used', function () {
* var argv = yargs([ '--settings', '../test/config.json', '--foo', 'bar' ])
* .alias('z', 'zoom')
* .config('settings')
* .argv;
* argv.should.have.property('herp', 'derp');
* argv.should.have.property('zoom', 55);
* argv.should.have.property('foo').and.deep.equal(['baz','bar']);
*});
*/
test('alias', function (t) {
var argv = optimist([ '-f', '11', '--zoom', '55' ])
.alias('z', 'zoom')
.argv
;
t.equal(argv.zoom, 55);
t.equal(argv.z, argv.zoom);
t.equal(argv.f, 11);
t.end();
});
it('should allow multiple aliases to be specified', function () {
var argv = yargs([ '-f', '11', '--zoom', '55' ])
.alias('z', [ 'zm', 'zoom' ])
.argv;
argv.should.have.property('zoom', 55);
argv.should.have.property('z', 55);
argv.should.have.property('zm', 55);
argv.should.have.property('f', 11);
});
test('config', function (t) {
var argv = optimist([ '--settings', 'config.json', '--foo', 'bar' ])
.alias('z', 'zoom')
.config('settings')
.argv;
it('should define option as boolean and set default to true', function () {
var argv = yargs.options({
sometrue: {
boolean: true,
default: true
}
}).argv;
argv.should.have.property('sometrue', true);
});
t.equal(argv.herp, 'derp');
t.equal(argv.zoom, 55);
t.equal(argv.foo[0], 'baz');
t.equal(argv.foo[1], 'bar');
t.end();
});
it('should define option as boolean and set default to false', function () {
var argv = yargs.options({
somefalse: {
boolean: true,
default: false
}
}).argv;
argv.should.have.property('somefalse', false);
});
test('multiAlias', function (t) {
var argv = optimist([ '-f', '11', '--zoom', '55' ])
.alias('z', [ 'zm', 'zoom' ])
.argv
;
t.equal(argv.zoom, 55);
t.equal(argv.z, argv.zoom);
t.equal(argv.z, argv.zm);
t.equal(argv.f, 11);
t.end();
});
it('should allow object graph traversal via dot notation', function () {
var argv = yargs([
'--foo.bar', '3', '--foo.baz', '4',
'--foo.quux.quibble', '5', '--foo.quux.o_O',
'--beep.boop'
]).argv;
argv.should.have.property('foo').and.deep.equal({
bar: 3,
baz: 4,
quux: {
quibble: 5,
o_O: true
}
});
argv.should.have.property('beep').and.deep.equal({ boop: true });
});
test('boolean default true', function (t) {
var argv = optimist.options({
sometrue: {
boolean: true,
default: true
}
}).argv;
t.equal(argv.sometrue, true);
t.end();
});
it('should allow booleans and aliases to be defined with chainable api', function () {
var aliased = [ '-h', 'derp' ],
regular = [ '--herp', 'derp' ],
opts = {
herp: { alias: 'h', boolean: true }
},
aliasedArgv = yargs(aliased).boolean('herp').alias('h', 'herp').argv,
propertyArgv = yargs(regular).boolean('herp').alias('h', 'herp').argv;
aliasedArgv.should.have.property('herp', true);
aliasedArgv.should.have.property('h', true);
aliasedArgv.should.have.property('_').and.deep.equal(['derp']);
propertyArgv.should.have.property('herp', true);
propertyArgv.should.have.property('h', true);
propertyArgv.should.have.property('_').and.deep.equal(['derp']);
});
test('boolean default false', function (t) {
var argv = optimist.options({
somefalse: {
boolean: true,
default: false
}
}).argv;
it('should allow booleans and aliases to be defined with options hash', function () {
var aliased = [ '-h', 'derp' ],
regular = [ '--herp', 'derp' ],
opts = {
herp: { alias: 'h', boolean: true }
},
aliasedArgv = yargs(aliased).options(opts).argv,
propertyArgv = yargs(regular).options(opts).argv;
aliasedArgv.should.have.property('herp', true);
aliasedArgv.should.have.property('h', true);
aliasedArgv.should.have.property('_').and.deep.equal(['derp']);
propertyArgv.should.have.property('herp', true);
propertyArgv.should.have.property('h', true);
propertyArgv.should.have.property('_').and.deep.equal(['derp']);
});
t.equal(argv.somefalse, false);
t.end();
});
it('should set boolean and alias using explicit true', function () {
var aliased = [ '-h', 'true' ],
regular = [ '--herp', 'true' ],
opts = {
herp: { alias: 'h', boolean: true }
},
aliasedArgv = yargs(aliased).boolean('h').alias('h', 'herp').argv,
propertyArgv = yargs(regular).boolean('h').alias('h', 'herp').argv;
aliasedArgv.should.have.property('herp', true);
aliasedArgv.should.have.property('h', true);
aliasedArgv.should.have.property('_').with.length(0);
});
test('nested dotted objects', function (t) {
var argv = optimist([
'--foo.bar', '3', '--foo.baz', '4',
'--foo.quux.quibble', '5', '--foo.quux.o_O',
'--beep.boop'
]).argv;
t.same(argv.foo, {
bar : 3,
baz : 4,
quux : {
quibble : 5,
o_O : true
},
// regression, see https://github.com/substack/node-optimist/issues/71
it('should set boolean and --x=true', function() {
var parsed = yargs(['--boool', '--other=true']).boolean('boool').argv;
parsed.should.have.property('boool', true);
parsed.should.have.property('other', 'true');
parsed = yargs(['--boool', '--other=false']).boolean('boool').argv;
parsed.should.have.property('boool', true);
parsed.should.have.property('other', 'false');
});
t.same(argv.beep, { boop : true });
t.end();
});
test('boolean and alias with chainable api', function (t) {
var aliased = [ '-h', 'derp' ];
var regular = [ '--herp', 'derp' ];
var opts = {
herp: { alias: 'h', boolean: true }
};
var aliasedArgv = optimist(aliased)
.boolean('herp')
.alias('h', 'herp')
.argv;
var propertyArgv = optimist(regular)
.boolean('herp')
.alias('h', 'herp')
.argv;
var expected = {
herp: true,
h: true,
'_': [ 'derp' ],
'$0': $0,
};
t.same(aliasedArgv, expected);
t.same(propertyArgv, expected);
t.end();
});
test('boolean and alias with options hash', function (t) {
var aliased = [ '-h', 'derp' ];
var regular = [ '--herp', 'derp' ];
var opts = {
herp: { alias: 'h', boolean: true }
};
var aliasedArgv = optimist(aliased)
.options(opts)
.argv;
var propertyArgv = optimist(regular).options(opts).argv;
var expected = {
herp: true,
h: true,
'_': [ 'derp' ],
'$0': $0,
};
t.same(aliasedArgv, expected);
t.same(propertyArgv, expected);
t.end();
});
test('boolean and alias using explicit true', function (t) {
var aliased = [ '-h', 'true' ];
var regular = [ '--herp', 'true' ];
var opts = {
herp: { alias: 'h', boolean: true }
};
var aliasedArgv = optimist(aliased)
.boolean('h')
.alias('h', 'herp')
.argv;
var propertyArgv = optimist(regular)
.boolean('h')
.alias('h', 'herp')
.argv;
var expected = {
herp: true,
h: true,
'_': [ ],
'$0': $0,
};
t.same(aliasedArgv, expected);
t.same(propertyArgv, expected);
t.end();
});
// regression, see https://github.com/substack/node-optimist/issues/71
test('boolean and --x=true', function(t) {
var parsed = optimist(['--boool', '--other=true']).boolean('boool').argv;
t.same(parsed.boool, true);
t.same(parsed.other, 'true');
parsed = optimist(['--boool', '--other=false']).boolean('boool').argv;
t.same(parsed.boool, true);
t.same(parsed.other, 'false');
t.end();
});

@@ -1,16 +0,20 @@

var optimist = require('../index');
var test = require('tap').test;
var should = require('chai').should(),
yargs = require('../');
test('-n123', function (t) {
t.plan(1);
var parse = optimist.parse([ '-n123' ]);
t.equal(parse.n, 123);
});
describe('short options', function () {
test('-123', function (t) {
t.plan(3);
var parse = optimist.parse([ '-123', '456' ]);
t.equal(parse['1'], true);
t.equal(parse['2'], true);
t.equal(parse['3'], 456);
it ('should set n to the numeric value 123', function () {
var argv = yargs.parse([ '-n123' ]);
should.exist(argv);
argv.should.have.property('n', 123);
});
it ('should set option "1" to true, option "2" to true, and option "3" to numeric value 456', function () {
var argv = yargs.parse([ '-123', '456' ]);
should.exist(argv);
argv.should.have.property('1', true);
argv.should.have.property('2', true);
argv.should.have.property('3', 456);
});
});

@@ -1,20 +0,18 @@

var Hash = require('hashish');
var optimist = require('../index');
var test = require('tap').test;
var should = require('chai').should(),
Hash = require('hashish'),
yargs = require('../');
test('usageFail', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'])
.argv;
});
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
describe('usage', function () {
t.same(
r.errors.join('\n').split(/\n+/),
[
it ('should show an error along with the missing arguments on demand fail', function () {
var r = checkUsage(function () {
return yargs('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'])
.argv;
});
r.result.should.have.property('x', 10);
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage -x NUM -y NUM',

@@ -25,24 +23,17 @@ 'Options:',

'Missing required arguments: y'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('usageFailWithMessage', function (t) {
var r = checkUsage(function () {
return optimist('-z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'], 'x and y are both required to multiply all the things')
.argv;
]);
r.logs.should.have.length(0);
r.exit.should.be.ok;
});
t.same(
r.result,
{ z: 20, _: [], $0: './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/),
[
it('should show an error along with a custom message on demand fail', function () {
var r = checkUsage(function () {
return yargs('-z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'], 'x and y are both required to multiply all the things')
.argv;
});
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage -x NUM -y NUM',

@@ -54,383 +45,317 @@ 'Options:',

'x and y are both required to multiply all the things'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('usagePass', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'])
.argv;
]);
r.logs.should.have.length(0);
r.exit.should.be.ok;
});
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },
errors : [],
logs : [],
exit : false
});
t.end();
});
test('checkPass', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) throw 'You forgot about -x';
if (!('y' in argv)) throw 'You forgot about -y';
})
.argv;
it('should return valid values when demand passes', function () {
var r = checkUsage(function () {
return yargs('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.demand(['x','y'])
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('y', 20)
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
});
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },
errors : [],
logs : [],
exit : false
});
t.end();
});
test('checkFail', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) throw 'You forgot about -x';
if (!('y' in argv)) throw 'You forgot about -y';
})
.argv;
it('should return valid values when check passes', function () {
var r = checkUsage(function () {
return yargs('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) throw 'You forgot about -x';
if (!('y' in argv)) throw 'You forgot about -y';
})
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('y', 20);
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
});
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/),
[
it('should display missing arguments when check fails with a thrown exception', function () {
var r = checkUsage(function () {
return yargs('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) throw 'You forgot about -x';
if (!('y' in argv)) throw 'You forgot about -y';
})
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage -x NUM -y NUM',
'You forgot about -y'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('checkFailReturn', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/),
[
it('should display missing arguments when check fails with a return value', function () {
var r = checkUsage(function () {
return yargs('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage -x NUM -y NUM',
'You forgot about -y'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
exports.checkFailReturn = function () {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
]);
});
assert.deepEqual(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
assert.deepEqual(
r.errors.join('\n').split(/\n+/),
[
exports.checkFailReturn = function () {
var r = checkUsage(function () {
return yargs('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage -x NUM -y NUM',
'You forgot about -y'
]
);
]);
};
assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};
test('checkCondPass', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
it('should return a valid result when check condition passes', function () {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {
return yargs('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('y', 20);
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
});
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },
errors : [],
logs : [],
exit : false
});
t.end();
});
test('checkCondFail', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
it('should display a failed message when check condition fails', function () {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {
return yargs('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
});
r.should.have.property('result');
r.result.should.have.property('x', 10);
r.result.should.have.property('z', 20);
r.result.should.have.property('_').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).join('\n').should.equal(
'Usage: ./usage -x NUM -y NUM\n'
+ 'Argument check failed: ' + checker.toString()
);
});
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/).join('\n'),
'Usage: ./usage -x NUM -y NUM\n'
+ 'Argument check failed: ' + checker.toString()
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('countPass', function (t) {
var r = checkUsage(function () {
return optimist('1 2 3 --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
.demand(3)
.argv;
it('should return a valid result when demanding a count of non-hyphenated values', function () {
var r = checkUsage(function () {
return yargs('1 2 3 --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
.demand(3)
.argv;
});
r.should.have.property('result');
r.should.have.property('errors').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
r.result.should.have.property('_').and.deep.equal([1,2,3]);
r.result.should.have.property('moo', true);
});
t.same(r, {
result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' },
errors : [],
logs : [],
exit : false
});
t.end();
});
test('countFail', function (t) {
var r = checkUsage(function () {
return optimist('1 2 --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
.demand(3)
.argv;
});
t.same(
r.result,
{ _ : [ '1', '2' ], moo : true, $0 : './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/),
[
it('should return a failure message when not enough non-hyphenated arguments are found after a demand count', function () {
var r = checkUsage(function () {
return yargs('1 2 --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
.demand(3)
.argv;
});
r.should.have.property('result');
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.result.should.have.property('_').and.deep.equal([1,2]);
r.result.should.have.property('moo', true);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [x] [y] [z] {OPTIONS}',
'Not enough non-option arguments: got 2, need at least 3'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('countFailWithMessage', function (t) {
var r = checkUsage(function () {
return optimist('src --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]')
.demand(2, 'src and dest files are both required')
.argv;
]);
});
t.same(
r.result,
{ _: ['src'], moo: true, $0: './usage' }
);
t.same(
r.errors.join('\n').split(/\n+/),
[
it('should return a custom failure message when not enough non-hyphenated arguments are found after a demand count', function () {
var r = checkUsage(function () {
return yargs('src --moo'.split(' '))
.usage('Usage: $0 [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]')
.demand(2, 'src and dest files are both required')
.argv;
});
r.should.have.property('result');
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.result.should.have.property('_').and.deep.equal(['src']);
r.result.should.have.property('moo', true);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]',
'src and dest files are both required'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('defaultSingles', function (t) {
var r = checkUsage(function () {
return optimist('--foo 50 --baz 70 --powsy'.split(' '))
.default('foo', 5)
.default('bar', 6)
.default('baz', 7)
.argv
;
]);
});
t.same(r.result, {
foo : '50',
bar : 6,
baz : '70',
powsy : true,
_ : [],
$0 : './usage'
});
t.end();
});
test('defaultAliases', function (t) {
var r = checkUsage(function () {
return optimist('')
.alias('f', 'foo')
.default('f', 5)
.argv
;
it('should return a valid result when setting defaults for singles', function () {
var r = checkUsage(function () {
return yargs('--foo 50 --baz 70 --powsy'.split(' '))
.default('foo', 5)
.default('bar', 6)
.default('baz', 7)
.argv
;
});
r.should.have.property('result');
r.result.should.have.property('foo', 50);
r.result.should.have.property('bar', 6);
r.result.should.have.property('baz', 70);
r.result.should.have.property('powsy', true);
r.result.should.have.property('_').with.length(0);
});
t.same(r.result, {
f : '5',
foo : '5',
_ : [],
$0 : './usage'
});
t.end();
});
test('defaultHash', function (t) {
var r = checkUsage(function () {
return optimist('--foo 50 --baz 70'.split(' '))
.default({ foo : 10, bar : 20, quux : 30 })
.argv
;
it('should return a valid result when default is set for an alias', function () {
var r = checkUsage(function () {
return yargs('')
.alias('f', 'foo')
.default('f', 5)
.argv
;
});
r.should.have.property('result');
r.result.should.have.property('f', 5);
r.result.should.have.property('foo', 5);
r.result.should.have.property('_').with.length(0);
});
t.same(r.result, {
_ : [],
$0 : './usage',
foo : 50,
baz : 70,
bar : 20,
quux : 30
});
t.end();
});
test('exampleFail', function (t) {
var r = checkUsage(function () {
return optimist('')
.example("$0 something", "description")
.example("$0 something else", "other description")
.demand(['y'])
.argv;
it('should allow you to set default values for a hash of options', function () {
var r = checkUsage(function () {
return yargs('--foo 50 --baz 70'.split(' '))
.default({ foo : 10, bar : 20, quux : 30 })
.argv
;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('foo', 50);
r.result.should.have.property('baz', 70);
r.result.should.have.property('bar', 20);
r.result.should.have.property('quux', 30);
});
t.same(
r.result,
{ _ : [], $0 : './usage' }
);
t.same(
r.errors.join('\n').split(/\n/),
[
it('should display example on fail', function () {
var r = checkUsage(function () {
return yargs('')
.example("$0 something", "description")
.example("$0 something else", "other description")
.demand(['y'])
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors');
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Examples:',
' ./usage something description',
' ./usage something else other description',
'',
'',
'Options:',
' -y [required]',
'',
'Missing required arguments: y'
]
);
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
]);
});
test('rebase', function (t) {
t.equal(
optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),
'./foo/bar/baz'
);
t.equal(
optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),
'../../..'
);
t.equal(
optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),
'../pow/zoom.txt'
);
t.end();
});
it('should succeed when rebase', function () {
yargs.rebase('/home/chevex', '/home/chevex/foo/bar/baz').should.equal('./foo/bar/baz');
yargs.rebase('/home/chevex/foo/bar/baz', '/home/chevex').should.equal('../../..');
yargs.rebase('/home/chevex/foo', '/home/chevex/pow/zoom.txt').should.equal('../pow/zoom.txt');
});
function checkUsage (f) {
function checkUsage (f) {
var exit = false;
var exit = false;
process._exit = process.exit;
process._env = process.env;
process._argv = process.argv;
process._exit = process.exit;
process._env = process.env;
process._argv = process.argv;
process.exit = function (t) { exit = true };
process.env = Hash.merge(process.env, { _ : 'node' });
process.argv = [ './usage' ];
process.exit = function () { exit = true };
process.env = Hash.merge(process.env, { _ : 'node' });
process.argv = [ './usage' ];
var errors = [];
var logs = [];
var errors = [];
var logs = [];
console._error = console.error;
console.error = function (msg) { errors.push(msg) };
console._log = console.log;
console.log = function (msg) { logs.push(msg) };
console._error = console.error;
console.error = function (msg) { errors.push(msg) };
console._log = console.log;
console.log = function (msg) { logs.push(msg) };
var result = f();
var result = f();
process.exit = process._exit;
process.env = process._env;
process.argv = process._argv;
process.exit = process._exit;
process.env = process._env;
process.argv = process._argv;
console.error = console._error;
console.log = console._log;
console.error = console._error;
console.log = console._log;
return {
errors : errors,
logs : logs,
exit : exit,
result : result
return {
errors : errors,
logs : logs,
exit : exit,
result : result
};
};
};
});

@@ -9,1 +9,15 @@ var optimist = require('../');

});
var should = require('chai').should(),
yargs = require('../');
describe('whitespace', function () {
it('should be whitespace', function () {
var argv = yargs.parse([ '-x', '\t' ]);
should.exist(argv);
argv.should.have.property('x', '\t');
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc