command-line-args
Advanced tools
Comparing version
@@ -7,3 +7,2 @@ #!/usr/bin/env node | ||
var path = require('path') | ||
var ansi = require('ansi-escape-sequences') | ||
@@ -29,4 +28,4 @@ var tmpPath = path.join(os.tmpDir(), Date.now() + '-cla.js') | ||
function halt (msg) { | ||
console.error(ansi.format(msg, 'red')) | ||
console.error(msg) | ||
process.exit(1) | ||
} |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var arrayify = require('array-back'); | ||
@@ -15,10 +11,6 @@ var option = require('./option'); | ||
var Argv = (function (_Array) { | ||
_inherits(Argv, _Array); | ||
var Argv = function () { | ||
function Argv(argv) { | ||
_classCallCheck(this, Argv); | ||
_get(Object.getPrototypeOf(Argv.prototype), 'constructor', this).call(this); | ||
if (argv) { | ||
@@ -31,18 +23,9 @@ argv = arrayify(argv); | ||
this.load(argv); | ||
this.list = argv; | ||
} | ||
_createClass(Argv, [{ | ||
key: 'load', | ||
value: function load(array) { | ||
var _this = this; | ||
arrayify(array).forEach(function (item) { | ||
return _this.push(item); | ||
}); | ||
} | ||
}, { | ||
key: 'clear', | ||
value: function clear() { | ||
this.length = 0; | ||
this.list.length = 0; | ||
} | ||
@@ -52,15 +35,19 @@ }, { | ||
value: function expandOptionEqualsNotation() { | ||
var _this = this; | ||
var optEquals = option.optEquals; | ||
if (this.some(optEquals.test.bind(optEquals))) { | ||
var expandedArgs = []; | ||
this.forEach(function (arg) { | ||
var matches = arg.match(optEquals.re); | ||
if (matches) { | ||
expandedArgs.push(matches[1], matches[2]); | ||
} else { | ||
expandedArgs.push(arg); | ||
} | ||
}); | ||
this.clear(); | ||
this.load(expandedArgs); | ||
if (this.list.some(optEquals.test.bind(optEquals))) { | ||
(function () { | ||
var expandedArgs = []; | ||
_this.list.forEach(function (arg) { | ||
var matches = arg.match(optEquals.re); | ||
if (matches) { | ||
expandedArgs.push(matches[1], matches[2]); | ||
} else { | ||
expandedArgs.push(arg); | ||
} | ||
}); | ||
_this.clear(); | ||
_this.list = expandedArgs; | ||
})(); | ||
} | ||
@@ -72,5 +59,5 @@ } | ||
var combinedArg = option.combined; | ||
var hasGetopt = this.some(combinedArg.test.bind(combinedArg)); | ||
var hasGetopt = this.list.some(combinedArg.test.bind(combinedArg)); | ||
if (hasGetopt) { | ||
findReplace(this, combinedArg.re, function (arg) { | ||
findReplace(this.list, combinedArg.re, function (arg) { | ||
arg = arg.slice(1); | ||
@@ -86,5 +73,5 @@ return arg.split('').map(function (letter) { | ||
value: function validate(definitions) { | ||
var invalidOption; | ||
var invalidOption = undefined; | ||
var optionWithoutDefinition = this.filter(function (arg) { | ||
var optionWithoutDefinition = this.list.filter(function (arg) { | ||
return option.isOption(arg); | ||
@@ -104,3 +91,3 @@ }).some(function (arg) { | ||
return Argv; | ||
})(Array); | ||
}(); | ||
@@ -107,0 +94,0 @@ function halt(name, message) { |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var arrayify = require('array-back'); | ||
var o = require('object-tools'); | ||
var Definitions = require('./definitions'); | ||
var option = require('./option'); | ||
var _getUsage = require('command-line-usage'); | ||
var t = require('typical'); | ||
@@ -17,3 +17,3 @@ var Argv = require('./argv'); | ||
var CommandLineArgs = (function () { | ||
var CommandLineArgs = function () { | ||
function CommandLineArgs(definitions) { | ||
@@ -36,5 +36,5 @@ _classCallCheck(this, CommandLineArgs); | ||
var output = this.definitions.createOutput(); | ||
var def; | ||
var def = undefined; | ||
argv.forEach(function (item) { | ||
argv.list.forEach(function (item) { | ||
if (option.isOption(item)) { | ||
@@ -62,27 +62,34 @@ def = _this.definitions.get(item); | ||
o.each(output, function (value, key) { | ||
for (var key in output) { | ||
var value = output[key]; | ||
if (Array.isArray(value) && value._initial) delete value._initial; | ||
}); | ||
} | ||
if (this.definitions.isGrouped()) { | ||
var grouped = { | ||
_all: output | ||
}; | ||
var _ret = function () { | ||
var grouped = { | ||
_all: output | ||
}; | ||
this.definitions.whereGrouped().forEach(function (def) { | ||
arrayify(def.group).forEach(function (groupName) { | ||
grouped[groupName] = grouped[groupName] || {}; | ||
_this.definitions.whereGrouped().forEach(function (def) { | ||
arrayify(def.group).forEach(function (groupName) { | ||
grouped[groupName] = grouped[groupName] || {}; | ||
if (t.isDefined(output[def.name])) { | ||
grouped[groupName][def.name] = output[def.name]; | ||
} | ||
}); | ||
}); | ||
_this.definitions.whereNotGrouped().forEach(function (def) { | ||
if (t.isDefined(output[def.name])) { | ||
grouped[groupName][def.name] = output[def.name]; | ||
if (!grouped._none) grouped._none = {}; | ||
grouped._none[def.name] = output[def.name]; | ||
} | ||
}); | ||
}); | ||
return { | ||
v: grouped | ||
}; | ||
}(); | ||
this.definitions.whereNotGrouped().forEach(function (def) { | ||
if (t.isDefined(output[def.name])) { | ||
if (!grouped._none) grouped._none = {}; | ||
grouped._none[def.name] = output[def.name]; | ||
} | ||
}); | ||
return grouped; | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} else { | ||
@@ -95,3 +102,4 @@ return output; | ||
value: function getUsage(options) { | ||
return _getUsage(this.definitions, options); | ||
var getUsage = require('command-line-usage'); | ||
return getUsage(this.definitions, options); | ||
} | ||
@@ -101,3 +109,3 @@ }]); | ||
return CommandLineArgs; | ||
})(); | ||
}(); | ||
@@ -104,0 +112,0 @@ function outputSet(output, property, value) { |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var OptionDefinition = (function () { | ||
var OptionDefinition = function () { | ||
function OptionDefinition(definition) { | ||
@@ -49,4 +49,4 @@ _classCallCheck(this, OptionDefinition); | ||
return OptionDefinition; | ||
})(); | ||
}(); | ||
module.exports = OptionDefinition; |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var arrayify = require('array-back'); | ||
@@ -16,5 +12,3 @@ var option = require('./option'); | ||
var Definitions = (function (_Array) { | ||
_inherits(Definitions, _Array); | ||
var Definitions = function () { | ||
function Definitions(definitions) { | ||
@@ -25,5 +19,5 @@ var _this = this; | ||
_get(Object.getPrototypeOf(Definitions.prototype), 'constructor', this).call(this); | ||
this.list = []; | ||
arrayify(definitions).forEach(function (def) { | ||
return _this.push(new Definition(def)); | ||
return _this.list.push(new Definition(def)); | ||
}); | ||
@@ -36,3 +30,3 @@ this.validate(); | ||
value: function validate(argv) { | ||
var someHaveNoName = this.some(function (def) { | ||
var someHaveNoName = this.list.some(function (def) { | ||
return !def.name; | ||
@@ -44,3 +38,3 @@ }); | ||
var someDontHaveFunctionType = this.some(function (def) { | ||
var someDontHaveFunctionType = this.list.some(function (def) { | ||
return def.type && typeof def.type !== 'function'; | ||
@@ -52,5 +46,5 @@ }); | ||
var invalidOption; | ||
var invalidOption = undefined; | ||
var numericAlias = this.some(function (def) { | ||
var numericAlias = this.list.some(function (def) { | ||
invalidOption = def; | ||
@@ -63,3 +57,3 @@ return t.isDefined(def.alias) && t.isNumber(def.alias); | ||
var multiCharacterAlias = this.some(function (def) { | ||
var multiCharacterAlias = this.list.some(function (def) { | ||
invalidOption = def; | ||
@@ -72,3 +66,3 @@ return t.isDefined(def.alias) && def.alias.length !== 1; | ||
var hypenAlias = this.some(function (def) { | ||
var hypenAlias = this.list.some(function (def) { | ||
invalidOption = def; | ||
@@ -81,3 +75,3 @@ return def.alias === '-'; | ||
var duplicateName = hasDuplicates(this.map(function (def) { | ||
var duplicateName = hasDuplicates(this.list.map(function (def) { | ||
return def.name; | ||
@@ -89,3 +83,3 @@ })); | ||
var duplicateAlias = hasDuplicates(this.map(function (def) { | ||
var duplicateAlias = hasDuplicates(this.list.map(function (def) { | ||
return def.alias; | ||
@@ -97,3 +91,3 @@ })); | ||
var duplicateDefaultOption = hasDuplicates(this.map(function (def) { | ||
var duplicateDefaultOption = hasDuplicates(this.list.map(function (def) { | ||
return def.defaultOption; | ||
@@ -109,3 +103,3 @@ })); | ||
var output = {}; | ||
this.forEach(function (def) { | ||
this.list.forEach(function (def) { | ||
if (t.isDefined(def.defaultValue)) output[def.name] = def.defaultValue; | ||
@@ -121,5 +115,5 @@ if (Array.isArray(output[def.name])) { | ||
value: function get(arg) { | ||
return option.short.test(arg) ? this.find(function (def) { | ||
return option.short.test(arg) ? this.list.find(function (def) { | ||
return def.alias === option.short.name(arg); | ||
}) : this.find(function (def) { | ||
}) : this.list.find(function (def) { | ||
return def.name === option.long.name(arg); | ||
@@ -131,3 +125,3 @@ }); | ||
value: function getDefault() { | ||
return this.find(function (def) { | ||
return this.list.find(function (def) { | ||
return def.defaultOption === true; | ||
@@ -139,3 +133,3 @@ }); | ||
value: function isGrouped() { | ||
return this.some(function (def) { | ||
return this.list.some(function (def) { | ||
return def.group; | ||
@@ -147,3 +141,3 @@ }); | ||
value: function whereGrouped() { | ||
return this.filter(containsValidGroup); | ||
return this.list.filter(containsValidGroup); | ||
} | ||
@@ -153,3 +147,3 @@ }, { | ||
value: function whereNotGrouped() { | ||
return this.filter(function (def) { | ||
return this.list.filter(function (def) { | ||
return !containsValidGroup(def); | ||
@@ -161,3 +155,3 @@ }); | ||
return Definitions; | ||
})(Array); | ||
}(); | ||
@@ -164,0 +158,0 @@ function halt(name, message) { |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Arg = (function () { | ||
var Arg = function () { | ||
function Arg(re) { | ||
@@ -27,3 +27,3 @@ _classCallCheck(this, Arg); | ||
return Arg; | ||
})(); | ||
}(); | ||
@@ -37,2 +37,3 @@ var option = { | ||
}, | ||
optEquals: new Arg(/^(--\S+)=(.*)/) | ||
@@ -39,0 +40,0 @@ }; |
'use strict' | ||
var arrayify = require('array-back') | ||
var option = require('./option') | ||
var findReplace = require('find-replace') | ||
const arrayify = require('array-back') | ||
const option = require('./option') | ||
const findReplace = require('find-replace') | ||
@@ -13,6 +13,4 @@ /** | ||
class Argv extends Array { | ||
class Argv { | ||
constructor (argv) { | ||
super() | ||
if (argv) { | ||
@@ -26,11 +24,7 @@ argv = arrayify(argv) | ||
this.load(argv) | ||
this.list = argv | ||
} | ||
load (array) { | ||
arrayify(array).forEach(item => this.push(item)) | ||
} | ||
clear () { | ||
this.length = 0 | ||
this.list.length = 0 | ||
} | ||
@@ -40,7 +34,7 @@ | ||
expandOptionEqualsNotation () { | ||
var optEquals = option.optEquals | ||
if (this.some(optEquals.test.bind(optEquals))) { | ||
var expandedArgs = [] | ||
this.forEach(arg => { | ||
var matches = arg.match(optEquals.re) | ||
const optEquals = option.optEquals | ||
if (this.list.some(optEquals.test.bind(optEquals))) { | ||
const expandedArgs = [] | ||
this.list.forEach(arg => { | ||
const matches = arg.match(optEquals.re) | ||
if (matches) { | ||
@@ -53,3 +47,3 @@ expandedArgs.push(matches[1], matches[2]) | ||
this.clear() | ||
this.load(expandedArgs) | ||
this.list = expandedArgs | ||
} | ||
@@ -60,6 +54,6 @@ } | ||
expandGetoptNotation () { | ||
var combinedArg = option.combined | ||
var hasGetopt = this.some(combinedArg.test.bind(combinedArg)) | ||
const combinedArg = option.combined | ||
const hasGetopt = this.list.some(combinedArg.test.bind(combinedArg)) | ||
if (hasGetopt) { | ||
findReplace(this, combinedArg.re, arg => { | ||
findReplace(this.list, combinedArg.re, arg => { | ||
arg = arg.slice(1) | ||
@@ -72,5 +66,5 @@ return arg.split('').map(letter => '-' + letter) | ||
validate (definitions) { | ||
var invalidOption | ||
let invalidOption | ||
var optionWithoutDefinition = this | ||
const optionWithoutDefinition = this.list | ||
.filter(arg => option.isOption(arg)) | ||
@@ -93,3 +87,3 @@ .some(arg => { | ||
function halt (name, message) { | ||
var err = new Error(message) | ||
const err = new Error(message) | ||
err.name = name | ||
@@ -96,0 +90,0 @@ throw err |
'use strict' | ||
var arrayify = require('array-back') | ||
var o = require('object-tools') | ||
var Definitions = require('./definitions') | ||
var option = require('./option') | ||
var getUsage = require('command-line-usage') | ||
var t = require('typical') | ||
var Argv = require('./argv') | ||
const arrayify = require('array-back') | ||
const Definitions = require('./definitions') | ||
const option = require('./option') | ||
const t = require('typical') | ||
const Argv = require('./argv') | ||
@@ -36,4 +34,4 @@ /** | ||
* ```js | ||
* var commandLineArgs = require('command-line-args') | ||
* var cli = commandLineArgs([ | ||
* const commandLineArgs = require('command-line-args') | ||
* const cli = commandLineArgs([ | ||
* { name: 'file' }, | ||
@@ -50,8 +48,8 @@ * { name: 'verbose' }, | ||
/** | ||
Returns an object containing all the values and flags set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array. | ||
@param [argv] {string[]} - An array of strings, which if passed will be parsed instead of `process.argv`. | ||
@returns {object} | ||
@throws `UNKNOWN_OPTION` if the user sets an option without a definition | ||
*/ | ||
* Returns an object containing all the values and flags set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array. | ||
* | ||
* @param [argv] {string[]} - An array of strings, which if passed will be parsed instead of `process.argv`. | ||
* @returns {object} | ||
* @throws `UNKNOWN_OPTION` if the user sets an option without a definition | ||
*/ | ||
parse (argv) { | ||
@@ -64,7 +62,7 @@ argv = new Argv(argv) | ||
/* create output initialised with default values */ | ||
var output = this.definitions.createOutput() | ||
var def | ||
const output = this.definitions.createOutput() | ||
let def | ||
/* walk argv building the output */ | ||
argv.forEach(item => { | ||
argv.list.forEach(item => { | ||
if (option.isOption(item)) { | ||
@@ -78,3 +76,3 @@ def = this.definitions.get(item) | ||
} else { | ||
var value = item | ||
const value = item | ||
if (!def) { | ||
@@ -86,3 +84,3 @@ def = this.definitions.getDefault() | ||
var outputValue = def.type ? def.type(value) : value | ||
const outputValue = def.type ? def.type(value) : value | ||
outputSet(output, def.name, outputValue) | ||
@@ -95,9 +93,10 @@ | ||
/* clear _initial flags */ | ||
o.each(output, (value, key) => { | ||
for (let key in output) { | ||
const value = output[key] | ||
if (Array.isArray(value) && value._initial) delete value._initial | ||
}) | ||
} | ||
/* group the output values */ | ||
if (this.definitions.isGrouped()) { | ||
var grouped = { | ||
const grouped = { | ||
_all: output | ||
@@ -128,8 +127,9 @@ } | ||
/** | ||
Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use. | ||
@param [options] {object} - the options to pass to [command-line-usage](https://github.com/75lb/command-line-usage) | ||
@returns {string} | ||
*/ | ||
* Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use. | ||
* | ||
* @param [options] {object} - the options to pass to [command-line-usage](https://github.com/75lb/command-line-usage) | ||
* @returns {string} | ||
*/ | ||
getUsage (options) { | ||
const getUsage = require('command-line-usage') | ||
return getUsage(this.definitions, options) | ||
@@ -136,0 +136,0 @@ } |
@@ -50,3 +50,3 @@ 'use strict' | ||
* ```js | ||
* var fs = require('fs') | ||
* const fs = require('fs') | ||
* | ||
@@ -59,3 +59,3 @@ * function FileDetails(filename){ | ||
* | ||
* var cli = commandLineArgs([ | ||
* const cli = commandLineArgs([ | ||
* { name: 'file', type: FileDetails }, | ||
@@ -225,3 +225,3 @@ * { name: 'depth', type: Number } | ||
/* pick up any remaining properties */ | ||
for (var prop in definition) { | ||
for (let prop in definition) { | ||
if (!this[prop]) this[prop] = definition[prop] | ||
@@ -228,0 +228,0 @@ } |
'use strict' | ||
var arrayify = require('array-back') | ||
var option = require('./option') | ||
var Definition = require('./definition') | ||
var t = require('typical') | ||
const arrayify = require('array-back') | ||
const option = require('./option') | ||
const Definition = require('./definition') | ||
const t = require('typical') | ||
/** | ||
* @class | ||
* @module definitions | ||
* @private | ||
*/ | ||
/** | ||
* @alias module:definitions | ||
*/ | ||
class Definitions extends Array { | ||
class Definitions { | ||
constructor (definitions) { | ||
super() | ||
arrayify(definitions).forEach(def => this.push(new Definition(def))) | ||
this.list = [] | ||
arrayify(definitions).forEach(def => this.list.push(new Definition(def))) | ||
this.validate() | ||
@@ -23,3 +27,3 @@ } | ||
validate (argv) { | ||
var someHaveNoName = this.some(def => !def.name) | ||
const someHaveNoName = this.list.some(def => !def.name) | ||
if (someHaveNoName) { | ||
@@ -32,3 +36,3 @@ halt( | ||
var someDontHaveFunctionType = this.some(def => def.type && typeof def.type !== 'function') | ||
const someDontHaveFunctionType = this.list.some(def => def.type && typeof def.type !== 'function') | ||
if (someDontHaveFunctionType) { | ||
@@ -41,5 +45,5 @@ halt( | ||
var invalidOption | ||
let invalidOption | ||
var numericAlias = this.some(def => { | ||
const numericAlias = this.list.some(def => { | ||
invalidOption = def | ||
@@ -55,3 +59,3 @@ return t.isDefined(def.alias) && t.isNumber(def.alias) | ||
var multiCharacterAlias = this.some(def => { | ||
const multiCharacterAlias = this.list.some(def => { | ||
invalidOption = def | ||
@@ -67,3 +71,3 @@ return t.isDefined(def.alias) && def.alias.length !== 1 | ||
var hypenAlias = this.some(def => { | ||
const hypenAlias = this.list.some(def => { | ||
invalidOption = def | ||
@@ -79,3 +83,3 @@ return def.alias === '-' | ||
var duplicateName = hasDuplicates(this.map(def => def.name)) | ||
const duplicateName = hasDuplicates(this.list.map(def => def.name)) | ||
if (duplicateName) { | ||
@@ -88,3 +92,3 @@ halt( | ||
var duplicateAlias = hasDuplicates(this.map(def => def.alias)) | ||
const duplicateAlias = hasDuplicates(this.list.map(def => def.alias)) | ||
if (duplicateAlias) { | ||
@@ -97,3 +101,3 @@ halt( | ||
var duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)) | ||
const duplicateDefaultOption = hasDuplicates(this.list.map(def => def.defaultOption)) | ||
if (duplicateDefaultOption) { | ||
@@ -107,5 +111,9 @@ halt( | ||
/** | ||
* Initialise .parse() output object. | ||
* @returns {object} | ||
*/ | ||
createOutput () { | ||
var output = {} | ||
this.forEach(def => { | ||
const output = {} | ||
this.list.forEach(def => { | ||
if (t.isDefined(def.defaultValue)) output[def.name] = def.defaultValue | ||
@@ -119,21 +127,25 @@ if (Array.isArray(output[def.name])) { | ||
/** | ||
* @param {string} | ||
* @returns {Definition} | ||
*/ | ||
get (arg) { | ||
return option.short.test(arg) | ||
? this.find(def => def.alias === option.short.name(arg)) | ||
: this.find(def => def.name === option.long.name(arg)) | ||
? this.list.find(def => def.alias === option.short.name(arg)) | ||
: this.list.find(def => def.name === option.long.name(arg)) | ||
} | ||
getDefault () { | ||
return this.find(def => def.defaultOption === true) | ||
return this.list.find(def => def.defaultOption === true) | ||
} | ||
isGrouped () { | ||
return this.some(def => def.group) | ||
return this.list.some(def => def.group) | ||
} | ||
whereGrouped () { | ||
return this.filter(containsValidGroup) | ||
return this.list.filter(containsValidGroup) | ||
} | ||
whereNotGrouped () { | ||
return this.filter(def => !containsValidGroup(def)) | ||
return this.list.filter(def => !containsValidGroup(def)) | ||
} | ||
@@ -144,3 +156,3 @@ | ||
function halt (name, message) { | ||
var err = new Error(message) | ||
const err = new Error(message) | ||
err.name = name | ||
@@ -155,5 +167,5 @@ throw err | ||
function hasDuplicates (array) { | ||
var items = {} | ||
for (var i = 0; i < array.length; i++) { | ||
var value = array[i] | ||
const items = {} | ||
for (let i = 0; i < array.length; i++) { | ||
const value = array[i] | ||
if (items[value]) { | ||
@@ -167,6 +179,2 @@ return true | ||
/** | ||
@module definitions | ||
@private | ||
*/ | ||
module.exports = Definitions |
@@ -23,3 +23,3 @@ 'use strict' | ||
var option = { | ||
const option = { | ||
short: new Arg(/^-([^\d-])$/), | ||
@@ -26,0 +26,0 @@ long: new Arg(/^--(\S+)/), |
{ | ||
"name": "command-line-args", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"description": "A library to collect command-line args and generate a usage guide.", | ||
@@ -12,3 +12,3 @@ "repository": "https://github.com/75lb/command-line-args.git", | ||
"cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf coverage; echo", | ||
"es5": "babel --no-comments lib --out-dir es5" | ||
"es5": "babel --presets babel-preset-es2015 --no-comments lib --out-dir es5" | ||
}, | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel": "^5.8.29", | ||
"babel-preset-es2015": "^6.5.0", | ||
"coveralls": "^2.11.4", | ||
@@ -39,3 +39,2 @@ "jsdoc-to-markdown": "^1.1.1", | ||
"dependencies": { | ||
"ansi-escape-sequences": "^2.1.1", | ||
"array-back": "^1.0.2", | ||
@@ -46,3 +45,2 @@ "command-line-usage": "^2", | ||
"find-replace": "^1", | ||
"object-tools": "^2", | ||
"typical": "^2.3.0" | ||
@@ -49,0 +47,0 @@ }, |
@@ -43,8 +43,8 @@ [](https://www.npmjs.org/package/command-line-args) | ||
{ | ||
files: [ | ||
"one.js", | ||
"two.js" | ||
], | ||
verbose: true, | ||
timeout: 1000 | ||
files: [ | ||
'one.js', | ||
'two.js' | ||
], | ||
verbose: true, | ||
timeout: 1000 | ||
} | ||
@@ -139,4 +139,4 @@ ``` | ||
```js | ||
var commandLineArgs = require('command-line-args') | ||
var cli = commandLineArgs([ | ||
const commandLineArgs = require('command-line-args') | ||
const cli = commandLineArgs([ | ||
{ name: 'file' }, | ||
@@ -223,3 +223,3 @@ { name: 'verbose' }, | ||
```js | ||
var fs = require('fs') | ||
const fs = require('fs') | ||
@@ -232,3 +232,3 @@ function FileDetails(filename){ | ||
var cli = commandLineArgs([ | ||
const cli = commandLineArgs([ | ||
{ name: 'file', type: FileDetails }, | ||
@@ -235,0 +235,0 @@ { name: 'depth', type: Number } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
2190204
2636.69%6
-25%115
139.58%3062
69.26%1
Infinity%- Removed
- Removed