Socket
Socket
Sign inDemoInstall

yargs

Package Overview
Dependencies
1
Maintainers
1
Versions
250
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.5 to 1.2.6

example/requires_arg.js

38

index.js

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

default: [],
requiresArg: [],
count: [],

@@ -131,2 +132,7 @@ normalize: [],

self.requiresArg = function (requiresArgs) {
options.requiresArg.push.apply(options.requiresArg, [].concat(requiresArgs));
return self;
};
var implied = {};

@@ -230,2 +236,6 @@ self.implies = function (key, value) {

}
if (opt.requiresArg) {
self.requiresArg(key);
}
}

@@ -293,2 +303,7 @@

keys = keys.filter(function(key) {
return Object.keys(options.alias).every(function(alias) {
return -1 == options.alias[alias].indexOf(key);
});
});
var switches = keys.reduce(function (acc, key) {

@@ -303,3 +318,3 @@ acc[key] = [ key ].concat(options.alias[key] || [])

}, {});
var switchlen = longest(Object.keys(switches).map(function (s) {

@@ -399,2 +414,23 @@ return switches[s] || '';

}
if (options.requiresArg.length > 0) {
var missingRequiredArgs = [];
options.requiresArg.forEach(function(key) {
var value = argv[key];
// minimist sets --foo value to true / --no-foo to false
if (value === true || value === false) {
missingRequiredArgs.push(key);
}
});
if (missingRequiredArgs.length == 1) {
fail("Missing argument value: " + missingRequiredArgs[0]);
}
else if (missingRequiredArgs.length > 1) {
message = "Missing argument values: " + missingRequiredArgs.join(", ");
fail(message);
}
}

@@ -401,0 +437,0 @@ var missing = null;

1

lib/minimist.js

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

var config = JSON.parse(fs.readFileSync(val, 'utf8'));
console.warn(config);
Object.keys(config).forEach(function (key) {

@@ -91,0 +90,0 @@ setArg(key, config[key]);

{
"name": "yargs",
"version": "1.2.5",
"version": "1.2.6",
"description": "Light-weight option parsing with an argv hash. No optstrings attached.",

@@ -5,0 +5,0 @@ "main": "./index.js",

@@ -389,2 +389,12 @@ yargs

.requiresArg(key)
-----------------
Specifies either a single option key (string), or an array of options that
must be followed by option values. If any option value is missing, show the
usage information and exit.
The default behaviour is to set the value of any key not followed by an
option value to `true`.
.describe(key, desc)

@@ -391,0 +401,0 @@ --------------------

@@ -278,2 +278,18 @@ var should = require('chai').should(),

it('should print a single line when failing and default is set for an alias', function() {
var r = checkUsage(function() {
return yargs('')
.alias('f', 'foo')
.default('f', 5)
.demand(1)
.argv
;
});
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' -f, --foo [default: 5]',
'Not enough non-option arguments: got 0, need at least 1',
]);
});
it('should allow you to set default values for a hash of options', function () {

@@ -294,2 +310,84 @@ var r = checkUsage(function () {

describe('required arguments', function () {
describe('with options object', function () {
it('should show a failure message if a required option is missing', function () {
var r = checkUsage(function () {
var opts = {
foo: { description: 'foo option', alias: 'f', requiresArg: true },
bar: { description: 'bar option', alias: 'b', requiresArg: true }
};
return yargs('-f --bar 20'.split(' '))
.usage('Usage: $0 [options]', opts)
.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([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Missing argument value: foo',
]);
});
it('should show a failure message if more than one required option is missing', function () {
var r = checkUsage(function () {
var opts = {
foo: { description: 'foo option', alias: 'f', requiresArg: true },
bar: { description: 'bar option', alias: 'b', requiresArg: true }
};
return yargs('-f --bar'.split(' '))
.usage('Usage: $0 [options]', opts)
.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([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Missing argument values: foo, bar',
]);
});
});
describe('with requiresArg method', function () {
it('should show a failure message if a required option is missing', function () {
var r = checkUsage(function () {
var opts = {
foo: { description: 'foo option', alias: 'f' },
bar: { description: 'bar option', alias: 'b' }
};
return yargs('-f --bar 20'.split(' '))
.usage('Usage: $0 [options]', opts)
.requiresArg(['foo', 'bar'])
.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([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Missing argument value: foo',
]);
});
});
});
context("with strict() option set", function () {

@@ -296,0 +394,0 @@ it('should fail given an option argument that is not demanded', function () {

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc