yargs-parser
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,3 @@ | ||
var argv = require('./')(process.argv.slice(2)) | ||
console.log(argv) | ||
var parser = require('./') | ||
var parse = parser(['-cats', 'meow']) | ||
console.log(parse) |
41
index.js
var camelCase = require('camelcase') | ||
var findUp = require('find-up') | ||
var pkgConf = require('pkg-conf') | ||
var path = require('path') | ||
@@ -13,2 +15,3 @@ var tokenizeArgString = require('./lib/tokenize-arg-string') | ||
var aliases = combineAliases(opts.alias || {}) | ||
var configuration = loadConfiguration(opts.cwd || path.resolve(path.dirname(__filename), '../')) | ||
var defaults = opts.default || {} | ||
@@ -102,7 +105,9 @@ var envPrefix = opts.envPrefix | ||
// -- seperated by = | ||
if (arg.match(/^--.+=/)) { | ||
if (arg.match(/^--.+=/) || ( | ||
!configuration['short-option-groups'] && arg.match(/^-.+=/) | ||
)) { | ||
// Using [\s\S] instead of . because js doesn't support the | ||
// 'dotall' regex modifier. See: | ||
// http://stackoverflow.com/a/1068308/13216 | ||
m = arg.match(/^--([^=]+)=([\s\S]*)$/) | ||
m = arg.match(/^--?([^=]+)=([\s\S]*)$/) | ||
@@ -120,3 +125,3 @@ // nargs format = '--f=monkey washing cat' | ||
} | ||
} else if (arg.match(/^--no-.+/)) { | ||
} else if (arg.match(/^--no-.+/) && configuration['boolean-negation']) { | ||
key = arg.match(/^--no-(.+)/)[1] | ||
@@ -126,4 +131,6 @@ setArg(key, false) | ||
// -- seperated by space. | ||
} else if (arg.match(/^--.+/)) { | ||
key = arg.match(/^--(.+)/)[1] | ||
} else if (arg.match(/^--.+/) || ( | ||
!configuration['short-option-groups'] && arg.match(/^-.+/) | ||
)) { | ||
key = arg.match(/^--?(.+)/)[1] | ||
@@ -301,3 +308,3 @@ // nargs format = '--foo a b c' | ||
if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length)) { | ||
if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length) && configuration['camel-case-expansion']) { | ||
var c = camelCase(key) | ||
@@ -443,2 +450,5 @@ flags.aliases[key] = [c] | ||
var o = obj | ||
if (!configuration['dot-notation']) keys = [keys.join('.')] | ||
keys.slice(0, -1).forEach(function (key) { | ||
@@ -476,3 +486,3 @@ if (o[key] === undefined) o[key] = {} | ||
flags.aliases[key].concat(key).forEach(function (x) { | ||
if (/-/.test(x)) { | ||
if (/-/.test(x) && configuration['camel-case-expansion']) { | ||
var c = camelCase(x) | ||
@@ -539,2 +549,3 @@ flags.aliases[key].push(c) | ||
function isNumber (x) { | ||
if (!configuration['parse-numbers']) return false | ||
if (typeof x === 'number') return true | ||
@@ -549,6 +560,20 @@ if (/^0x[0-9a-f]+$/i.test(x)) return true | ||
aliases: flags.aliases, | ||
newAliases: newAliases | ||
newAliases: newAliases, | ||
configuration: configuration | ||
} | ||
} | ||
function loadConfiguration (cwd) { | ||
return pkgConf.sync('yargs', { | ||
defaults: { | ||
'short-option-groups': true, | ||
'camel-case-expansion': true, | ||
'dot-notation': true, | ||
'parse-numbers': true, | ||
'boolean-negation': true | ||
}, | ||
cwd: findUp.sync('package.json', {cwd: cwd}) | ||
}) | ||
} | ||
// if any aliases reference each other, we should | ||
@@ -555,0 +580,0 @@ // merge them together. |
{ | ||
"name": "yargs-parser", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "the mighty option parser used by yargs", | ||
@@ -11,2 +11,5 @@ "main": "index.js", | ||
}, | ||
"repository": { | ||
"url": "git@github.com:yargs/yargs-parser.git" | ||
}, | ||
"keywords": [ | ||
@@ -30,7 +33,10 @@ "argument", | ||
"nyc": "^5.3.0", | ||
"rimraf": "^2.5.1", | ||
"standard": "^5.4.1" | ||
}, | ||
"dependencies": { | ||
"camelcase": "^2.0.1" | ||
"camelcase": "^2.0.1", | ||
"find-up": "^1.1.0", | ||
"pkg-conf": "^1.1.1" | ||
} | ||
} |
@@ -6,6 +6,8 @@ # yargs-parser | ||
[![NPM version](https://img.shields.io/npm/v/yargs-parser.svg)](https://www.npmjs.com/package/yargs-parser) | ||
[![Windows Tests](https://img.shields.io/appveyor/ci/yargs/yargs-parser/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/yargs/yargs-parser) | ||
[![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/yargs-parser/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/yargs-parser) | ||
The mighty option parser used by yargs. | ||
The mighty option parser used by [yargs](https://github.com/bcoe/yargs). | ||
visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions. | ||
<img width="250" src="yargs-logo.png"> | ||
@@ -85,2 +87,3 @@ | ||
* `newAliases`: any new aliases added via camel-case expansion. | ||
* `configuration`: the configuration loaded from the `yargs` stanza in package.json. | ||
@@ -124,3 +127,3 @@ ### Configuration | ||
* default: `true`. | ||
* key: `camel-case`. | ||
* key: `camel-case-expansion`. | ||
@@ -127,0 +130,0 @@ Should hyphenated arguments be expanded into camel-case aliases? |
@@ -1,2 +0,2 @@ | ||
/* global beforeEach, describe, it */ | ||
/* global beforeEach, describe, it, afterEach */ | ||
@@ -9,2 +9,3 @@ require('chai').should() | ||
var path = require('path') | ||
var rimraf = require('rimraf') | ||
@@ -1413,2 +1414,161 @@ describe('yargs-parser', function () { | ||
}) | ||
describe('configuration', function () { | ||
beforeEach(function () { | ||
rimraf.sync('./test/fixtures/package.json') | ||
}) | ||
afterEach(function () { | ||
rimraf.sync('./test/fixtures/package.json') | ||
}) | ||
it('reads configuration from parent package.json', function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'short-option-groups': false | ||
} | ||
}), 'utf-8') | ||
var result = parser.detailed([], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
result.configuration['short-option-groups'].should.equal(false) | ||
}) | ||
describe('short option groups', function () { | ||
it('allows short-option-groups to be disabled', function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'short-option-groups': false | ||
} | ||
}), 'utf-8') | ||
var parse = parser(['-cats=meow'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
parse.cats.should.equal('meow') | ||
parse = parser(['-cats', 'meow'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
parse.cats.should.equal('meow') | ||
}) | ||
}) | ||
describe('camel-case expansion', function () { | ||
beforeEach(function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'camel-case-expansion': false | ||
} | ||
}), 'utf-8') | ||
}) | ||
it('does not expand camel-case aliases', function () { | ||
var parsed = parser.detailed([], { | ||
alias: { | ||
'foo-bar': ['x'] | ||
}, | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed.newAliases.fooBar).to.equal(undefined) | ||
expect(parsed.aliases.fooBar).to.equal(undefined) | ||
}) | ||
it('does not expand camel-case keys', function () { | ||
var parsed = parser.detailed(['--foo-bar=apple'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed.argv.fooBar).to.equal(undefined) | ||
expect(parsed.argv['foo-bar']).to.equal('apple') | ||
}) | ||
}) | ||
describe('dot notation', function () { | ||
beforeEach(function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'dot-notation': false | ||
} | ||
}), 'utf-8') | ||
}) | ||
it('does not expand dot notation defaults', function () { | ||
var parsed = parser([], { | ||
default: { | ||
'foo.bar': 'x' | ||
}, | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed['foo.bar']).to.equal('x') | ||
}) | ||
it('does not expand dot notation arguments', function () { | ||
var parsed = parser(['--foo.bar', 'banana'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed['foo.bar']).to.equal('banana') | ||
parsed = parser(['--foo.bar=banana'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed['foo.bar']).to.equal('banana') | ||
}) | ||
}) | ||
describe('parse numbers', function () { | ||
beforeEach(function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'parse-numbers': false | ||
} | ||
}), 'utf-8') | ||
}) | ||
it('does not coerce defaults into numbers', function () { | ||
var parsed = parser([], { | ||
default: { | ||
'foo': '5' | ||
}, | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed['foo']).to.equal('5') | ||
}) | ||
it('does not coerce arguments into numbers', function () { | ||
var parsed = parser(['--foo', '5'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed['foo']).to.equal('5') | ||
}) | ||
it('does not coerce positional arguments into numbers', function () { | ||
var parsed = parser(['5'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
expect(parsed._[0]).to.equal('5') | ||
}) | ||
}) | ||
describe('boolean negation', function () { | ||
beforeEach(function () { | ||
fs.writeFileSync('./test/fixtures/package.json', JSON.stringify({ | ||
yargs: { | ||
'boolean-negation': false | ||
} | ||
}), 'utf-8') | ||
}) | ||
it('does not negate arguments prefixed with --no-', function () { | ||
var parsed = parser(['--no-dice'], { | ||
cwd: './test/fixtures/inner' | ||
}) | ||
parsed['no-dice'].should.equal(true) | ||
expect(parsed.dice).to.equal(undefined) | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
75302
15
1965
206
3
6
+ Addedfind-up@^1.1.0
+ Addedpkg-conf@^1.1.1
+ Addederror-ex@1.3.2(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpkg-conf@1.1.3(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsymbol@0.2.3(transitive)