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.2.3 to 1.2.4

example/strict.js

36

index.js

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

};
var strict = false;
self.strict = function () {
strict = true;
return self;
};

@@ -410,3 +416,31 @@ self.showHelp = function (fn) {

}
if (strict) {
var unknown = [];
var aliases = {};
Object.keys(options.alias).forEach(function (key) {
options.alias[key].forEach(function (alias) {
aliases[alias] = key;
});
});
Object.keys(argv).forEach(function (key) {
if (key !== "$0" && key !== "_" &&
!descriptions.hasOwnProperty(key) &&
!demanded.hasOwnProperty(key) &&
!aliases.hasOwnProperty(key)) {
unknown.push(key);
}
});
if (unknown.length == 1) {
fail("Unknown argument: " + unknown[0]);
}
else if (unknown.length > 1) {
fail("Unknown arguments: " + unknown.join(", "));
}
}
checks.forEach(function (f) {

@@ -413,0 +447,0 @@ try {

4

package.json
{
"name": "yargs",
"version": "1.2.3",
"version": "1.2.4",
"description": "Light-weight option parsing with an argv hash. No optstrings attached.",
"main": "./index.js",
"dependencies": {
"minimist": "~0.0.1"
"minimist": "^0.1.0"
},

@@ -9,0 +9,0 @@ "devDependencies": {

@@ -12,4 +12,6 @@ yargs

> NOTE: Yargs is a fork of [optimist](https://github.com/substack/node-optimist) by [substack (James Halliday)](https://github.com/substack). It is obvious that substack is stretched pretty thin maintaining over 300 modules on npm at the time of this writing. So rather than complain in the project issue tracker I thought I'd just pick up the torch and maintain a proper fork. Currently the project is totally backward compatible with optimist but this may change in the future (if it does I will update this notice to inform you of this). For now though, enjoy optimist with about 5 months worth of fixes and updates rolled in, most of them pulled from optimist's own [stale pull requests](https://github.com/substack/node-optimist/pulls).
> ~~NOTE: Yargs is a fork of [optimist](https://github.com/substack/node-optimist) by [substack (James Halliday)](https://github.com/substack). It is obvious that substack is stretched pretty thin maintaining over 300 modules on npm at the time of this writing. So rather than complain in the project issue tracker I thought I'd just pick up the torch and maintain a proper fork. Currently the project is totally backward compatible with optimist but this may change in the future (if it does I will update this notice to inform you of this). For now though, enjoy optimist with about 5 months worth of fixes and updates rolled in, most of them pulled from optimist's own [stale pull requests](https://github.com/substack/node-optimist/pulls).~~
> UPDATE: Yargs is now the official successor to optimist. Please feel free to submit issues and pull requests. While I personally don't have the time to pour over all the issues and fix all of them on a regular basis, I'm more than happy to look over pull requests, test them, and merge them in. If you'd like to contribute and don't know where to start, have a look at [the issue list](https://github.com/chevex/yargs/issues) :)
examples

@@ -482,2 +484,8 @@ ========

.strict()
---------
Any command-line argument given that is not demanded, or does not have a
corresponding description, will be reported as an error.
.help()

@@ -484,0 +492,0 @@ -------

@@ -293,2 +293,123 @@ var should = require('chai').should(),

context("with strict() option set", function () {
it('should fail given an option argument that is not demanded', function () {
var r = checkUsage(function () {
opts = {
foo: { demand: 'foo option', alias: 'f' },
bar: { demand: 'bar option', alias: 'b' }
};
return yargs('-f 10 --bar 20 --baz 30'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f [required]',
' --bar, -b [required]',
'Unknown argument: baz',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});
it('should fail given an option argument without a corresponding description', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option', alias: 'f' },
bar: { description: 'bar option', alias: 'b' }
};
return yargs('-f 10 --bar 20 --baz 30'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Unknown argument: baz',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});
it('should fail given multiple option arguments without corresponding descriptions', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option', alias: 'f' },
bar: { description: 'bar option', alias: 'b' }
};
return yargs('-f 10 --bar 20 --baz 30 -q 40'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.result.should.have.property('q', 40);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Unknown arguments: baz, q',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});
it('should pass given option arguments with corresponding descriptions', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option' },
bar: { description: 'bar option' }
};
return yargs('--foo 10 --bar 20'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});
r.should.have.property('result');
r.result.should.have.property('foo', 10);
r.result.should.have.property('bar', 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);
});
});
it('should display example on fail', function () {

@@ -295,0 +416,0 @@ var r = checkUsage(function () {

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