Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

optimist

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optimist - npm Package Compare versions

Comparing version 0.3.1 to 0.3.3

.travis.yml

6

index.js

@@ -373,3 +373,4 @@ var path = require('path');

if (args[i+1] && !args[i+1].match(/^-/)
&& !flags.bools[key]) {
&& !flags.bools[key]
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, args[i+1]);

@@ -396,2 +397,5 @@ i++;

argv[key] = defaults[key];
if (key in aliases) {
argv[aliases[key]] = defaults[key];
}
}

@@ -398,0 +402,0 @@ });

12

package.json
{
"name" : "optimist",
"version" : "0.3.1",
"version" : "0.3.3",
"description" : "Light-weight option parsing with an argv hash. No optstrings attached.",

@@ -9,13 +9,13 @@ "main" : "./index.js",

"test" : "test",
"example" : "examples"
"example" : "example"
},
"dependencies" : {
"wordwrap" : ">=0.0.1 <0.1.0"
"wordwrap" : "~0.0.2"
},
"devDependencies" : {
"hashish": "0.0.x",
"expresso" : "0.7.x"
"hashish": "~0.0.4",
"tap" : "~0.2.4"
},
"scripts" : {
"test" : "expresso"
"test" : "tap ./test/*.js"
},

@@ -22,0 +22,0 @@ "repository" : {

var spawn = require('child_process').spawn;
var assert = require('assert');
var test = require('tap').test;
exports.dotSlashEmpty = function () {
testCmd('./bin.js', []);
};
test('dotSlashEmpty', testCmd('./bin.js', []));
exports.dotSlashArgs = function () {
testCmd('./bin.js', [ 'a', 'b', 'c' ]);
};
test('dotSlashArgs', testCmd('./bin.js', [ 'a', 'b', 'c' ]));
exports.nodeEmpty = function () {
testCmd('node bin.js', []);
};
test('nodeEmpty', testCmd('node bin.js', []));
exports.nodeArgs = function () {
testCmd('node bin.js', [ 'x', 'y', 'z' ]);
};
test('nodeArgs', testCmd('node bin.js', [ 'x', 'y', 'z' ]));
exports.whichNodeEmpty = function () {
test('whichNodeEmpty', function (t) {
var which = spawn('which', ['node']);
which.stdout.on('data', function (buf) {
testCmd(buf.toString().trim() + ' bin.js', []);
t.test(
testCmd(buf.toString().trim() + ' bin.js', [])
);
t.end();
});
which.stderr.on('data', function (err) {
assert.fail(err.toString());
assert.error(err);
t.end();
});
};
});
exports.whichNodeArgs = function () {
test('whichNodeArgs', function (t) {
var which = spawn('which', ['node']);
which.stdout.on('data', function (buf) {
testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ]);
t.test(
testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ])
);
t.end();
});
which.stderr.on('data', function (err) {
assert.fail(err.toString());
t.error(err);
t.end();
});
};
});
function testCmd (cmd, args) {
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) {
assert.fail(err.toString());
});
bin.stdout.on('data', function (buf) {
clearTimeout(to);
var _ = JSON.parse(buf.toString());
assert.eql(_.map(String), args.map(String));
});
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();
});
};
}
var optimist = require('../index');
var assert = require('assert');
var path = require('path');
var test = require('tap').test;
var localExpresso = path.normalize(
__dirname + '/../node_modules/.bin/expresso'
);
var expresso = 'node ./parse.js';
var expresso = process.argv[1] === localExpresso
? 'node ./node_modules/.bin/expresso'
: 'expresso'
;
exports['short boolean'] = function () {
test('short boolean', function (t) {
var parse = optimist.parse([ '-b' ]);
assert.eql(parse, { b : true, _ : [], $0 : expresso });
assert.eql(typeof parse.b, 'boolean');
};
t.same(parse, { b : true, _ : [], $0 : expresso });
t.same(typeof parse.b, 'boolean');
t.end();
});
exports['long boolean'] = function () {
assert.eql(
test('long boolean', function (t) {
t.same(
optimist.parse([ '--bool' ]),
{ bool : true, _ : [], $0 : expresso }
);
};
t.end();
});
exports.bare = function () {
assert.eql(
test('bare', function (t) {
t.same(
optimist.parse([ 'foo', 'bar', 'baz' ]),
{ _ : [ 'foo', 'bar', 'baz' ], $0 : expresso }
);
};
t.end();
});
exports['short group'] = function () {
assert.eql(
test('short group', function (t) {
t.same(
optimist.parse([ '-cats' ]),
{ c : true, a : true, t : true, s : true, _ : [], $0 : expresso }
);
};
t.end();
});
exports['short group next'] = function () {
assert.eql(
test('short group next', function (t) {
t.same(
optimist.parse([ '-cats', 'meow' ]),
{ c : true, a : true, t : true, s : 'meow', _ : [], $0 : expresso }
);
};
t.end();
});
exports['short capture'] = function () {
assert.eql(
test('short capture', function (t) {
t.same(
optimist.parse([ '-h', 'localhost' ]),
{ h : 'localhost', _ : [], $0 : expresso }
);
};
t.end();
});
exports['short captures'] = function () {
assert.eql(
test('short captures', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '-p', '555' ]),
{ h : 'localhost', p : 555, _ : [], $0 : expresso }
);
};
t.end();
});
exports['long capture sp'] = function () {
assert.eql(
test('long capture sp', function (t) {
t.same(
optimist.parse([ '--pow', 'xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : expresso }
);
};
t.end();
});
exports['long capture eq'] = function () {
assert.eql(
test('long capture eq', function (t) {
t.same(
optimist.parse([ '--pow=xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : expresso }
);
};
t.end()
});
exports['long captures sp'] = function () {
assert.eql(
test('long captures sp', function (t) {
t.same(
optimist.parse([ '--host', 'localhost', '--port', '555' ]),
{ host : 'localhost', port : 555, _ : [], $0 : expresso }
);
};
t.end();
});
exports['long captures eq'] = function () {
assert.eql(
test('long captures eq', function (t) {
t.same(
optimist.parse([ '--host=localhost', '--port=555' ]),
{ host : 'localhost', port : 555, _ : [], $0 : expresso }
);
};
t.end();
});
exports['mixed short bool and capture'] = function () {
assert.eql(
test('mixed short bool and capture', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),

@@ -98,6 +102,7 @@ {

);
};
t.end();
});
exports['short and long'] = function () {
assert.eql(
test('short and long', function (t) {
t.same(
optimist.parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),

@@ -109,20 +114,23 @@ {

);
};
t.end();
});
exports.no = function () {
assert.eql(
test('no', function (t) {
t.same(
optimist.parse([ '--no-moo' ]),
{ moo : false, _ : [], $0 : expresso }
);
};
t.end();
});
exports.multi = function () {
assert.eql(
test('multi', function (t) {
t.same(
optimist.parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
{ v : ['a','b','c'], _ : [], $0 : expresso }
);
};
t.end();
});
exports.comprehensive = function () {
assert.eql(
test('comprehensive', function (t) {
t.same(
optimist.parse([

@@ -151,5 +159,6 @@ '--name=meowmers', 'bare', '-cats', 'woo',

);
};
t.end();
});
exports.nums = function () {
test('nums', function (t) {
var argv = optimist.parse([

@@ -163,3 +172,3 @@ '-x', '1234',

]);
assert.eql(argv, {
t.same(argv, {
x : 1234,

@@ -173,21 +182,23 @@ y : 5.67,

});
assert.eql(typeof argv.x, 'number');
assert.eql(typeof argv.y, 'number');
assert.eql(typeof argv.z, 'number');
assert.eql(typeof argv.w, 'string');
assert.eql(typeof argv.hex, 'number');
assert.eql(typeof argv._[0], '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();
});
exports['flag boolean'] = function () {
test('flag boolean', function (t) {
var parse = optimist([ '-t', 'moo' ]).boolean(['t']).argv;
assert.eql(parse, { t : true, _ : [ 'moo' ], $0 : expresso });
assert.eql(typeof parse.t, 'boolean');
};
t.same(parse, { t : true, _ : [ 'moo' ], $0 : expresso });
t.same(typeof parse.t, 'boolean');
t.end();
});
exports['flag boolean value'] = function () {
test('flag boolean value', function (t) {
var parse = optimist(['--verbose', 'false', 'moo', '-t', 'true'])
.boolean(['t', 'verbose']).default('verbose', true).argv;
assert.eql(parse, {
t.same(parse, {
verbose: false,

@@ -199,7 +210,8 @@ t: true,

assert.eql(typeof parse.verbose, 'boolean');
assert.eql(typeof parse.t, 'boolean');
};
t.same(typeof parse.verbose, 'boolean');
t.same(typeof parse.t, 'boolean');
t.end();
});
exports['flag boolean default false'] = function () {
test('flag boolean default false', function (t) {
var parse = optimist(['moo'])

@@ -210,3 +222,3 @@ .boolean(['t', 'verbose'])

assert.eql(parse, {
t.same(parse, {
verbose: false,

@@ -218,12 +230,13 @@ t: false,

assert.eql(typeof parse.verbose, 'boolean');
assert.eql(typeof parse.t, 'boolean');
};
t.same(typeof parse.verbose, 'boolean');
t.same(typeof parse.t, 'boolean');
t.end();
exports['boolean groups'] = function () {
});
test('boolean groups', function (t) {
var parse = optimist([ '-x', '-z', 'one', 'two', 'three' ])
.boolean(['x','y','z']).argv;
assert.eql(parse, {
t.same(parse, {
x : true,

@@ -236,38 +249,42 @@ y : false,

assert.eql(typeof parse.x, 'boolean');
assert.eql(typeof parse.y, 'boolean');
assert.eql(typeof parse.z, 'boolean');
};
t.same(typeof parse.x, 'boolean');
t.same(typeof parse.y, 'boolean');
t.same(typeof parse.z, 'boolean');
t.end();
});
exports.strings = function () {
test('strings' , function (t) {
var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
assert.eql(s, '0001234');
assert.eql(typeof s, 'string');
t.same(s, '0001234');
t.same(typeof s, 'string');
var x = optimist([ '-x', '56' ]).string('x').argv.x;
assert.eql(x, '56');
assert.eql(typeof x, 'string');
};
t.same(x, '56');
t.same(typeof x, 'string');
t.end();
});
exports.stringArgs = function () {
test('stringArgs', function (t) {
var s = optimist([ ' ', ' ' ]).string('_').argv._;
assert.eql(s.length, 2);
assert.eql(typeof s[0], 'string');
assert.eql(s[0], ' ');
assert.eql(typeof s[1], 'string');
assert.eql(s[1], ' ');
};
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();
});
exports.slashBreak = function () {
assert.eql(
test('slashBreak', function (t) {
t.same(
optimist.parse([ '-I/foo/bar/baz' ]),
{ I : '/foo/bar/baz', _ : [], $0 : expresso }
);
assert.eql(
t.same(
optimist.parse([ '-xyz/foo/bar/baz' ]),
{ x : true, y : true, z : '/foo/bar/baz', _ : [], $0 : expresso }
);
};
t.end();
});
exports.alias = function () {
test('alias', function (t) {
var argv = optimist([ '-f', '11', '--zoom', '55' ])

@@ -277,8 +294,9 @@ .alias('z', 'zoom')

;
assert.equal(argv.zoom, 55);
assert.equal(argv.z, argv.zoom);
assert.equal(argv.f, 11);
};
t.equal(argv.zoom, 55);
t.equal(argv.z, argv.zoom);
t.equal(argv.f, 11);
t.end();
});
exports.multiAlias = function () {
test('multiAlias', function (t) {
var argv = optimist([ '-f', '11', '--zoom', '55' ])

@@ -288,9 +306,10 @@ .alias('z', [ 'zm', 'zoom' ])

;
assert.equal(argv.zoom, 55);
assert.equal(argv.z, argv.zoom);
assert.equal(argv.z, argv.zm);
assert.equal(argv.f, 11);
};
t.equal(argv.zoom, 55);
t.equal(argv.z, argv.zoom);
t.equal(argv.z, argv.zm);
t.equal(argv.f, 11);
t.end();
});
exports['boolean default true'] = function () {
test('boolean default true', function (t) {
var argv = optimist.options({

@@ -303,6 +322,7 @@ sometrue: {

assert.equal(argv.sometrue, true);
};
t.equal(argv.sometrue, true);
t.end();
});
exports['boolean default false'] = function () {
test('boolean default false', function (t) {
var argv = optimist.options({

@@ -315,6 +335,7 @@ somefalse: {

assert.equal(argv.somefalse, false);
};
t.equal(argv.somefalse, false);
t.end();
});
exports['nested dotted objects'] = function () {
test('nested dotted objects', function (t) {
var argv = optimist([

@@ -326,3 +347,3 @@ '--foo.bar', '3', '--foo.baz', '4',

assert.deepEqual(argv.foo, {
t.same(argv.foo, {
bar : 3,

@@ -335,3 +356,53 @@ baz : 4,

});
assert.deepEqual(argv.beep, { boop : true });
};
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': expresso,
};
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': expresso,
};
t.same(aliasedArgv, expected);
t.same(propertyArgv, expected);
t.end();
});
var Hash = require('hashish');
var optimist = require('../index');
var assert = require('assert');
var test = require('tap').test;
exports.usageFail = function () {
test('usageFail', function (t) {
var r = checkUsage(function () {

@@ -12,8 +12,8 @@ return optimist('-x 10 -z 20'.split(' '))

});
assert.deepEqual(
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
assert.deepEqual(
t.same(
r.errors.join('\n').split(/\n+/),

@@ -28,7 +28,9 @@ [

);
assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
exports.usagePass = function () {
test('usagePass', function (t) {
var r = checkUsage(function () {

@@ -40,3 +42,3 @@ return optimist('-x 10 -y 20'.split(' '))

});
assert.deepEqual(r, {
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },

@@ -47,5 +49,6 @@ errors : [],

});
};
t.end();
});
exports.checkPass = function () {
test('checkPass', function (t) {
var r = checkUsage(function () {

@@ -60,3 +63,3 @@ return optimist('-x 10 -y 20'.split(' '))

});
assert.deepEqual(r, {
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },

@@ -67,5 +70,6 @@ errors : [],

});
};
t.end();
});
exports.checkFail = function () {
test('checkFail', function (t) {
var r = checkUsage(function () {

@@ -80,9 +84,9 @@ return optimist('-x 10 -z 20'.split(' '))

});
assert.deepEqual(
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
assert.deepEqual(
t.same(
r.errors.join('\n').split(/\n+/),

@@ -94,12 +98,13 @@ [

);
assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};
exports.checkCondPass = function () {
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('checkCondPass', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {

@@ -111,3 +116,3 @@ return optimist('-x 10 -y 20'.split(' '))

});
assert.deepEqual(r, {
t.same(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },

@@ -118,9 +123,10 @@ errors : [],

});
};
t.end();
});
exports.checkCondFail = function () {
test('checkCondFail', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
var r = checkUsage(function () {

@@ -132,9 +138,9 @@ return optimist('-x 10 -z 20'.split(' '))

});
assert.deepEqual(
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
assert.deepEqual(
t.same(
r.errors.join('\n').split(/\n+/).join('\n'),

@@ -144,8 +150,9 @@ 'Usage: ./usage -x NUM -y NUM\n'

);
assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};
exports.countPass = function () {
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('countPass', function (t) {
var r = checkUsage(function () {

@@ -157,3 +164,3 @@ return optimist('1 2 3 --moo'.split(' '))

});
assert.deepEqual(r, {
t.same(r, {
result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' },

@@ -164,5 +171,6 @@ errors : [],

});
};
t.end();
});
exports.countFail = function () {
test('countFail', function (t) {
var r = checkUsage(function () {

@@ -174,8 +182,8 @@ return optimist('1 2 --moo'.split(' '))

});
assert.deepEqual(
t.same(
r.result,
{ _ : [ '1', '2' ], moo : true, $0 : './usage' }
);
assert.deepEqual(
t.same(
r.errors.join('\n').split(/\n+/),

@@ -187,8 +195,9 @@ [

);
assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};
exports.defaultSingles = function () {
t.same(r.logs, []);
t.ok(r.exit);
t.end();
});
test('defaultSingles', function (t) {
var r = checkUsage(function () {

@@ -202,3 +211,3 @@ return optimist('--foo 50 --baz 70 --powsy'.split(' '))

});
assert.eql(r.result, {
t.same(r.result, {
foo : '50',

@@ -211,6 +220,24 @@ bar : 6,

});
};
t.end();
});
exports.defaultHash = function () {
test('defaultAliases', function (t) {
var r = checkUsage(function () {
return optimist('')
.alias('f', 'foo')
.default('f', 5)
.argv
;
});
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(' '))

@@ -221,38 +248,44 @@ .default({ foo : 10, bar : 20, quux : 30 })

});
assert.eql(r.result, {
foo : '50',
t.same(r.result, {
_ : [],
$0 : './usage',
foo : 50,
baz : 70,
bar : 20,
baz : 70,
quux : 30,
_ : [],
$0 : './usage',
});
};
t.end();
});
exports.rebase = function () {
assert.equal(
test('rebase', function (t) {
t.equal(
optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),
'./foo/bar/baz'
);
assert.equal(
t.equal(
optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),
'../../..'
);
assert.equal(
t.equal(
optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),
'../pow/zoom.txt'
);
};
t.end();
});
function checkUsage (f) {
var _process = process;
process = Hash.copy(process);
var exit = false;
process.exit = function () { exit = true };
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' ];
var errors = [];
var logs = [];
console._error = console.error;

@@ -262,9 +295,12 @@ console.error = function (msg) { errors.push(msg) };

console.log = function (msg) { logs.push(msg) };
var result = f();
process = _process;
process.exit = process._exit;
process.env = process._env;
process.argv = process._argv;
console.error = console._error;
console.log = console._log;
return {

@@ -271,0 +307,0 @@ errors : errors,

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