Comparing version 1.3.0 to 1.3.1
@@ -7,3 +7,3 @@ var yargs = require('yargs'); | ||
required: true, | ||
short: 'a', | ||
alias: 'a', | ||
}, | ||
@@ -13,3 +13,3 @@ 'info': { | ||
boolean: true, | ||
short: 'i' | ||
alias: 'i' | ||
} | ||
@@ -16,0 +16,0 @@ }).argv; |
16
index.js
@@ -113,3 +113,3 @@ var path = require('path'); | ||
var demanded = {}; | ||
self.demand = function (keys, msg) { | ||
self.demand = self.required = self.require = function (keys, msg) { | ||
if (typeof keys == 'number') { | ||
@@ -126,3 +126,8 @@ if (!demanded._) demanded._ = { count: 0, msg: null }; | ||
else { | ||
demanded[keys] = { msg: msg }; | ||
if (typeof msg === 'string') { | ||
demanded[keys] = { msg: msg }; | ||
} | ||
else if (msg === true || typeof msg === 'undefined') { | ||
demanded[keys] = { msg: null }; | ||
} | ||
} | ||
@@ -223,3 +228,8 @@ | ||
if (opt.alias) self.alias(key, opt.alias); | ||
if (opt.demand) self.demand(key, opt.demand); | ||
var demand = opt.demand || opt.required || opt.require; | ||
if (demand) { | ||
self.demand(key, demand); | ||
} | ||
if (typeof opt.default !== 'undefined') { | ||
@@ -226,0 +236,0 @@ self.default(key, opt.default); |
{ | ||
"name": "yargs", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
"main": "./index.js", | ||
"dependencies": { | ||
"minimist": "^0.2.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -10,0 +8,0 @@ "hashish": "*", |
@@ -375,4 +375,8 @@ yargs | ||
.demand(key, [msg]) | ||
------------ | ||
.demand(key, [msg | boolean]) | ||
----------------------------- | ||
.require(key, [msg | boolean]) | ||
------------------------------ | ||
.required(key, [msg | boolean]) | ||
------------------------------- | ||
@@ -387,5 +391,8 @@ If `key` is a string, show the usage information and exit if `key` wasn't | ||
If `msg` is supplied, it will be printed when the argument is missing, | ||
If a `msg` string is given, it will be printed when the argument is missing, | ||
instead of the standard error message. This is especially helpful for the non-option arguments in `argv._`. | ||
If a `boolean` value is given, it controls whether the option is demanded; | ||
this is useful when using `.options()` to specify command line parameters. | ||
.requiresArg(key) | ||
@@ -392,0 +399,0 @@ ----------------- |
@@ -7,58 +7,85 @@ var should = require('chai').should(), | ||
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; | ||
describe('demand options', function () { | ||
describe('using .demand()', function () { | ||
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', | ||
'Options:', | ||
' -x [required]', | ||
' -y [required]', | ||
'Missing required arguments: y' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
describe('using .require()', function() { | ||
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') | ||
.require(['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', | ||
'Options:', | ||
' -x [required]', | ||
' -y [required]', | ||
'Missing required arguments: y' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
}); | ||
}); | ||
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', | ||
'Options:', | ||
' -x [required]', | ||
' -y [required]', | ||
'Missing required arguments: y' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
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; | ||
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', | ||
'Options:', | ||
' -x [required]', | ||
' -y [required]', | ||
'Missing required arguments: x, y', | ||
'x and y are both required to multiply all the things' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
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', | ||
'Options:', | ||
' -x [required]', | ||
' -y [required]', | ||
'Missing required arguments: x, y', | ||
'x and y are both required to multiply all the things' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
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; | ||
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); | ||
}); | ||
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); | ||
}); | ||
@@ -536,2 +563,76 @@ | ||
describe('demand option with boolean flag', function () { | ||
describe('with demand option', function () { | ||
it('should report missing required arguments', function () { | ||
var r = checkUsage(function () { | ||
return yargs('-y 10 -z 20'.split(' ')) | ||
.usage('Usage: $0 -x NUM [-y NUM]') | ||
.options({ | ||
'x': { description: 'an option', demand: true }, | ||
'y': { description: 'another option', demand: false } | ||
}) | ||
.argv; | ||
}); | ||
r.result.should.have.property('y', 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]', | ||
'', | ||
'Options:', | ||
' -x an option [required]', | ||
' -y another option', | ||
'', | ||
'Missing required arguments: x' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
}); | ||
describe('with required option', function () { | ||
it('should report missing required arguments', function () { | ||
var r = checkUsage(function () { | ||
return yargs('-y 10 -z 20'.split(' ')) | ||
.usage('Usage: $0 -x NUM [-y NUM]') | ||
.options({ | ||
'x': { description: 'an option', required: true }, | ||
'y': { description: 'another option', required: false } | ||
}) | ||
.argv; | ||
}); | ||
r.result.should.have.property('y', 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]', | ||
'', | ||
'Options:', | ||
' -x an option [required]', | ||
' -y another option', | ||
'', | ||
'Missing required arguments: x' | ||
]); | ||
r.logs.should.have.length(0); | ||
r.exit.should.be.ok; | ||
}); | ||
}); | ||
it('should not report missing required arguments when given an alias', function () { | ||
var r = checkUsage(function () { | ||
return yargs('-w 10'.split(' ')) | ||
.usage('Usage: $0 --width NUM [--height NUM]') | ||
.options({ | ||
'width': { description: 'Width', alias: 'w', demand: true }, | ||
'height': { description: 'Height', alias: 'h', demand: false } | ||
}) | ||
.argv; | ||
}); | ||
r.result.should.have.property('w', 10); | ||
r.result.should.have.property('_').with.length(0); | ||
r.should.have.property('errors').with.length(0); | ||
r.logs.should.have.length(0); | ||
}); | ||
}); | ||
describe('help option', function () { | ||
@@ -538,0 +639,0 @@ it('should display usage', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112374
0
2384
682
- Removedminimist@^0.2.0
- Removedminimist@0.2.4(transitive)