Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "argly", | ||
"description": "A concise command line arguments parser with robust type handling", | ||
"keywords": [ | ||
"argument", | ||
"args", | ||
"option", | ||
"parser", | ||
"parsing", | ||
"cli", | ||
"command" | ||
], | ||
"homepage": "https://github.com/patrick-steele-idem/argly", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/patrick-steele-idem/argly.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint src" | ||
}, | ||
"author": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"contributors": [ | ||
"Patrick Steele-Idem <pnidem@gmail.com>", | ||
"Phillip Gates-Idem <phillip.idem@gmail.com>" | ||
], | ||
"maintainers": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "~1.15.1", | ||
"chai": "~1.8.1", | ||
"jshint": "^2.4.4" | ||
}, | ||
"license": "MIT", | ||
"bin": {}, | ||
"main": "src/index.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"version": "1.0.0" | ||
} | ||
"name": "argly", | ||
"description": "A concise command line arguments parser with robust type handling", | ||
"keywords": [ | ||
"argument", | ||
"args", | ||
"option", | ||
"parser", | ||
"parsing", | ||
"cli", | ||
"command" | ||
], | ||
"homepage": "https://github.com/patrick-steele-idem/argly", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/patrick-steele-idem/argly.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint src" | ||
}, | ||
"author": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"contributors": [ | ||
"Patrick Steele-Idem <pnidem@gmail.com>", | ||
"Phillip Gates-Idem <phillip.idem@gmail.com>" | ||
], | ||
"maintainers": "Patrick Steele-Idem <pnidem@gmail.com>", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "~1.15.1", | ||
"chai": "~1.8.1", | ||
"jshint": "^2.4.4" | ||
}, | ||
"license": "MIT", | ||
"bin": {}, | ||
"main": "src/index.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"version": "1.1.0" | ||
} |
@@ -303,14 +303,3 @@ argly | ||
* Detect repeated declared options and throw an error | ||
* Add support for a default value | ||
```javascript | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'boolean', | ||
defaultValue: true | ||
} | ||
}); | ||
``` | ||
# Additional Reading | ||
@@ -317,0 +306,0 @@ |
@@ -36,2 +36,77 @@ var optionRegExp = /^--?(.+)$/; | ||
var strToBool = { | ||
'1': true, | ||
'true': true, | ||
'yes': true, | ||
'y': true, | ||
'0': false, | ||
'false': false, | ||
'no': false, | ||
'n': false | ||
}; | ||
var defaultTypeHandlers = { | ||
'string': function(defaultVal) { | ||
if (typeof defaultVal === 'string') { | ||
return defaultVal; | ||
} | ||
}, | ||
'boolean': function(defaultVal) { | ||
if (typeof defaultVal === 'boolean') { | ||
return defaultVal; | ||
} else if (typeof defaultVal === 'string') { | ||
defaultVal = defaultVal.toLowerCase(); | ||
var boolConversion = strToBool[defaultVal]; | ||
if (typeof boolConversion !== 'undefined') { | ||
return boolConversion; | ||
} | ||
} | ||
}, | ||
'int': function(defaultVal) { | ||
if (typeof defaultVal === 'number' || typeof defaultVal === 'string') { | ||
return parseInt(defaultVal, 10); | ||
} | ||
}, | ||
'integer': function(defaultVal) { | ||
if (typeof defaultVal === 'number' || typeof defaultVal === 'string') { | ||
return parseInt(defaultVal, 10); | ||
} | ||
}, | ||
'number': function(defaultVal) { | ||
if (typeof defaultVal === 'number' || typeof defaultVal === 'string') { | ||
return parseFloat(defaultVal); | ||
} | ||
} | ||
}; | ||
function handleDefaults(state, onError) { | ||
state.options.getOptions().forEach(function(option) { | ||
var targetProperty = option.targetProperty; | ||
var defaultValue = option.defaultValue; | ||
// Skip options that already have a value or do not have a default | ||
if (typeof state.result[targetProperty] !== 'undefined' || | ||
typeof defaultValue === 'undefined') { | ||
return; | ||
} | ||
var handler; | ||
// If the option has a type and it is not a complex type, we validate | ||
// that the default is the type we expect | ||
if (option.type && (handler = defaultTypeHandlers[option.type])) { | ||
var result = handler(defaultValue); | ||
if (typeof result === 'undefined') { | ||
return onError("Invalid default value '" + defaultValue + "' for target property '" + targetProperty + "'"); | ||
} | ||
state.result[option.targetProperty] = result; | ||
} else { | ||
state.result[option.targetProperty] = option.defaultValue; | ||
} | ||
}); | ||
} | ||
function Options(config) { | ||
@@ -108,2 +183,6 @@ this._lookup = {}; | ||
getOptions: function() { | ||
return this._options; | ||
}, | ||
get: function(optionName) { | ||
@@ -258,3 +337,2 @@ return this._lookup[optionName]; | ||
var onError = this._onError ? this._onError.bind(this) : DEFAULT_ON_ERROR; | ||
@@ -405,2 +483,3 @@ | ||
finishLastOption(); | ||
handleDefaults(state, onError); | ||
@@ -417,5 +496,2 @@ // Run the validators | ||
return state.result; | ||
} | ||
@@ -422,0 +498,0 @@ }; |
107
test/test.js
@@ -201,3 +201,108 @@ 'use strict'; | ||
}); | ||
it('should allow default values', function() { | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'string', | ||
defaultValue: 'bar' | ||
}, | ||
'--bar -b': { | ||
type: 'int', | ||
defaultValue: 5 | ||
}, | ||
'--hello -h': { | ||
type: 'string', | ||
defaultValue: 'hello world' | ||
} | ||
}); | ||
var parsed = parser.parse('--foo bor'.split(/\s/)); | ||
expect(parsed).to.deep.equal({ | ||
foo: 'bor', | ||
bar: 5, | ||
hello: 'hello world' | ||
}); | ||
}); | ||
it('should allow default values use pseudo defaults', function() { | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'string' | ||
}, | ||
'--test -t': { | ||
type: 'boolean', | ||
defaultValue: 'y' | ||
} | ||
}); | ||
var parsed = parser.parse('--foo bar'.split(/\s/)); | ||
expect(parsed).to.deep.equal({ | ||
foo: 'bar', | ||
test: true | ||
}); | ||
}); | ||
it('should receive error when default value is incorrect type', function() { | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'string', | ||
}, | ||
'--bar -b': { | ||
type: 'int', | ||
defaultValue: true | ||
} | ||
}); | ||
expect(function() { | ||
parser.parse('--foo bor'.split(/\s/)); | ||
}).to.throw(/Invalid default value \'true\' for target property \'bar\'/); | ||
}); | ||
it('should allow default value using complex types', function() { | ||
var parser = require('../') | ||
.createParser({ | ||
'--minify -m': 'boolean', | ||
'--hello': 'string', | ||
'--plugins': { | ||
options: { | ||
'--module -m *': 'string', | ||
'-*': null | ||
}, | ||
defaultValue: [ | ||
{ | ||
module: 'foo', | ||
x: true, | ||
y: true | ||
}, | ||
{ | ||
module: 'bar', | ||
z: 'hello' | ||
} | ||
] | ||
} | ||
}); | ||
var parsed = parser.parse('--minify --plugins [ foo -x -y ] [ bar -z hello ] --hello world'.split(/\s/)); | ||
expect(parsed).to.deep.equal({ | ||
minify: true, | ||
hello: 'world', | ||
plugins: [ | ||
{ | ||
module: 'foo', | ||
x: true, | ||
y: true | ||
}, | ||
{ | ||
module: 'bar', | ||
z: 'hello' | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
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
33608
664
311