command-line-args
Advanced tools
Comparing version 5.1.3 to 5.2.0
@@ -1,6 +0,6 @@ | ||
'use strict' | ||
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex.default : ex } | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
const camelCase = _interopDefault(require('lodash.camelcase')) | ||
var camelCase = _interopDefault(require('lodash.camelcase')); | ||
@@ -10,6 +10,6 @@ /** | ||
* | ||
* - converts array-like objects (e.g. `arguments`) to a real array | ||
* - converts `undefined` to an empty array | ||
* - converts any another other, singular value (including `null`) into an array containing that value | ||
* - ignores input which is already an array | ||
* - Converts array-like objects (e.g. `arguments`, `Set`) to a real array. | ||
* - Converts `undefined` to an empty array. | ||
* - Converts any another other, singular value (including `null`, objects and iterables other than `Set`) into an array containing that value. | ||
* - Ignores input which is already an array. | ||
* | ||
@@ -32,2 +32,5 @@ * @module array-back | ||
* | ||
* > arrayify(new Set([ 1, 2 ])) | ||
* [ 1, 2 ] | ||
* | ||
* > function f(){ return arrayify(arguments); } | ||
@@ -47,3 +50,3 @@ * > f(1,2,3) | ||
/** | ||
* @param {*} - the input value to convert to an array | ||
* @param {*} - The input value to convert to an array | ||
* @returns {Array} | ||
@@ -55,11 +58,13 @@ * @alias module:array-back | ||
return input | ||
} else { | ||
if (input === undefined) { | ||
return [] | ||
} else if (isArrayLike(input)) { | ||
return Array.prototype.slice.call(input) | ||
} else { | ||
return [input] | ||
} | ||
} | ||
if (input === undefined) { | ||
return [] | ||
} | ||
if (isArrayLike(input) || input instanceof Set) { | ||
return Array.from(input) | ||
} | ||
return [ input ] | ||
} | ||
@@ -118,3 +123,3 @@ | ||
} else { | ||
return [input] | ||
return [ input ] | ||
} | ||
@@ -153,15 +158,15 @@ } | ||
function findReplace (array, testFn) { | ||
const found = [] | ||
const replaceWiths = arrayify$1(arguments) | ||
replaceWiths.splice(0, 2) | ||
const found = []; | ||
const replaceWiths = arrayify$1(arguments); | ||
replaceWiths.splice(0, 2); | ||
arrayify$1(array).forEach((value, index) => { | ||
let expanded = [] | ||
let expanded = []; | ||
replaceWiths.forEach(replaceWith => { | ||
if (typeof replaceWith === 'function') { | ||
expanded = expanded.concat(replaceWith(value)) | ||
expanded = expanded.concat(replaceWith(value)); | ||
} else { | ||
expanded.push(replaceWith) | ||
expanded.push(replaceWith); | ||
} | ||
}) | ||
}); | ||
@@ -172,10 +177,10 @@ if (testFn(value)) { | ||
replaceWithValue: expanded | ||
}) | ||
}); | ||
} | ||
}) | ||
}); | ||
found.reverse().forEach(item => { | ||
const spliceArgs = [item.index, 1].concat(item.replaceWithValue) | ||
array.splice.apply(array, spliceArgs) | ||
}) | ||
const spliceArgs = [ item.index, 1 ].concat(item.replaceWithValue); | ||
array.splice.apply(array, spliceArgs); | ||
}); | ||
@@ -203,3 +208,3 @@ return array | ||
optEquals: /^(--\S+?)=(.*)/ | ||
} | ||
}; | ||
@@ -216,12 +221,12 @@ /** | ||
load (argv) { | ||
this.clear() | ||
this.clear(); | ||
if (argv && argv !== process.argv) { | ||
argv = arrayify(argv) | ||
argv = arrayify(argv); | ||
} else { | ||
/* if no argv supplied, assume we are parsing process.argv */ | ||
argv = process.argv.slice(0) | ||
const deleteCount = process.execArgv.some(isExecArg) ? 1 : 2 | ||
argv.splice(0, deleteCount) | ||
argv = process.argv.slice(0); | ||
const deleteCount = process.execArgv.some(isExecArg) ? 1 : 2; | ||
argv.splice(0, deleteCount); | ||
} | ||
argv.forEach(arg => this.push(String(arg))) | ||
argv.forEach(arg => this.push(String(arg))); | ||
} | ||
@@ -233,3 +238,3 @@ | ||
clear () { | ||
this.length = 0 | ||
this.length = 0; | ||
} | ||
@@ -242,13 +247,13 @@ | ||
if (this.some(arg => re.optEquals.test(arg))) { | ||
const expandedArgs = [] | ||
const expandedArgs = []; | ||
this.forEach(arg => { | ||
const matches = arg.match(re.optEquals) | ||
const matches = arg.match(re.optEquals); | ||
if (matches) { | ||
expandedArgs.push(matches[1], matches[2]) | ||
expandedArgs.push(matches[1], matches[2]); | ||
} else { | ||
expandedArgs.push(arg) | ||
expandedArgs.push(arg); | ||
} | ||
}) | ||
this.clear() | ||
this.load(expandedArgs) | ||
}); | ||
this.clear(); | ||
this.load(expandedArgs); | ||
} | ||
@@ -262,3 +267,3 @@ } | ||
if (this.hasCombinedShortOptions()) { | ||
findReplace(this, re.combinedShort, expandCombinedShortArg) | ||
findReplace(this, re.combinedShort, expandCombinedShortArg); | ||
} | ||
@@ -276,4 +281,4 @@ } | ||
static from (argv) { | ||
const result = new this() | ||
result.load(argv) | ||
const result = new this(); | ||
result.load(argv); | ||
return result | ||
@@ -291,3 +296,3 @@ } | ||
/* remove initial hypen */ | ||
arg = arg.slice(1) | ||
arg = arg.slice(1); | ||
return arg.split('').map(letter => '-' + letter) | ||
@@ -525,4 +530,4 @@ } | ||
if (input) { | ||
const isPromise = isDefined(Promise) && input instanceof Promise | ||
const isThenable = input.then && typeof input.then === 'function' | ||
const isPromise = isDefined(Promise) && input instanceof Promise; | ||
const isThenable = input.then && typeof input.then === 'function'; | ||
return !!(isPromise || isThenable) | ||
@@ -584,3 +589,3 @@ } else { | ||
const t = { | ||
var t = { | ||
isNumber, | ||
@@ -598,3 +603,3 @@ isString, | ||
isIterable | ||
} | ||
}; | ||
@@ -640,3 +645,3 @@ /** | ||
*/ | ||
this.name = definition.name | ||
this.name = definition.name; | ||
@@ -678,3 +683,3 @@ /** | ||
*/ | ||
this.type = definition.type || String | ||
this.type = definition.type || String; | ||
@@ -699,3 +704,3 @@ /** | ||
*/ | ||
this.alias = definition.alias | ||
this.alias = definition.alias; | ||
@@ -721,3 +726,3 @@ /** | ||
*/ | ||
this.multiple = definition.multiple | ||
this.multiple = definition.multiple; | ||
@@ -741,3 +746,3 @@ /** | ||
*/ | ||
this.lazyMultiple = definition.lazyMultiple | ||
this.lazyMultiple = definition.lazyMultiple; | ||
@@ -761,3 +766,3 @@ /** | ||
*/ | ||
this.defaultOption = definition.defaultOption | ||
this.defaultOption = definition.defaultOption; | ||
@@ -782,3 +787,3 @@ /** | ||
*/ | ||
this.defaultValue = definition.defaultValue | ||
this.defaultValue = definition.defaultValue; | ||
@@ -848,7 +853,7 @@ /** | ||
*/ | ||
this.group = definition.group | ||
this.group = definition.group; | ||
/* pick up any remaining properties */ | ||
for (const prop in definition) { | ||
if (!this[prop]) this[prop] = definition[prop] | ||
if (!this[prop]) this[prop] = definition[prop]; | ||
} | ||
@@ -866,3 +871,3 @@ } | ||
static create (def) { | ||
const result = new this(def) | ||
const result = new this(def); | ||
return result | ||
@@ -882,6 +887,7 @@ } | ||
* validate option definitions | ||
* @param {boolean} [caseInsensitive=false] - whether arguments will be parsed in a case insensitive manner | ||
* @returns {string} | ||
*/ | ||
validate () { | ||
const someHaveNoName = this.some(def => !def.name) | ||
validate (caseInsensitive) { | ||
const someHaveNoName = this.some(def => !def.name); | ||
if (someHaveNoName) { | ||
@@ -891,6 +897,6 @@ halt( | ||
'Invalid option definitions: the `name` property is required on each definition' | ||
) | ||
); | ||
} | ||
const someDontHaveFunctionType = this.some(def => def.type && typeof def.type !== 'function') | ||
const someDontHaveFunctionType = this.some(def => def.type && typeof def.type !== 'function'); | ||
if (someDontHaveFunctionType) { | ||
@@ -900,11 +906,11 @@ halt( | ||
'Invalid option definitions: the `type` property must be a setter fuction (default: `Boolean`)' | ||
) | ||
); | ||
} | ||
let invalidOption | ||
let invalidOption; | ||
const numericAlias = this.some(def => { | ||
invalidOption = def | ||
invalidOption = def; | ||
return t.isDefined(def.alias) && t.isNumber(def.alias) | ||
}) | ||
}); | ||
if (numericAlias) { | ||
@@ -914,9 +920,9 @@ halt( | ||
'Invalid option definition: to avoid ambiguity an alias cannot be numeric [--' + invalidOption.name + ' alias is -' + invalidOption.alias + ']' | ||
) | ||
); | ||
} | ||
const multiCharacterAlias = this.some(def => { | ||
invalidOption = def | ||
invalidOption = def; | ||
return t.isDefined(def.alias) && def.alias.length !== 1 | ||
}) | ||
}); | ||
if (multiCharacterAlias) { | ||
@@ -926,9 +932,9 @@ halt( | ||
'Invalid option definition: an alias must be a single character' | ||
) | ||
); | ||
} | ||
const hypenAlias = this.some(def => { | ||
invalidOption = def | ||
invalidOption = def; | ||
return def.alias === '-' | ||
}) | ||
}); | ||
if (hypenAlias) { | ||
@@ -938,6 +944,6 @@ halt( | ||
'Invalid option definition: an alias cannot be "-"' | ||
) | ||
); | ||
} | ||
const duplicateName = hasDuplicates(this.map(def => def.name)) | ||
const duplicateName = hasDuplicates(this.map(def => caseInsensitive ? def.name.toLowerCase() : def.name)); | ||
if (duplicateName) { | ||
@@ -947,6 +953,6 @@ halt( | ||
'Two or more option definitions have the same name' | ||
) | ||
); | ||
} | ||
const duplicateAlias = hasDuplicates(this.map(def => def.alias)) | ||
const duplicateAlias = hasDuplicates(this.map(def => caseInsensitive && t.isDefined(def.alias) ? def.alias.toLowerCase() : def.alias)); | ||
if (duplicateAlias) { | ||
@@ -956,6 +962,6 @@ halt( | ||
'Two or more option definitions have the same alias' | ||
) | ||
); | ||
} | ||
const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)) | ||
const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)); | ||
if (duplicateDefaultOption) { | ||
@@ -965,9 +971,9 @@ halt( | ||
'Only one option definition can be the defaultOption' | ||
) | ||
); | ||
} | ||
const defaultBoolean = this.some(def => { | ||
invalidOption = def | ||
invalidOption = def; | ||
return def.isBoolean() && def.defaultOption | ||
}) | ||
}); | ||
if (defaultBoolean) { | ||
@@ -977,3 +983,3 @@ halt( | ||
`A boolean option ["${invalidOption.name}"] can not also be the defaultOption.` | ||
) | ||
); | ||
} | ||
@@ -984,10 +990,25 @@ } | ||
* Get definition by option arg (e.g. `--one` or `-o`) | ||
* @param {string} | ||
* @param {string} [arg] the argument name to get the definition for | ||
* @param {boolean} [caseInsensitive] whether to use case insensitive comparisons when finding the appropriate definition | ||
* @returns {Definition} | ||
*/ | ||
get (arg) { | ||
get (arg, caseInsensitive) { | ||
if (isOption(arg)) { | ||
return re.short.test(arg) | ||
? this.find(def => def.alias === getOptionName(arg)) | ||
: this.find(def => def.name === getOptionName(arg)) | ||
if (re.short.test(arg)) { | ||
const shortOptionName = getOptionName(arg); | ||
if (caseInsensitive) { | ||
const lowercaseShortOptionName = shortOptionName.toLowerCase(); | ||
return this.find(def => t.isDefined(def.alias) && def.alias.toLowerCase() === lowercaseShortOptionName) | ||
} else { | ||
return this.find(def => def.alias === shortOptionName) | ||
} | ||
} else { | ||
const optionName = getOptionName(arg); | ||
if (caseInsensitive) { | ||
const lowercaseOptionName = optionName.toLowerCase(); | ||
return this.find(def => def.name.toLowerCase() === lowercaseOptionName) | ||
} else { | ||
return this.find(def => def.name === optionName) | ||
} | ||
} | ||
} else { | ||
@@ -1018,6 +1039,6 @@ return this.find(def => def.name === arg) | ||
static from (definitions) { | ||
static from (definitions, caseInsensitive) { | ||
if (definitions instanceof this) return definitions | ||
const result = super.from(arrayify(definitions), def => OptionDefinition.create(def)) | ||
result.validate() | ||
const result = super.from(arrayify(definitions), def => OptionDefinition.create(def)); | ||
result.validate(caseInsensitive); | ||
return result | ||
@@ -1028,4 +1049,4 @@ } | ||
function halt (name, message) { | ||
const err = new Error(message) | ||
err.name = name | ||
const err = new Error(message); | ||
err.name = name; | ||
throw err | ||
@@ -1039,9 +1060,9 @@ } | ||
function hasDuplicates (array) { | ||
const items = {} | ||
const items = {}; | ||
for (let i = 0; i < array.length; i++) { | ||
const value = array[i] | ||
const value = array[i]; | ||
if (items[value]) { | ||
return true | ||
} else { | ||
if (t.isDefined(value)) items[value] = true | ||
if (t.isDefined(value)) items[value] = true; | ||
} | ||
@@ -1064,9 +1085,10 @@ } | ||
* @param {boolean} [options.stopAtFirstUnknown] - | ||
* @param {boolean} [options.caseInsensitive] - Arguments will be parsed in a case insensitive manner. Defaults to false. | ||
*/ | ||
constructor (definitions, options) { | ||
this.options = Object.assign({}, options) | ||
this.options = Object.assign({}, options); | ||
/** | ||
* Option Definitions | ||
*/ | ||
this.definitions = Definitions.from(definitions) | ||
this.definitions = Definitions.from(definitions, this.options.caseInsensitive); | ||
@@ -1076,8 +1098,8 @@ /** | ||
*/ | ||
this.argv = ArgvArray.from(this.options.argv) | ||
this.argv = ArgvArray.from(this.options.argv); | ||
if (this.argv.hasCombinedShortOptions()) { | ||
findReplace(this.argv, re.combinedShort.test.bind(re.combinedShort), arg => { | ||
arg = arg.slice(1) | ||
arg = arg.slice(1); | ||
return arg.split('').map(letter => ({ origArg: `-${arg}`, arg: '-' + letter })) | ||
}) | ||
}); | ||
} | ||
@@ -1090,20 +1112,20 @@ } | ||
* [Symbol.iterator] () { | ||
const definitions = this.definitions | ||
const definitions = this.definitions; | ||
let def | ||
let value | ||
let name | ||
let event | ||
let singularDefaultSet = false | ||
let unknownFound = false | ||
let origArg | ||
let def; | ||
let value; | ||
let name; | ||
let event; | ||
let singularDefaultSet = false; | ||
let unknownFound = false; | ||
let origArg; | ||
for (let arg of this.argv) { | ||
if (t.isPlainObject(arg)) { | ||
origArg = arg.origArg | ||
arg = arg.arg | ||
origArg = arg.origArg; | ||
arg = arg.arg; | ||
} | ||
if (unknownFound && this.options.stopAtFirstUnknown) { | ||
yield { event: 'unknown_value', arg, name: '_unknown', value: undefined } | ||
yield { event: 'unknown_value', arg, name: '_unknown', value: undefined }; | ||
continue | ||
@@ -1114,9 +1136,9 @@ } | ||
if (isOption(arg)) { | ||
def = definitions.get(arg) | ||
value = undefined | ||
def = definitions.get(arg, this.options.caseInsensitive); | ||
value = undefined; | ||
if (def) { | ||
value = def.isBoolean() ? true : null | ||
event = 'set' | ||
value = def.isBoolean() ? true : null; | ||
event = 'set'; | ||
} else { | ||
event = 'unknown_option' | ||
event = 'unknown_option'; | ||
} | ||
@@ -1126,15 +1148,15 @@ | ||
} else if (isOptionEqualsNotation(arg)) { | ||
const matches = arg.match(re.optEquals) | ||
def = definitions.get(matches[1]) | ||
const matches = arg.match(re.optEquals); | ||
def = definitions.get(matches[1], this.options.caseInsensitive); | ||
if (def) { | ||
if (def.isBoolean()) { | ||
yield { event: 'unknown_value', arg, name: '_unknown', value, def } | ||
event = 'set' | ||
value = true | ||
yield { event: 'unknown_value', arg, name: '_unknown', value, def }; | ||
event = 'set'; | ||
value = true; | ||
} else { | ||
event = 'set' | ||
value = matches[2] | ||
event = 'set'; | ||
value = matches[2]; | ||
} | ||
} else { | ||
event = 'unknown_option' | ||
event = 'unknown_option'; | ||
} | ||
@@ -1145,13 +1167,13 @@ | ||
if (def) { | ||
value = arg | ||
event = 'set' | ||
value = arg; | ||
event = 'set'; | ||
} else { | ||
/* get the defaultOption */ | ||
def = this.definitions.getDefault() | ||
def = this.definitions.getDefault(); | ||
if (def && !singularDefaultSet) { | ||
value = arg | ||
event = 'set' | ||
value = arg; | ||
event = 'set'; | ||
} else { | ||
event = 'unknown_value' | ||
def = undefined | ||
event = 'unknown_value'; | ||
def = undefined; | ||
} | ||
@@ -1161,26 +1183,26 @@ } | ||
name = def ? def.name : '_unknown' | ||
const argInfo = { event, arg, name, value, def } | ||
name = def ? def.name : '_unknown'; | ||
const argInfo = { event, arg, name, value, def }; | ||
if (origArg) { | ||
argInfo.subArg = arg | ||
argInfo.arg = origArg | ||
argInfo.subArg = arg; | ||
argInfo.arg = origArg; | ||
} | ||
yield argInfo | ||
yield argInfo; | ||
/* unknownFound logic */ | ||
if (name === '_unknown') unknownFound = true | ||
if (name === '_unknown') unknownFound = true; | ||
/* singularDefaultSet logic */ | ||
if (def && def.defaultOption && !def.isMultiple() && event === 'set') singularDefaultSet = true | ||
if (def && def.defaultOption && !def.isMultiple() && event === 'set') singularDefaultSet = true; | ||
/* reset values once consumed and yielded */ | ||
if (def && def.isBoolean()) def = undefined | ||
if (def && def.isBoolean()) def = undefined; | ||
/* reset the def if it's a singular which has been set */ | ||
if (def && !def.multiple && t.isDefined(value) && value !== null) { | ||
def = undefined | ||
def = undefined; | ||
} | ||
value = undefined | ||
event = undefined | ||
name = undefined | ||
origArg = undefined | ||
value = undefined; | ||
event = undefined; | ||
name = undefined; | ||
origArg = undefined; | ||
} | ||
@@ -1190,3 +1212,3 @@ } | ||
const _value = new WeakMap() | ||
const _value = new WeakMap(); | ||
@@ -1198,5 +1220,5 @@ /** | ||
constructor (definition) { | ||
this.definition = new OptionDefinition(definition) | ||
this.state = null /* set or default */ | ||
this.resetToDefault() | ||
this.definition = new OptionDefinition(definition); | ||
this.state = null; /* set or default */ | ||
this.resetToDefault(); | ||
} | ||
@@ -1209,14 +1231,14 @@ | ||
set (val) { | ||
this._set(val, 'set') | ||
this._set(val, 'set'); | ||
} | ||
_set (val, state) { | ||
const def = this.definition | ||
const def = this.definition; | ||
if (def.isMultiple()) { | ||
/* don't add null or undefined to a multiple */ | ||
if (val !== null && val !== undefined) { | ||
const arr = this.get() | ||
if (this.state === 'default') arr.length = 0 | ||
arr.push(def.type(val)) | ||
this.state = state | ||
const arr = this.get(); | ||
if (this.state === 'default') arr.length = 0; | ||
arr.push(def.type(val)); | ||
this.state = state; | ||
} | ||
@@ -1226,9 +1248,9 @@ } else { | ||
if (!def.isMultiple() && this.state === 'set') { | ||
const err = new Error(`Singular option already set [${this.definition.name}=${this.get()}]`) | ||
err.name = 'ALREADY_SET' | ||
err.value = val | ||
err.optionName = def.name | ||
const err = new Error(`Singular option already set [${this.definition.name}=${this.get()}]`); | ||
err.name = 'ALREADY_SET'; | ||
err.value = val; | ||
err.optionName = def.name; | ||
throw err | ||
} else if (val === null || val === undefined) { | ||
_value.set(this, val) | ||
_value.set(this, val); | ||
// /* required to make 'partial: defaultOption with value equal to defaultValue 2' pass */ | ||
@@ -1239,4 +1261,4 @@ // if (!(def.defaultOption && !def.isMultiple())) { | ||
} else { | ||
_value.set(this, def.type(val)) | ||
this.state = state | ||
_value.set(this, def.type(val)); | ||
this.state = state; | ||
} | ||
@@ -1249,18 +1271,18 @@ } | ||
if (this.definition.isMultiple()) { | ||
_value.set(this, arrayify(this.definition.defaultValue).slice()) | ||
_value.set(this, arrayify(this.definition.defaultValue).slice()); | ||
} else { | ||
_value.set(this, this.definition.defaultValue) | ||
_value.set(this, this.definition.defaultValue); | ||
} | ||
} else { | ||
if (this.definition.isMultiple()) { | ||
_value.set(this, []) | ||
_value.set(this, []); | ||
} else { | ||
_value.set(this, null) | ||
_value.set(this, null); | ||
} | ||
} | ||
this.state = 'default' | ||
this.state = 'default'; | ||
} | ||
static create (definition) { | ||
definition = new OptionDefinition(definition) | ||
definition = new OptionDefinition(definition); | ||
if (definition.isBoolean()) { | ||
@@ -1276,3 +1298,3 @@ return FlagOption.create(definition) | ||
set (val) { | ||
super.set(true) | ||
super.set(true); | ||
} | ||
@@ -1290,12 +1312,12 @@ | ||
constructor (definitions) { | ||
super() | ||
super(); | ||
/** | ||
* @type {OptionDefinitions} | ||
*/ | ||
this.definitions = Definitions.from(definitions) | ||
this.definitions = Definitions.from(definitions); | ||
/* by default, an Output has an `_unknown` property and any options with defaultValues */ | ||
this.set('_unknown', Option.create({ name: '_unknown', multiple: true })) | ||
this.set('_unknown', Option.create({ name: '_unknown', multiple: true })); | ||
for (const def of this.definitions.whereDefaultValueSet()) { | ||
this.set(def.name, Option.create(def)) | ||
this.set(def.name, Option.create(def)); | ||
} | ||
@@ -1305,12 +1327,12 @@ } | ||
toObject (options) { | ||
options = options || {} | ||
const output = {} | ||
options = options || {}; | ||
const output = {}; | ||
for (const item of this) { | ||
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0] | ||
const option = item[1] | ||
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0]; | ||
const option = item[1]; | ||
if (name === '_unknown' && !option.get().length) continue | ||
output[name] = option.get() | ||
output[name] = option.get(); | ||
} | ||
if (options.skipUnknown) delete output._unknown | ||
if (options.skipUnknown) delete output._unknown; | ||
return output | ||
@@ -1322,30 +1344,30 @@ } | ||
toObject (options) { | ||
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown }) | ||
const superOutput = super.toObject(options) | ||
const unknown = superOutput._unknown | ||
delete superOutput._unknown | ||
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown }); | ||
const superOutput = super.toObject(options); | ||
const unknown = superOutput._unknown; | ||
delete superOutput._unknown; | ||
const grouped = { | ||
_all: superOutput | ||
} | ||
if (unknown && unknown.length) grouped._unknown = unknown | ||
}; | ||
if (unknown && unknown.length) grouped._unknown = unknown; | ||
this.definitions.whereGrouped().forEach(def => { | ||
const name = options.camelCase ? camelCase(def.name) : def.name | ||
const outputValue = superOutputNoCamel[def.name] | ||
const name = options.camelCase ? camelCase(def.name) : def.name; | ||
const outputValue = superOutputNoCamel[def.name]; | ||
for (const groupName of arrayify(def.group)) { | ||
grouped[groupName] = grouped[groupName] || {} | ||
grouped[groupName] = grouped[groupName] || {}; | ||
if (t.isDefined(outputValue)) { | ||
grouped[groupName][name] = outputValue | ||
grouped[groupName][name] = outputValue; | ||
} | ||
} | ||
}) | ||
}); | ||
this.definitions.whereNotGrouped().forEach(def => { | ||
const name = options.camelCase ? camelCase(def.name) : def.name | ||
const outputValue = superOutputNoCamel[def.name] | ||
const name = options.camelCase ? camelCase(def.name) : def.name; | ||
const outputValue = superOutputNoCamel[def.name]; | ||
if (t.isDefined(outputValue)) { | ||
if (!grouped._none) grouped._none = {} | ||
grouped._none[name] = outputValue | ||
if (!grouped._none) grouped._none = {}; | ||
grouped._none[name] = outputValue; | ||
} | ||
}) | ||
}); | ||
return grouped | ||
@@ -1364,3 +1386,3 @@ } | ||
* | ||
* @param {module:definition[]} - An array of [OptionDefinition](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) objects | ||
* @param {Array<OptionDefinition>} - An array of [OptionDefinition](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) objects | ||
* @param {object} [options] - Options. | ||
@@ -1371,2 +1393,3 @@ * @param {string[]} [options.argv] - An array of strings which, if present will be parsed instead of `process.argv`. | ||
* @param {boolean} [options.camelCase] - If `true`, options with hypenated names (e.g. `move-to`) will be returned in camel-case (e.g. `moveTo`). | ||
* @param {boolean} [options.caseInsensitive] - If `true`, the case of each option name or alias parsed is insignificant. In other words, both `--Verbose` and `--verbose`, `-V` and `-v` would be equivalent. Defaults to false. | ||
* @returns {object} | ||
@@ -1387,27 +1410,28 @@ * @throws `UNKNOWN_OPTION` If `options.partial` is false and the user set an undefined option. The `err.optionName` property contains the arg that specified an unknown option, e.g. `--one`. | ||
function commandLineArgs (optionDefinitions, options) { | ||
options = options || {} | ||
if (options.stopAtFirstUnknown) options.partial = true | ||
optionDefinitions = Definitions.from(optionDefinitions) | ||
options = options || {}; | ||
if (options.stopAtFirstUnknown) options.partial = true; | ||
optionDefinitions = Definitions.from(optionDefinitions, options.caseInsensitive); | ||
const parser = new ArgvParser(optionDefinitions, { | ||
argv: options.argv, | ||
stopAtFirstUnknown: options.stopAtFirstUnknown | ||
}) | ||
stopAtFirstUnknown: options.stopAtFirstUnknown, | ||
caseInsensitive: options.caseInsensitive | ||
}); | ||
const OutputClass = optionDefinitions.isGrouped() ? GroupedOutput : Output | ||
const output = new OutputClass(optionDefinitions) | ||
const OutputClass = optionDefinitions.isGrouped() ? GroupedOutput : Output; | ||
const output = new OutputClass(optionDefinitions); | ||
/* Iterate the parser setting each known value to the output. Optionally, throw on unknowns. */ | ||
for (const argInfo of parser) { | ||
const arg = argInfo.subArg || argInfo.arg | ||
const arg = argInfo.subArg || argInfo.arg; | ||
if (!options.partial) { | ||
if (argInfo.event === 'unknown_value') { | ||
const err = new Error(`Unknown value: ${arg}`) | ||
err.name = 'UNKNOWN_VALUE' | ||
err.value = arg | ||
const err = new Error(`Unknown value: ${arg}`); | ||
err.name = 'UNKNOWN_VALUE'; | ||
err.value = arg; | ||
throw err | ||
} else if (argInfo.event === 'unknown_option') { | ||
const err = new Error(`Unknown option: ${arg}`) | ||
err.name = 'UNKNOWN_OPTION' | ||
err.optionName = arg | ||
const err = new Error(`Unknown option: ${arg}`); | ||
err.name = 'UNKNOWN_OPTION'; | ||
err.optionName = arg; | ||
throw err | ||
@@ -1417,14 +1441,14 @@ } | ||
let option | ||
let option; | ||
if (output.has(argInfo.name)) { | ||
option = output.get(argInfo.name) | ||
option = output.get(argInfo.name); | ||
} else { | ||
option = Option.create(argInfo.def) | ||
output.set(argInfo.name, option) | ||
option = Option.create(argInfo.def); | ||
output.set(argInfo.name, option); | ||
} | ||
if (argInfo.name === '_unknown') { | ||
option.set(arg) | ||
option.set(arg); | ||
} else { | ||
option.set(argInfo.value) | ||
option.set(argInfo.value); | ||
} | ||
@@ -1436,2 +1460,2 @@ } | ||
module.exports = commandLineArgs | ||
module.exports = commandLineArgs; |
{ | ||
"name": "command-line-args", | ||
"version": "5.1.3", | ||
"version": "5.2.0", | ||
"description": "A mature, feature-complete library to parse command-line options.", | ||
"repository": "https://github.com/75lb/command-line-args", | ||
"scripts": { | ||
"test": "npm run test:js && npm run test:mjs", | ||
"test:js": "rollup test/tests.mjs -f cjs -e 'test-runner,assert,lodash.camelcase' -o dist/tests.js && node dist/tests.js", | ||
"test": "npm run dist && npm run test:js && npm run test:mjs", | ||
"test:js": "node dist/tests.js", | ||
"test:mjs": "node --experimental-modules test/tests.mjs", | ||
"travis-test": "node dist/tests.js", | ||
"test:ci": "npm run test:js", | ||
"docs": "jsdoc2md -c jsdoc.conf index.mjs > doc/API.md && jsdoc2md -c jsdoc.conf lib/option-definition.mjs > doc/option-definition.md", | ||
"cover": "nyc --reporter=text-lcov test-runner test/*.js test/internals/*.js | coveralls", | ||
"dist": "rollup index.mjs -f cjs -e 'lodash.camelcase' -o dist/index.js && rollup index.mjs -f esm -e 'lodash.camelcase' -o dist/index.mjs" | ||
"dist": "rollup index.mjs -f cjs -e 'lodash.camelcase' -o dist/index.js && rollup index.mjs -f esm -e 'lodash.camelcase' -o dist/index.mjs && rollup test/tests.mjs -f cjs -e 'test-runner,assert,lodash.camelcase' -o dist/tests.js" | ||
}, | ||
@@ -42,3 +42,3 @@ "main": "dist/index.js", | ||
"coveralls": "^3.1.1", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"jsdoc-to-markdown": "^7.0.1", | ||
"rollup": "~1.7.4", | ||
@@ -45,0 +45,0 @@ "test-runner": "^0.5.1" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
116729
3496